This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
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


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
 
Consider Paul's rocket mass heater.
 
subject: Hibernate session thread safety issue