• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

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

 
Ranch Hand
Posts: 137
Hibernate Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author and cow tipper
Posts: 5009
1
Hibernate Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?

 
reply
    Bookmark Topic Watch Topic
  • New Topic