Tuesday, May 17, 2011

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

While developing a web application using SpringMVC and Hibernate I got "No Hibernate Session bound to thread Exception" becuase of some configuration issue.
Here I am going to explain how I resolved the issue.

I used the SpringMVC/@Controller approach and configured the Web related Spring configuration in dispatcher-servlet.xml as follows:

 <context:annotation-config"/>
 <context:component-scan base-package="com.sivalabs"/>
 
 
 

 
  
 



I have configured my business serices and DAOs in applicationContext.xml as follows:

 
 <context:component-scan base-package="com.sivalabs"/>
 <context:property-placeholder location="classpath:app-config.properties"/>
 
 <tx:annotation-driven/>
 
 
 
 
     
     
       
         UserAccount.hbm.xml
         Contact.hbm.xml
       
     
     
       
   ${hibernate.dialect}
   ${hibernate.show_sql}
   
     
   
 
 
  .....
  .....
 


To enable the transaction management I have used @Transactional annotation on my business services.
package com.sivalabs.homeautomation.useraccounts;
@Service
@Transactional
public class UserAccountsService 
{
 @Autowired
 private UserAccountsDAO userAccountsDAO;
 
 public UserAccount login(Credentials credentials) {
  return userAccountsDAO.login(credentials);
 }
}

But when I invoked UserAccountsService.login() method I got the the below error:
org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
 at org.springframework.orm.hibernate3.SpringSessionContext.currentSession(SpringSessionContext.java:63)
 at org.hibernate.impl.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:544)
 at com.sivalabs.homeautomation.useraccounts.UserAccountsDAO.login(UserAccountsDAO.java:30)
 at com.sivalabs.homeautomation.useraccounts.UserAccountsService.login(UserAccountsService.java:25)
 at com.sivalabs.homeautomation.useraccounts.LoginController.login(LoginController.java:51)

Here I have enabled the Annotation based configuration using <context:annotation-config/>.
I have configured the base package containing the Spring beans using <context:component-scan base-package="com.sivalabs"/>.
I have enabled the Annotation based Transaction Management using <tx:annotation-driven/> and @Transactional.

But still I am getting "No Hibernate Session bound to thread" Exception. Why?

Here is the reason:

In Spring reference documentation we can found the below mentioned important note:

"<tx:annotation-driven/> only looks for @Transactional on beans in the same application context it is defined in. This means that, if you put <tx:annotation-driven/> in a WebApplicationContext for a DispatcherServlet, it only checks for @Transactional beans in your controllers, and not your services."


So when my application is started first it loads the beans configured in dispatcher-servlet.xml and then look in applicationContext.xml. As i mentioned "com.sivalabs" as my base-package to scan for Spring beans my business services and DAOs which are annotated with @Service, @Repository will also be loaded by the container. Later when Spring tries to load beans from applicationContext.xml it won't load my services and DAOs as they are already loaded by parent ApplicaationContext. So the <tx:annotation-driven/> wont be applied for business services or DAOs annotated with @Transactional.

Solution1: If you are following package-by-layer approach:
Probably you may put all your controllers in one package say com.sivalabs.appname.web.controllers

Then change the <context:annotation-config/> configuration in dispatcher-servlet.xml as:
<context:component-scan base-package="com.sivalabs.appname.web.controllers"/>
With this only the controllers annotated with @Controller in com.sivalabs.appname.web.controllers package will be loaded by parent ApplicationContext and rest of the services, DAOs will be loaded by child ApplicationContext.

Solution2: If you are following package-by-feature approach:
If you follow the package-by-feature approach, you will put all the Controller, Service, DAOs related to one feature in one package.
With this the Controllers, Services, DAOs will be spanned across the packages.

Then change the <context:annotation-config/> configuration in dispatcher-servlet.xml as:

 <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>


With this only the controllers annotated with @Controller will be loaded by parent ApplicationContext.
And the Services, DAOs will be loaded by child ApplicationContext and <tx:annotation-driven/> will be applied.

8 comments:

  1. hi ..i have tried by above solution it is not working for me again it is coming same error.............

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


    ...anybody pls help me to resolve this error

    ReplyDelete
  2. Thank you. The second solution worked perfectly :)

    ReplyDelete
  3. Thanks, the post made my day!!

    ReplyDelete
  4. It worked. Or just add the in the servlet-context.xml

    ReplyDelete
  5. Thanks, you saved my day too :)

    ReplyDelete
  6. ApplicationContext created from dispatcher-servlet.xml is a parent one or child one ?
    Beans defined in dispatcher-servlet can not be accessed in applicationContext.xml but vice versa can be done.

    ReplyDelete
  7. Hi siva,

    I am not following MVC model.But i am using Servlets ,DAO and some utils. I am Accessing the DAO classes from servlets , i am unable to fix this problem could you help me...?

    in My Dao i have
    "@Transactional(propagation=Propagation.REQUIRED,isolation=Isolation.REPEATABLE_READ)" on Method.

    in spring configuration file i am enabling tx.mngmt








    while i am calling
    "//Getting current session object.
    session=sessionFactory.getCurrentSession();" in my DAO class method i am getting this exception, i have tried in different ways but unable to solve this. suggest me hoe to fix it.

    Thanks
    raju.

    ReplyDelete