• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is Hibernate Session object is thread-safe ?

 
Xavier George
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is Hibernate Session object is thread-safe ?
If not, how to insure data integrity between multiple request?
 
Yuval Goldstein
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Hanna Habashy
Ranch Hand
Posts: 532
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic