Hibernate : No CurrentSessionContext configured
Solution 1
As per the best of my knowledge, your configuration is not proper for current session. Instead of
<property name="hibernate.current_session_context_class">org.hibernate.context.ThreadLocalSessionContext</property>
use
<property name="hibernate.current_session_context_class">thread</property>
For more information on this, please visit this link:
Specially, read the last lines of last paragraph.
For those who're using Hibernate 4.1,
<property name="hibernate.current_session_context_class">org.hibernate.context.internal.ThreadLocalSessionContext</property>
Solution 2
Try changing
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
to :
Session session = HibernateUtil.getSessionFactory().openSession();
Solution 3
You need to change your property:
org.hibernate.context.internal.ThreadLocalSessionContext to Thread
Solution 4
I know this is an old post but just in case someone comes here as I did - this is working:
package com.spring.util;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
private static HibernateUtil instance = new HibernateUtil();
private SessionFactory sessionFactory;
private HibernateUtil(){
this.sessionFactory = buildSessionFactory();
}
private synchronized static SessionFactory buildSessionFactory() {
return new Configuration().configure().buildSessionFactory();
}
public static HibernateUtil getInstance() {
if(instance == null){
return new HibernateUtil();
}
return instance;
}
public SessionFactory getSessionFactory() {
return sessionFactory;
}
}
Now to invoke this is quite simple:
SessionFactory sessionFactory = HibernateUtil.getInstance().getSessionFactory();
And the next step would be something like bellow:
public void saveOrUpdate(MyObject myObj) {
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();
session.saveOrUpdate(myObj);
session.getTransaction().commit();
}
Also make sure that hibernate.cfg.xml is in src/main/resources folder.
Solution 5
For hibernate 4.x you need to use org.hibernate.context.internal.ThreadLocalSessionContext instead of org.hibernate.context.ThreadLocalSessionContext .
However, you can just set the property to thread and not Thread .
<property name="hibernate.current_session_context_class">thread</property>
Kaliyug Antagonist
I am a simple guy who believes knowledge can never be the destination - it's an eternal journey !
Updated on July 09, 2022Comments
-
Kaliyug Antagonist almost 2 years
I'm stuck due to some silly mistake somewhere but not able to figure out !
Hibernate 4.2.6
I have referred to this question, asked several times previously, for example here
hibernate.cfg.xml
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-configuration SYSTEM "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.bytecode.use_reflection_optimizer">false</property> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.password">root</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property> <property name="show_sql">true</property> <property name="hibernate.current_session_context_class">org.hibernate.context.ThreadLocalSessionContext</property> <mapping resource="Event.hbm.xml"></mapping> </session-factory> </hibernate-configuration>
Event.hbm.xml
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.db.pojo.Event" table="Events"> <id name="id" column="Id"> <generator class="native"></generator> </id> <property name="title" column="Title"></property> <property name="date" column="Date" type="timestamp"></property> </class> </hibernate-mapping>
HibernateUtil
package com.db.util; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import org.hibernate.service.ServiceRegistryBuilder; public class HibernateUtil { private static final SessionFactory sessionFactory = buildSessionFactory(); private static SessionFactory buildSessionFactory() { // TODO Auto-generated method stub Configuration configuration = new Configuration(); configuration.configure("hibernate.cfg.xml"); ServiceRegistryBuilder serviceRegistryBuilder = new ServiceRegistryBuilder() .applySettings(configuration.getProperties()); return configuration.buildSessionFactory(serviceRegistryBuilder .buildServiceRegistry()); } public static SessionFactory getSessionFactory() { return sessionFactory; } }
Whenever I try this :
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
I get :
Exception in thread "main" org.hibernate.HibernateException: No CurrentSessionContext configured! at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:988) at test.EventManager.createAndStoreEvent(EventManager.java:27) at test.EventManager.main(EventManager.java:17)
Edited after executing on my personal laptop
- The code runs fine if I use 'thread' in the cfg file
- If I use 'ThreadLocalSessionContext', I get the below exception(Please note that even the output on the console is now enormous than the previous run) - is it that the class 'ThreadLocalSessionContext' was not present in the 3.0 version(I tried to find in the API but the site doesn't have the older API )
Now I'm suspicious about the dtd - are Hibernate 3.0 jars being referred somewhere while the local ones are ignored, even though SYSTEM is specified? I got the previous exception(no current context) while executing the code on a machine with restricted Internet access. But when I executed the same on my personal laptop :
eventDesciption : Team eventDate : 2013-12-12 Nov 28, 2013 8:55:59 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit> INFO: HCANN000001: Hibernate Commons Annotations {4.0.4.Final} Nov 28, 2013 8:55:59 PM org.hibernate.Version logVersion INFO: HHH000412: Hibernate Core {4.2.6.Final} Nov 28, 2013 8:55:59 PM org.hibernate.cfg.Environment <clinit> INFO: HHH000206: hibernate.properties not found Nov 28, 2013 8:55:59 PM org.hibernate.cfg.Environment buildBytecodeProvider INFO: HHH000021: Bytecode provider name : javassist Nov 28, 2013 8:55:59 PM org.hibernate.cfg.Configuration configure INFO: HHH000043: Configuring from resource: hibernate.cfg.xml Nov 28, 2013 8:55:59 PM org.hibernate.cfg.Configuration getConfigurationInputStream INFO: HHH000040: Configuration resource: hibernate.cfg.xml Nov 28, 2013 8:55:59 PM org.hibernate.cfg.Configuration addResource INFO: HHH000221: Reading mappings from resource: resources/Event.hbm.xml Nov 28, 2013 8:55:59 PM org.hibernate.cfg.Configuration doConfigure INFO: HHH000041: Configured SessionFactory: null Nov 28, 2013 8:55:59 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure INFO: HHH000402: Using Hibernate built-in connection pool (not for production use!) Nov 28, 2013 8:55:59 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure INFO: HHH000115: Hibernate connection pool size: 20 Nov 28, 2013 8:55:59 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure INFO: HHH000006: Autocommit mode: false Nov 28, 2013 8:55:59 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure INFO: HHH000401: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/test] Nov 28, 2013 8:55:59 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure INFO: HHH000046: Connection properties: {user=root, password=****} Nov 28, 2013 8:56:00 PM org.hibernate.dialect.Dialect <init> INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect Nov 28, 2013 8:56:00 PM org.hibernate.engine.jdbc.internal.LobCreatorBuilder useContextualLobCreation INFO: HHH000423: Disabling contextual LOB creation as JDBC driver reported JDBC version [3] less than 4 Nov 28, 2013 8:56:00 PM org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService INFO: HHH000399: Using default transaction strategy (direct JDBC transactions) Nov 28, 2013 8:56:00 PM org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init> INFO: HHH000397: Using ASTQueryTranslatorFactory Nov 28, 2013 8:56:00 PM org.hibernate.internal.SessionFactoryImpl buildCurrentSessionContext ERROR: HHH000302: Unable to construct current session context [org.hibernate.context.ThreadLocalSessionContext] org.hibernate.service.classloading.spi.ClassLoadingException: Unable to load class [org.hibernate.context.ThreadLocalSessionContext] at org.hibernate.service.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:152) at org.hibernate.internal.SessionFactoryImpl.buildCurrentSessionContext(SessionFactoryImpl.java:1544) at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:516) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1790) at com.db.util.HibernateUtil.buildSessionFactory(HibernateUtil.java:19) at com.db.util.HibernateUtil.<clinit>(HibernateUtil.java:9) at test.EventManager.createAndStoreEvent(EventManager.java:27) at test.EventManager.main(EventManager.java:17) Caused by: java.lang.ClassNotFoundException: Could not load requested class : org.hibernate.context.ThreadLocalSessionContext at org.hibernate.service.classloading.internal.ClassLoaderServiceImpl$AggregatedClassLoader.findClass(ClassLoaderServiceImpl.java:319) at java.lang.ClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Unknown Source) at org.hibernate.service.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:149) ... 7 more Exception in thread "main" org.hibernate.HibernateException: No CurrentSessionContext configured! at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:988) at test.EventManager.createAndStoreEvent(EventManager.java:27) at test.EventManager.main(EventManager.java:17)
-
Kaliyug Antagonist over 10 yearsBefore posting this question, I had referred to similar posts and had tried both ways in vain, issue is something else !
-
RAS over 10 years@KaliyugAntagonist what exception do you get when you write only
thread
instead oforg.hibernate.context.ThreadLocalSessionContext
? -
Kaliyug Antagonist over 10 yearsI have edited my post - I'm getting different behavior on different machines.
-
Solostaran14 over 9 yearsIt is stated in the Hibernate tutorial that "thread" is a value to consider for design and prototype but not for production. So I went to the hibernate library (v4.3) and found that the
org.hibernate.context.ThreadLocalSessionContext
class does not exists. Instead there is aorg.hibernate.context.internal.ThreadLocalSessionContext
class that worked for me. -
RAS over 9 years@Solostaran14, thanks for pointing that out. Updated the answer.
-
acsadam0404 over 9 yearsThis solves the issue but raises another. If used that way the session must be manually closed to avoid too many open sessions.
-
mikebertiean over 8 yearsthis still has problems. getting the error org.hibernate.HibernateException: No CurrentSessionContext configured!
-
Marco over 8 yearsOk, don't know what's happening since I can't see the full stack trace but maybe this post will help you: stackoverflow.com/questions/7281045/…
-
Admin over 2 yearsAs it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.