-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: openSession() vs getCurrentSession() in same Thread
PostPosted: Sun Jun 30, 2013 12:06 am 
Newbie

Joined: Sat Jun 29, 2013 11:56 pm
Posts: 1
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


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.