Hi All,
I'm learning Hibernate and I have a very basic question on Hibernate Session concept. As you know, there are two methods openSession() and getCurrentSession() in SessionFactory and as far as I understand, when we use current_session_context_class = thread, then a session created is bound to the Thread under execution and the thread that created that session. So, if a session is created using openSession() and then call getCurrentSession(), it should return the same session instance.
Code:
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">org.h2.Driver</property>
<property name="hibernate.connection.password">admin</property>
<property name="hibernate.connection.url">jdbc:h2:tcp://localhost/learn</property>
<property name="hibernate.connection.username">sa</property>
<property name="hibernate.default_schema">learn</property>
<property name="hibernate.dialect">org.hibernate.dialect.H2Dialect</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.current_session_context_class">thread</property>
<property name="hibernate.hbm2ddl.auto">update</property>
</session-factory>
</hibernate-configuration>
package edu.learning.orm;
import org.junit.Assert;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
/**
* Hello world!
*
*/
public class HibernateUtils
{
private static SessionFactory sFactory=buildSessionFactory();
private static SessionFactory buildSessionFactory() throws HibernateException {
Configuration configuration = new Configuration().configure();
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
return configuration.buildSessionFactory(serviceRegistry);
}
private static Session openNewSession(){
return sFactory.openSession();
}
private static Session openCurrentSession(){
return sFactory.getCurrentSession();
}
public static void main( String[] args )
{
Session session1 = HibernateUtils.openNewSession();
Session session2 = HibernateUtils.openNewSession();
Session session3 = HibernateUtils.openCurrentSession();
Assert.assertNotNull("Session should NOT null",session1);
Assert.assertNotNull("Session should NOT null",session2);
Assert.assertNotNull("Session should NOT null",session3);
Assert.assertNotEquals("Both sessions should NOT be different", session1, session2);
Assert.assertNotEquals("Both sessions should NOT be different", session2, session3);
Assert.assertEquals("Both sessions should be different", session1, session3);
Assert.assertEquals("Both sessions should be different", session2, session3);
}
}
I see the assertion failing at Assert.assertEquals(session1,session3);
Since the whole execution is under same single main thread, should not session1 and session3 be equal??
Also, I tried to call getCurrentSession() directly as the only call and I still see a Session being returned. Does getCurrentSession() creates a new session, if none existing bound to Thread.
Thanks