No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here

ucacppw
ucacppw used Ask the Experts™
on
I have a legacy webapp which uses a servlet filter to wrap each request as follows

           
HibernateUtil.beginTransaction();
chain.doFilter(request, response);         
(boilerplate code which then calls one of the following...)          
HibernateUtil.commitTransaction();            
or            
HibernateUtil.rollbackTransaction();

HibernateUtil created a sessionFactory according to info in hibernate.cfg.xml as follows (I was using hibernate3 contextual sessions)

<session-factory>
<property name="connection.datasource">java:comp/env/jdbc/filmSource</property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>      
<property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
<property name="hibernate.current_session_context_class">thread</property>
<mapping resource="film/hibernate/Commission.hbm.xml" />
</session-factory>
</hibernate-configuration>

Everything worked fine.

However the app has now been integrated with Spring and I want to configure the Hibernate session in springs applicationContext.xml

Here is what I currently have in there..

  <bean id="myDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="java:comp/env/jdbc/filmSource"/>
  </bean>
  <bean id="mySessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="myDataSource"/>
    <property name="mappingResources">
    <list>
      <value>film/hibernate/Commission.hbm.xml</value>
    </list>
    </property>
<property name="hibernateProperties">

<props>
  <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
  <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
  <prop key="hibernate.cache.use_second_level_cache">true</prop>
  <prop key="hibernate.current_session_context_class">thread</prop>
  <prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</prop>
  <prop key="hibernate.show_sql">true</prop>
</props>
    </property>
</bean>

<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="mySessionFactory" />
</bean>

<bean id="hibernateUtil" class="film.web.framework.util.HibernateUtil"/>

and here is my hibernateUtil which has now been modified for Spring.

public class HibernateUtil {
      private static Log log = LogFactory.getLog(HibernateUtil.class);

      private static SessionFactory sessionFactory;

      @Autowired
      public void setMySessionFactory(SessionFactory value) {
            sessionFactory = value;      
      }
      

      public static SessionFactory getSessionFactory() {
            return sessionFactory;
      }

      public static Session getSession() {
            return getSessionFactory().getCurrentSession();
      }

      public static void beginTransaction() {
            getSessionFactory().getCurrentSession().beginTransaction();

      }

      public static void commitTransaction() {
            getSessionFactory().getCurrentSession().getTransaction().commit();
      }

      public static void rollbackTransaction() {
            getSessionFactory().getCurrentSession().getTransaction().rollback();
      }

}

The problem is when the request filter calls HibernateUtil.beginTransaction() I get the following error:

org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Are you using your session factory defined in applicationContext.xml?
It seems to me you need to use it in your code. Are you calling setMySessionFactory with the value of bean "mySessionFactory" you get from the ApplicationContext (or making sure you use the bean in any other way)?

Author

Commented:
ozlevanon the answer to both your questions is yes. In the java class HibernateUtil, the spring annotation Autowired ensures the sessionFactory is set as defined in applicationContext.xml. No java code calls setMySessionFactory but it is definitely being called as I verified with a breakpoint. It is therefore definitely being called via spring according to the settings in applicationContext.xml.

@Autowired
      public void setMySessionFactory(SessionFactory value) {
            sessionFactory = value;      
      }
Commented:
I had this question answered in the spring forum

http://forum.springsource.org/showthread.php?p=251515#post251515

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial