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

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

chain.doFilter(request, response);         
(boilerplate code which then calls one of the following...)          

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

<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" />

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 id="mySessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="myDataSource"/>
    <property name="mappingResources">
<property name="hibernateProperties">

  <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>

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

<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;

      public void setMySessionFactory(SessionFactory value) {
            sessionFactory = value;      

      public static SessionFactory getSessionFactory() {
            return sessionFactory;

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

      public static void beginTransaction() {


      public static void commitTransaction() {

      public static void rollbackTransaction() {


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.
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)?


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.

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

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