Site icon codippa

How to solve org.hibernate.HibernateException: No CurrentSessionContext configured!

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 :

  1. org.hibernate.context.JTASessionContext : When hibernate.current_session_context_class property is set to org.hibernate.context.JTASessionContext, implies that the Session will be scoped by a JTA transaction. If the session is not available at the time getCurrentSession() is called, a new session will be opened and associated with the transaction.
  2. org.hibernate.context.ThreadLocalSessionContext : With hibernate.current_session_context_class property set to org.hibernate.context.ThreadLocalSessionContext, implies that the Session will be scoped by the current executing thread. Session could only be used after session.beginTransaction() is called.
  3. org.hibernate.context.ManagedLocalSessionContext : When hibernate.current_session_context_class property is set to org.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 :

  1. 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 class org.hibernate.context.JTASessionContext and so on.
  2. 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
  3. 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
  4. 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
  5. From Hibernate v4.0, CurrentSessionContext was moved to the package org.hibernate.context.spi.
  6. From Hibernate v4.0, all the three implementations of CurrentSessionContext were moved to the package org.hibernate.context.internal.

 

Solution Worked ???  Share and spread this post for others to benefit from this solution.

Exit mobile version