• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Is Hibernate Session object is thread-safe ?

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is Hibernate Session object is thread-safe ?
If not, how to insure data integrity between multiple request?
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 532
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic