-->
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.  [ 5 posts ] 
Author Message
 Post subject: Multi-threading and stale objects
PostPosted: Fri Nov 28, 2003 6:31 am 
Newbie

Joined: Fri Nov 28, 2003 6:20 am
Posts: 3
This is a general question regarding Hibernate in a multi-threaded environment. I am writing a server that uses multiple threads to service an incoming queue of messages. Each thread attempts to write the results of it's processing to a common database through a Hibernate interface. So far so good. If I light off just one thread everhthing is fine; if I light off more than one I get stale object exceptions. I've looked at the adivce in the documentation and each thread opens a new connection to the database.

My question is simple - are stale object excepitions a feature of muti-threaded access or is there a general approach (locking?) that makes them go away? I can deal with the exceptions but I'd like not to see them in the first place.

Thanx,
Allan Clearwaters, The Object Forge


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 28, 2003 6:50 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
You say connection, but I hope you mean Session, right ?

If you get StaleObjectException, then you are having a good old transaction conflict - two users have written or deleted the same data!

From the javadoc:

"Thrown when a version number or timestamp check failed, indicating that the
* <tt>Session</tt> contained stale data (when using long transactions
* with versioning). Also occurs if we try delete or update a row that does
* not exist.<br>
* <br>
* Note that this exception often indicates that the user failed to specify the
* correct <tt>unsaved-value</tt> strategy for a class!"

So - is it the case for your app ? Are you having conflicting writes/deletes or are you specifying an incorrect value for unsaved-value ?

And wether you should start to use locking is all up to your application....

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 28, 2003 7:39 am 
Newbie

Joined: Fri Nov 28, 2003 6:20 am
Posts: 3
Max,

I do mean session as well but as I read the documentation it says "... never create more than one concurrent session or transaction per database connection"; it take this to mean that each thread should have it's own SessionFactory despite the assretion that SessionFactories are thread safe and intended to be shared (is this correct?).

It is certainly the case that individual threads may well modify the same object. I don't belived this is a versioning problem.

Any more insight would be most useful.

Thanx,
Al


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 28, 2003 8:38 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
clearwa wrote:
"... never create more than one concurrent session or transaction per database connection"; it take this to mean that each thread should have it's own SessionFactory despite the assretion that SessionFactories are thread safe and intended to be shared (is this correct?).


No use 1 SessionFactory for all your threads and sf.openSession() for each thread. What is forbidden is to share the same connection object between several sessions
Code:
Connection conn;
Session s1 = sf.openSession(conn);
Session s2 = sf.openSession(conn);
// this is forbidden


Since session object isn't thread safe, the don't share sessions between threads.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 28, 2003 8:46 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
No - SessionFactory IS thread safe....but Session is not.
Use only a session in one thread at a time.

And regarding versioning - then it gotta be something about that since that is the only reason why you would get StaleObjectException!

When two threads can write to the same row in the database at the same time, then you should have some mechanisms to avoid inconsistency!
What do you do to prevent that ? Use optimistic locking and then report failure to the user or you use pessismistic locking and lock people out if necessary....basic database stuff ;)

_________________
Max
Don't forget to rate


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.