wood burning stoves 2.0*
The moose likes Object Relational Mapping and the fly likes Hibernate session thread safety issue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate session thread safety issue" Watch "Hibernate session thread safety issue" New topic
Author

Hibernate session thread safety issue

Saathvik Reddy
Ranch Hand

Joined: Jun 03, 2005
Posts: 228
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

Joined: Apr 14, 2004
Posts: 10336


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.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Hibernate session thread safety issue
 
Similar Threads
Vector as a local variable
Can I use Hibernate in my RFID based attendance tracking application?
Is it a good idea to share a Session in hibernate ?
Static methods and thread safety
threading issue in servlet