IntelliJ Java IDE
The moose likes Object Relational Mapping and the fly likes Need to clear the difference between hibernate opensession and currentsession method Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Object Relational Mapping
Reply Bookmark "Need to clear the difference between hibernate opensession and currentsession method" Watch "Need to clear the difference between hibernate opensession and currentsession method" New topic
Author

Need to clear the difference between hibernate opensession and currentsession method

Akshay Ajmani
Greenhorn

Joined: May 31, 2007
Posts: 2
<property name="current_session_context_class">thread</property> is used in assocation with currentSession()
This means that if you have a thread in which you create a Session s1 and you dont close s1 and you create another session s2
and then s2 wud point to s1

here in the following code snippet, value1 and value2 are same

but if s1 is closed then s2 would be different

for eg :

public static void main(String[] a)
{
Session session=HibernateUtil.currentSession();
session.beginTransaction();
Iterator itr=session.createCriteria(SmHostBean.class).list().iterator();
//session.getTransaction().commit(); <!-- session not closed-->
SmHostBean smHostBean=null;
while(itr.hasNext())
{
smHostBean=(SmHostBean)itr.next();
}

System.out.println("session in main thread=="+session); //value1
Sessionsession1=HibernateUtil.currentSession();
System.out.println("session in main thread=="+session1); //value2

}


but if in this example i had used opensession then i would have got different sessions i.e s1 and s2 would b diff when s1 is open
in openSession() session.close has to be done explicitly
in getcurrentsession() session is closed when transaction completes

Is my understanding of the two methods correct.

Kindly help
 
 
subject: Need to clear the difference between hibernate opensession and currentsession method
 
Threads others viewed
Problem with running my first Hibernate App
How many String Object are created in the following code ??
Confused while using currentsession method
Hibernate gives PSQLException for polymorphic query on "any"
Replacing a word in a text file
WebSphere development made easy
without the weight of IBM tools
http://www.myeclipseide.com