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

Filed Under: Hibernate

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.

org.hibernate.HibernateException: No CurrentSessionContext configured!

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

Session session = sessionFactory.getCurrentSession();

My Hibernate configuration was like this:


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
		"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.connection.datasource">java:comp/env/jdbc/MyLocalDB</property>

        <!-- Mapping with model class containing annotations -->
	<mapping class="com.journaldev.servlet.hibernate.model.Employee"/>

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:

<property name="hibernate.current_session_context_class">thread</property>

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

<property name="hibernate.current_session_context_class">

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


  1. Christian Arias says:

    Thanks Pankaj, very useful.

    1. srinivas says:

      not at all fixed the issue my issue as follows
      java.lang.ClassNotFoundException: org.hibernate.context.ManagedSessionContext

Comments are closed.

Generic selectors
Exact matches only
Search in title
Search in content