aspose file tools*
The moose likes Object Relational Mapping and the fly likes Is Hibernate Session object is thread-safe ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Is Hibernate Session object is thread-safe ?" Watch "Is Hibernate Session object is thread-safe ?" New topic
Author

Is Hibernate Session object is thread-safe ?

Xavier George
Ranch Hand

Joined: Jul 29, 2005
Posts: 39
Is Hibernate Session object is thread-safe ?
If not, how to insure data integrity between multiple request?
Yuval Goldstein
Greenhorn

Joined: Dec 27, 2006
Posts: 18
The Session object should only by used by a single Thread.
You usually (99% of the time) open/get a session with each user request and close is when its over.

This is from the javadocs(http://www.hibernate.org/hib_docs/v3/api/org/hibernate/Session.html):

<quote>
It is not intended that implementors be threadsafe. Instead each thread/transaction should obtain its own instance from a SessionFactory.
</quote>

Cheers!
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

Originally posted by Xavier George:
Is Hibernate Session object is thread-safe ?
If not, how to insure data integrity between multiple request?


There are design patterns for Hibernate to handle these types of Conversations. On the hibernate.org website on their wiki pages describes how to do this. Also in the Java Persistence with Hibernate book, there is a good chapter on conversations.

Good Luck

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Hanna Habashy
Ranch Hand

Joined: Aug 20, 2003
Posts: 532
If you put this statement in your configuration file, the session will be bound to the thresd
<property name="hibernate.current_session_context_class">thread</property>

In your Java class you call getCurrentSession() from the SessionFactory to obtain a session object that is bound to this thread.
[ April 18, 2007: Message edited by: Hanna Habashy ]

SCJD 1.4<br />SCJP 1.4<br />-----------------------------------<br />"With regard to excellence, it is not enough to know, but we must try to have and use it.<br />" Aristotle
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is Hibernate Session object is thread-safe ?