• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is open/close hibernate session in every business logic a good design?

 
Adrian Burlington
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We are building a struts application using Hibernate (so we are relatively newbies to Hibernate).
My question is with reference to the Hibernate session. Is it mandatory to close a session every time we open it? I mean, at the end of every action (or business logic to be precise) , should I 'manually close the session (close())? Isn't too 'expensive'? if yes, what is the alternative to keep the session open (if at all).

Example:



 
Vivek Kr Singh
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A little bit of Spring while using HibernateTemplate would resolve this situation for you.
 
Adrian Burlington
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you give more details please?
My initial question was if this issue is expensive or not? how would spring solve it?

Thanks again
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

My question is with reference to the Hibernate session. Is it mandatory to close a session every time we open it?

Yes. When you are finished with it you should close it. You need not explicitly close it yourself, there are frameworks that can do this for you (which is what Vivek is aluding to). You need not go the whole hog and introduce Spring to yourapplication; have a look at the open session in view pattern for example.

It is not an expensive operation to open and close sessions. It is usually problematic keeping them open too long as your cached session data grows more stale over time.
 
Vivek Kr Singh
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Paul. I was not aware of Open Session at all. Also found a recent thread which even points to use default hibernate Session factory rather than Templates (both using spring).
session-factory or templates
 
long meng
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi, i use the connection pool provided by websphere, i want to know if i call "session.close()", is that mean hibernate will give the connection back to the connection pool? if not, how to give it back, thanks!
 
Bosun Bello
Ranch Hand
Posts: 1511
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
session.close is supposed to close the connection and return it to the pool.

long meng wrote:hi, i use the connection pool provided by websphere, i want to know if i call "session.close()", is that mean hibernate will give the connection back to the connection pool? if not, how to give it back, thanks!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic