2
votes

I have a trouble when running my web application that tries to get all the information from the table snowboard and put it in a list which I will then print out in the xHtml. But I get this exception down below.

org.hibernate.TransactionException: nested transactions not supported

The thing is that I have no clue why this exception happens so would appreciate some explanation. Also if you find any trouble in the code that would be fantastic.

This is the exception that I get.

HibernateUtil

public class HibernateUtil {

    private static final SessionFactory sessionFactory;

    static {



        try {
            // Create the SessionFactory from standard (hibernate.cfg.xml) 
            // config file.
            sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
        } catch (Throwable ex) {
            // Log the exception. 
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
}

SnowHelper, HelperClass

public class SnowHelper {

    Session session = null;

    public SnowHelper() {
        this.session = HibernateUtil.getSessionFactory().getCurrentSession();
    }

    public List getSnowboards() {
        List<Snowboard> snowboardList = null;

        try {
            Transaction tx = session.beginTransaction();
            Query q = session.createQuery("from Snowboard");
            snowboardList = (List<Snowboard>) q.list();

        } catch (Exception e) {
            e.printStackTrace();
        }

        return snowboardList;
    }
}

HibernateCfg

<hibernate-configuration>
  <session-factory>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://cpsrv01.misshosting.com:3306/etvffqgz_snowshop</property>
    <property name="hibernate.connection.username">etvffqgz_user</property>
    <property name="hibernate.connection.password">759486456</property>
    <property name="hibernate.show_sql">true</property>
    <property name="hibernate.current_session_context_class">thread</property>
    <property name="hibernate.query.factory_class">org.hibernate.hql.internal.classic.ClassicQueryTranslatorFactory</property>
    <mapping resource="Hibernate/Account.hbm.xml"/>
    <mapping resource="Hibernate/Snowboard.hbm.xml"/>
  </session-factory>
</hibernate-configuration>

SnowboardBean, ManagedBean class

@Named(value = "snowboardBean")
@Dependent
public class SnowboardBean {

    private List<Snowboard> snowList;
    private SnowHelper snow;
    /**
     * Creates a new instance of SnowboardBean
     */
    public SnowboardBean() {
        snowList = new ArrayList<>();
        snow = new SnowHelper();

        snowList = snow.getSnowboards();
    }

    /**
     * @return the snowList
     */
    public List<Snowboard> getSnowList() {
        return snowList;
    }

}
1

1 Answers

1
votes

In this bit here.. commit your transaction

Transaction tx = session.beginTransaction();
Query q = session.createQuery("from Snowboard");
snowboardList = (List<Snowboard>) q.list();
tx.commit();

Otherwise you simply open a new transaction without closing it every time you invoke this method.. eventually one of them is opened while some other is not commited yet.

If you were using 'container managed transactions' (provided by Spring of EJB's) you would not have to worry about explicitly committing your transactions. Here you are using 'extended transaction management' and you have to take care of that yourself.