Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate session thread safety issue

 
Saathvik Reddy
Ranch Hand
Posts: 228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a class with 10 methods. Each method gets an hibernate session to execute a query.
Instead of creating hibernate session in each method can i have a class variable for hibernate session and use it in all the methods? Is it going to cause any thread-safety issues?

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

Instead of creating hibernate session in each method can i have a class variable for hibernate session and use it in all the methods?

Yes. In fact, if you don't you loose the benefit of the first level cache.

Depending on your architecture there are a number of patterns on the hibernate site for using the session (open session in view, transaction scoped etc.) Have a read through them.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic