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
posted
0
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.
<quote> It is not intended that implementors be threadsafe. Instead each thread/transaction should obtain its own instance from a SessionFactory. </quote>
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.
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