When using Hibernate in your application, there are bright chances that you have encountered the error org.hibernate.HibernateException: No CurrentSessionContext configured!. Stack trace of the error would have been :
Exception in thread "main" org.hibernate.HibernateException: No CurrentSessionContext configured!
org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:690)
at com.codippa.Main.main(Main.java:16)
Solution
If you are using Hibernate version 3.1 and above, then just check your configuration file (usually hibernate.cfg.xml) that it has a property like :
If the property is present then check its value to be
thread
With the above property added and set to thread the error should go away. The property alongwith its value should look like :
thread
Reason
Any application using Hibernate requires a session in a particular scope and context. A session is active throughout the scope of a given context. Context varies according to the type of application and how the transactions in it are being managed. That is, they are managed by the application itself, by the container etc.
Starting from Hibernate v3.1, a new interface
org.hibernate.context.CurrentSessionContext
and a configuration property hibernate.current_session_context_class
was added. This was done to make the scope and context of the sessions as configurable and pluggable. This property has to be provided to indicate how the session required to perform database operations will be managed. It may have three possible values jta, thread and managed which correspond to the three implementations of org.hibernate.context.CurrentSessionContext
interface. They are :
- org.hibernate.context.JTASessionContext : When
hibernate.current_session_context_class
property is set toorg.hibernate.context.JTASessionContext
, implies that the Session will be scoped by a JTA transaction. If the session is not available at the timegetCurrentSession()
is called, a new session will be opened and associated with the transaction. - org.hibernate.context.ThreadLocalSessionContext : With
hibernate.current_session_context_class
property set toorg.hibernate.context.ThreadLocalSessionContext
, implies that the Session will be scoped by the current executing thread. Session could only be used aftersession.beginTransaction()
is called. - org.hibernate.context.ManagedLocalSessionContext : When
hibernate.current_session_context_class
property is set toorg.hibernate.context.ManagedSessionContext
, implies that the Session will be managed by some external entity. This entity does not open new sessions but only binds / unbinds them and exposes them to the application.
Let’s tweak in :
- There is no need to give the full class names as a value to the property
hibernate.current_session_context_class
. Short names corresponding to each of these classes can be provided as given in the solution above. These short names are jta, thread and managed where jta refers to the classorg.hibernate.context.JTASessionContext
and so on. - If the application is running in standalone mode, that is, triggered by a java main class, then
hibernate.current_session_context_class
property should be set to thread - If you provide the value of this property as managed when your application is running in a standalone mode (triggered by a java main class), you will get an error like
Exception in thread "main" org.hibernate.HibernateException: No session currently bound to execution context
- If you provide the value of this property as jta when your application is running in a standalone mode (triggered by a java main class), you will get an error like
Exception in thread "main" org.hibernate.HibernateException: No TransactionManagerLookup specified
- From Hibernate v4.0,
CurrentSessionContext
was moved to the packageorg.hibernate.context.spi
. - From Hibernate v4.0, all the three implementations of
CurrentSessionContext
were moved to the packageorg.hibernate.context.internal.
Solution Worked ??? Share and spread this post for others to benefit from this solution.
Thanks.it helped me a lot.
You are most welcome…Keep visiting codippa.com
Thank you sir, it worked with me and I have understood the concept now. You are a guru and will keep visitiyng codippa.com for new insights.
Glad to know that it helped…You can also subscribe for updates on new posts !!!