Win a copy of Android Programming: The Big Nerd Ranch Guide this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Hibernate session thread safety issue  RSS feed

 
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.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!