SQL Zone is brought to you in partnership with:

I am Siva, a passionate java developer, open source enthusiast, blogger. I like to cover Java, Struts, Spring, Hibernate, Ajax Tutorials, How-To's and Best Practices. Sivaprasadreddy is a DZone MVB and is not an employee of DZone and has posted 34 posts at DZone. You can read more from them at their website. View Full User Profile

How I resolved SpringMVC+Hibernate Error: No Hibernate Session bound to thread

  • submit to reddit
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 annotation approach and configured the Web related Spring configuration in dispatcher-servlet.xml as follows:

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

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"></bean>
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"></bean>

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/" p:suffix=".jsp"></bean>

<bean id="messageSource"

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


<bean id="transactionManager"

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="mappingResources">
<property name="hibernateProperties">
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">

To enable the transaction management I have used @Transactional annotation on my business services.

package com.sivalabs.homeautomation.useraccounts;
public class UserAccountsService
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:component-scan base-package="com.sivalabs" use-default-filters="false">
<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.

From : http://sivalabs.blogspot.com/2011/05/springmvc-hibernate-error-no-hibernate.html


Published at DZone with permission of Sivaprasadreddy Katamreddy, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)


Manuel Jordan replied on Thu, 2011/06/02 - 5:20pm


Thanks a lot for write this Tutorial, just few minutes ago, it was a valuable guidance to me to resolve the same exception. But I did other thing:

I have this file applicationContext-service.xml where only I have defined

<context:annotation-config />
<context:component-scan base-package="com.jordan.manuel.service.impl" />	
<context:component-scan base-package="com.jordan.manuel.business.process.impl" />
<context:component-scan base-package="com.jordan.manuel.service.utils.factory" />
<context:component-scan base-package="com.jordan.manuel.service.utils.regex" />
<context:component-scan base-package="com.jordan.manuel.service.utils.dateandtime" />

Practically all my BO, DAO, Factories and other utils classes are scanned there. I use JUnit to do my testing, with Transactional control, but with Spring MVC I got the same exception.

BTW I am working with STS 2.6.1 and I created a Spring MVC Project (Spring Template Project), it created many files for default, among them this one controllers.xml

But I did the follow. I added only

<context:component-scan base-package="com.jordan.manuel" />
<context:component-scan base-package="com.jordan.manuel.web.validator" />
<context:component-scan base-package="com.jordan.manuel.web.controller" />>
But with the exception I added (twice or repeated) on controllers.xml
<context:annotation-config />

Good By Exception.

Even when I am repeating the <context:annotation-config /> , each component-scan is unique, therefore seems really only mandatory repeat <tx:annotation-driven/> one for the service layer (applicationContext-service.xml ) and other for the web layer ( controllers.xml)

Arju Surendra replied on Sun, 2011/09/25 - 1:38pm

I was able to solve the problem by moving the <tx:annotation-driven/> into the <app>-servlet.xml spring bean file.

Thus i guess the ordering is important.

session = sessionFactory.getCurrentSession();

Should now work.

 The other options i tried was session = sessionFactory.openSession();

But i guess this session wont be managed by the IOC container.


Emma Watson replied on Fri, 2012/03/30 - 12:26pm

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


Gamal Shaban replied on Tue, 2012/04/03 - 10:32am

I had this issue too, but only it was with the transaction manager, @transactional notaction it couldn't be seen, so i cannot use the update methods, simply i've moved the  <tx:annotation-driven/> from the application-context.xml file into the dispatcher-servlet.xml, when i used the filter attrbute it couldn't see the service classes.

Prabhat Kumar replied on Thu, 2012/11/01 - 5:12pm

I was getting the same error, but finally I fixed it, added @Transactional to before the class implementation, like:-@Repository**@Transactional**public class WorkFromHomeDAO {application-context.xml file declarations starts with the base packages to scan as:-<context:annotation-config /> <mvc:annotation-driven /> <tx:annotation-driven transaction-manager="transactionManager"/>  **<context:component-scan base-package="com.wc.prabhat" />**<context:property-placeholder location="classpath:conf/properties/*.properties" />Thanks,Prabhat Kumar KashyapHyderabad, India.

Gabriel Alejand... replied on Sun, 2014/02/23 - 10:04am in response to: Arju Surendra

After spending some hours trying to solve the "no session found for current thread" error, your solution was the only one that worked for me. Thanks a lot!

BTW: It's not the first problem I had using spring caused by having context splitted in different files.

Best regards,


Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.