No session currently bound to execution context

14,597

Solution 1

If you are using Dropwizard Hibernate. You need to add @UnitOfWork annotation to your Resource method. More info within dropwizard manual, hibernate chapter.

Solution 2

Can you try with : session.openSession() - It tell hibernate to always opens a new session and you have to close once you are done with the operations. With session.getCurrentSession(), hibernate returns a session bound to a context that you don't need to close and only need to set the hibernate.current_session_context_class to thread.

You can also configure session with SpringSessionContext, if your application is Spring based.

Edit your hibernate-cfg.xml with below line:

hibernate.current_session_context_class=org.springframework.orm.hibernate3.SpringSessionContext

What above line will do?

Making session context class as "org.springframework.orm.hibernate3.SpringSessionContext ", Hibernate will assume it is executing inside of a Spring transactional context (i.e. through a Spring transactional aspect) and Spring will now manage your transaction for you. However if you call getCurrentSession() outside of such a context, Hibernate will throw an exception complaining that no Session is bound to the thread.

Share:
14,597

Related videos on Youtube

Srinivas
Author by

Srinivas

Updated on February 17, 2020

Comments

  • Srinivas
    Srinivas about 4 years

    I got below exception when I used session.getCurrentSession().

    I have mentioned

    hibernate.current_session_context_class: managed
    
    org.hibernate.HibernateException: No session currently bound to execution context
        at org.hibernate.context.internal.ManagedSessionContext.currentSession(ManagedSessionContext.java:75)
        at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:1014)
        at io.dropwizard.hibernate.AbstractDAO.currentSession(AbstractDAO.java:36)
        at io.dropwizard.hibernate.AbstractDAO.persist(AbstractDAO.java:149)
    

    I use this with dropwizard. Can anyone help me to solve this?

    • Master Mind
      Master Mind almost 9 years
      Code you please provide the code which throws the exception
    • Srinivas
      Srinivas almost 9 years
      Initially I wrote Query for getting the information from DB. This will executed. But after that I used to save the data into the other tables. It gives an error. I thought session will be closed after execute the my first DB interaction. But this should be expected as I use hibernate.current_session_context_class: managed
    • Srinivas
      Srinivas almost 9 years
      Code will be too long to paste hear
    • Master Mind
      Master Mind almost 9 years
      you run in application class and configuration of your HibernateBundle at least so we could help
    • cнŝdk
      cнŝdk almost 9 years
      I think the best approach here is to open and close the session for each action.
    • cнŝdk
      cнŝdk almost 9 years
      How are you declaring your session, show us the relevant code.
  • Swetabja Hazra
    Swetabja Hazra almost 6 years
    The best answer
  • MetalMichael
    MetalMichael over 4 years
  • yunspace
    yunspace about 4 years
    Thanks @MetalMichael I've updated the link in the answer