• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Difference between Session.openSession() & Session.getCurrentSession()

 
Sidharth Pallai
Ranch Hand
Posts: 134
Hibernate Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone explain the actual difference between Session.getCurrentSession() and Session.openSession().When getCurrentSession() is invoked it fetch a new session everytime but can it span across multiple transactions.I mean can such Session will handle more than one transactions without closing after each committed transaction.
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Indeed!

What is the difference between the Hibernate Session openSession and getSession methods?

getSession gives you a Hibernate session, and associates that session with the current thread. Then, if you call getSession again, you get the session that was originally created. This is good.

Some people don't like binding the Hibernate Session to the current thread, and they don't like the ease of getting the current Session back with getSession. Instead, these people use openSession, which creates a new Session and asks the DEVELOPER to manage where the session goes. I gues you could put it in a list or a cache, or whatever, but YOU must manage it, as though you were creating your own database connection pool or something.

getSession is usually sufficient. openSession provides and facilitates a greater level of management of where the session is stored and managed. It's certainly an advanced option, but one that does indeed fit the need of very clever developers who are doing some nifty things with the session.

Some people who are much better looking than me, not to mention being much more intelligent, discussed just this topic in the following JavaRanch thread:

What is the difference between the Hibernate Session openSession and getSession methods?

-Cameron McKenzie
[ July 03, 2008: Message edited by: Cameron Wallace McKenzie ]
 
Rajkumar Kathiresan
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. what would happen if I open the session with "Session.getCurrentSession()" and close the same session with "session.close()" at the end of my method? Whether it will throw any error?
2. If I again I try to open the the session with "Session.getCurrentSession()" it will throw any error?
 
Nishant Verma
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually the hibernate uses the session pattern... so it means if the current session does not exists then it provide a new session. But getSession() will never fail.

There is very interesting explanation on the previous post by Cameron. Very well said. And yes you don't have to be better looking to code like this.....hahaha

Rajkumar Kathiresan wrote:1. what would happen if I open the session with "Session.getCurrentSession()" and close the same session with "session.close()" at the end of my method? Whether it will throw any error?
2. If I again I try to open the the session with "Session.getCurrentSession()" it will throw any error?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic