Apr 23, 2010

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

When we implemented transactions using autowiring and TxManager in the spring config xml file, we ended up in having the well known org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here.

I was googling a bit on "No Hibernate Session bound to thread". Emphatically, I ended up with innumerous answers in several sites on the Internet. Springsource forum is not exception to this! Abundant conversations have been happening on this topic all around!!!

But there were a few best practices that I was able to compile from the chit-chats happening across the Internet. I thought of sharing the same here so that it would help some one some day in the near future!

  1. First and foremost, configure the transaction manager properly : Use @Transaction annotation near your code and have a transactionManager bean in your spring config file.
  2. when integration a transaction manager, do not use hibernate.current_session_context_class and hibernate.transaction_factory_class in the hibernate properties unless you have proper reasons to
  3. Never call sessionFactory.openSession()
  4. Use ApplicationContext rather than using BeanFactory
  5. Have single dao instances running across the Application
  6. Above all, most importantly, ensure that the current application context has the component scan for the beans where you have added the @transactional annotations. as per spring documentation's @ Trasactional usage guidelines,

<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. See Section 15.2, “The DispatcherServlet” for more information.

Point # 6 was the culprit in our case and that was then resolved! So the lessson was You are likely to get "No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here" if you do not adhere to any of the above pointers.
PArticularly, if you are going to have the applicationContext.xml with the TxManager and the servletname-servlet.xml with the component scan annotations, then you are more likely to get this "No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here" exception since the TxManager's transaction holds good for the current application context only (which in this case does not have the bean definitions for the service or the daos where you have the @ Trasactional annotations)

If you find the information pretty helpful, I would really be happy if you would keep me posted via the comments form displayed under this article! If you had wanted some other information related to the same topic, I would suggest you to drop a note to me using the comments form for that would help me in getting back to you with the details you are in need of!

13 comments:

  1. Thanks for your sharing, it is very informative.
    I have the same case as you, putting in the application-context.xml and in XXX-servlet.xml.

    I got some ideas from your information, but still not very sure how to fix it. Could you say something more?

    ReplyDelete
  2. Thanks for your sharing, it is very informative.
    I have the same case as you, putting in the application-context.xml and in XXX-servlet.xml.

    I got some ideas from your information, but still not very sure how to fix it. Could you say something more?

    ReplyDelete
  3. How can I really fix it? I have same case with you.

    ReplyDelete
  4. Hi Anonymous,

    Based on your request, here is another article related to TxManager implementation via tx:annotation driven and @Transactional annotations. Let me know the details of the problems that you have. I would surely help you in getting rid of it if you could get me more details.

    Also, check out whether the following article helps!

    http://javaprogrammingtips4u.blogspot.com/2010/04/how-to-use-transaction-manager-with.html

    ReplyDelete
  5. Hi Rico,

    Based on your comment, I have come up with the steps that could help you in getting rid of the exception.

    Hi Anonymous,

    Based on your request, here is another article related to TxManager implementation via tx:annotation driven and @Transactional annotations. Let me know the details of the problems that you have. I would surely help you in getting rid of it if you could get me more details.

    Also, check out whether the following article helps!

    http://javaprogrammingtips4u.blogspot.com/2010/04/how-to-use-transaction-manager-with.html

    Hope this helps. Else,post the details of the problem and we shall get the problem resolved!

    ReplyDelete
  6. Thanks for sharing...

    ReplyDelete
  7. Thanks for your sharing..really help full thnk u very much..and i m new in spring frmk and hibernate..pls give a full example with minute points in hibernate and spring technology...

    ReplyDelete
  8. Hi Thanks for the excellent article.

    I have having the following problem. I have a controller called ServiceController that parses all the requests of type /rpc/ and forwards it to the corresponding RPC service implementation.

    So if the call was for a remote method in CustomerServiceImpl, the service controller will first check teh request for security and then forward teh call payload to the appropriate method in CustomerServiceImpl.

    Now, in the CustomerServiceImpl I make a call to the dao. And on return from DAO call, I use dozer to convert the object to DTO. I ofcourse get org.hibernate.LazyInitializationException: could not initialize proxy - no Session in the dozer call.

    Now, after reading your article, I made the service transactional but I am still getting this error. Probably because the spring controller in not scanning my service beans for "@Transactional" annotation. I even put the component scan for my service dir.

    Do know know what could be the problem?

    Note that if I put OpenSessionInViewFilter in my web.xml, everything goes as required.

    ReplyDelete
  9. Hi

    I did all the steps that you mentioned but transaction is not working for me yet.
    I suspect that it has to do something with the component-scan as it is not able to set the transaction on my service bean.

    So teh situation I have is, a central controller "ServiceController" parses all the incoming requests and after performing some security checks, it forwards the requests to the corresponding service implementation.

    I have defined the in a WebApplicationContext for a DispatcherServlet, how do I force it to scan my services directory?

    ReplyDelete
  10. I'm using @Transactional within a GWT Service Servlet and had the same issue. Using a "org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor" as described here : http://www.paulcodding.com/blog/2008/01/21/using-the-opensessioninviewinterceptor-for-spring-hibernate3/

    did immediately solve my problem !

    ReplyDelete
  11. After 3 hours of gooling, explanation in point 6 saved my day, thanks

    ReplyDelete
  12. Make sure you're using the correct annotations for @Transactional (org.springframework.transaction.annotation.Transactional). Liferay, and others have their own incompatible annotations. Quite hard to notice when Eclipse folds your imports.

    ReplyDelete
  13. 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:-




    ****


    Thanks,
    Prabhat Kumar Kashyap

    ReplyDelete