Win a copy of The Business Blockchain this week in the Cloud forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

help on releasing hibernate session

 
Casper Serrano
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

Im really new to hibernate, its been a week now on lingering through the codes that im handling now. im just using a lower version, i think its 1.1.x

right now im cleaning the codes as there are some hibernate sessions which are not closed
once a query or update is called. My task was to clear them up which consumes memory after using them.

Here is a sample of the current code:

public java.util.List findBook()
{
return findBook(getSession());
}



the solution i though off was

public java.util.List findBook()
{
Session sess = null;
try
{
sess = getSession();
return findBook(sess);
}
finally
{
SessionFactoryUtils.closeSessionIfNecessary(sess, getSessionFactory());
}
}


I like to ask any comments if what ive done is correct. or should do something else.
many thanks.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does SessionFactoryUtils.closeSessionIfNecessary(sess, getSessionFactory()); do? I can't remember the API for v1.1, but can't you just call session.close()?
 
Casper Serrano
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is the link that im reading..

http://static.springsource.org/spring/docs/1.1.x/api/org/springframework/orm/hibernate/SessionFactoryUtils.html

from what i understand.

when i use getSession().. i obtain an instance of the Session from SessionFactory.

so i just use SessionFactoryUtils then call closeSessionIfNecessary() method.

thanks.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you using such a crazy-old Spring?! That's circa... 2004?
 
Casper Serrano
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
my apologies.

this is the version my company is using.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic