-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Best practice in a multi-threaded application
PostPosted: Thu Mar 04, 2010 5:01 pm 
Newbie

Joined: Sat Jun 04, 2005 2:35 pm
Posts: 7
We have a stand-alone application that relies heavily on threading. We currently have the application built in such a way that each thread can potentially have its own hibernate session and there is no requirement that persisted objects get evicted from the session. The main problem that we are having is that in many cases the same object will end up in multiple threads (sessions) at the same time. In general, this is not a problem unless the object has a collection in which case we get an "illegal attempt to associate a collection with two open session." We also get a fair number of NonUniqueObjectException due to some internal cache behavior but that one we can probably deal with.

My main question here is: What is the best practice for this kind of application? Should I have one session for all threads and force synchronization? Should I allow the threads to do their own thing but ensure that the sessions are short lived in order to minimize this problem? Or is there something else that we should look at (like second level caching)? And how do servers do it?


Top
 Profile  
 
 Post subject: Re: Best practice in a multi-threaded application
PostPosted: Fri Mar 05, 2010 3:41 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
From my experience it should work if you strict always use a separate session for each thread,
and by not sharing persistent objects (entities, collections) with other threads.
(or by sharing them just for read-only purpose)


Top
 Profile  
 
 Post subject: Re: Best practice in a multi-threaded application
PostPosted: Fri Mar 05, 2010 6:48 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
agree with pb00067,
you should make sure that objects are not managed when you share them; use .clear() on the Session or better close the session.
The other thread receiving a reference can eventually re-attach it; best way is probabily using the second-level cache: enable the cache and reload the data from hibernate.
An alternative is to use lock(LockMode.NONE) which will re-attach a persistent object to your session (assuming it was detached from others).
Generally make sure you never have an entity managed by more than one session.

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject: Re: Best practice in a multi-threaded application
PostPosted: Wed Feb 16, 2011 9:28 am 
Newbie

Joined: Sat Jun 04, 2005 2:35 pm
Posts: 7
I just want to report back that the 2nd level cache really did the trick. The basic approach is to clear the sesssion after every access and rely upon the 2nd level cache to keep things in memory. We pay a small penalty for the hydration step, but it is well worth it.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.