[Solved] org.hibernate.HibernateException: No CurrentSessionContext configured

Hibernate requires a lot of configurations and sometimes you get weird exception and there is no clue that it’s because of some missing configuration.

org.hibernate.HibernateException: No CurrentSessionContext configured

I was working on a simple hibernate web application and getting below exception.

When I looked at the source code, the statement that was throwing the exception was;

My Hibernate configuration was like this:

hibernate.cfg.xml

Fix for org.hibernate.HibernateException: No CurrentSessionContext configured

Everything looked fine to me and the exception didn’t clearly says what is missing.

After going through the SessionFactoryImpl, I found that we also need to configure the current session context class to get the current session.

This is done in method buildCurrentSessionContext() and it looks for property hibernate.current_session_context_class.

From the method body, it became clear that the value of this property should be:

  1. jta for getting JTASessionContext
  2. managed for ManagedSessionContext
  3. thread for ThreadLocalSessionContext

The method also works if we provide above class name. So when I added this property in the hibernate configuration file, program started working fine.

Hibernate CurrentSessionContext class configuration example

Sample ways to define current_session_context_class property are:

Use below configuration if your hibernate configuration file is having xml format.

Thats’s all for fixing org.hibernate.HibernateException: No CurrentSessionContext configured. I hope it will save you some time.

By admin

Leave a Reply

%d bloggers like this: