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: Need help with thread related problem.
PostPosted: Tue Apr 06, 2010 7:25 pm 
Newbie

Joined: Tue Apr 06, 2010 6:47 pm
Posts: 3
I have the following scenario:
Thread 1 creates 'Job' objects, saves them to the db and puts them in a queue to process.
Thread 2 grabs the objects from the queue processes them and then saves them to the db with a 'completed' status.

The problem i'm having is that every once in a while i get
Code:
javax.persistence.OptimisticLockException
...
Could not synchronize database state with session
org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect)


This happens every once in a while at random times, and so far i have not been able to reproduce it in a controlled environment.
I can tell you that the "unsaved-value mapping was incorrect" is not the reason for this. The mappings are fine and the objects are actually saved fine despite the exception. So right now i just ignore the exception and keep going.

I should mention that the 'job processing' is actually very fast (a few millis), so Thread 2 will re-save the job very shortly after Thread 1 first saves it.

Both threads also flush the session after saving the job. My initial thought was that Thread 2 was re-saving the job before Thread 1 had finished saving it, but shouldn't flushing the session prevent this?

I am using the HibernateEntityManager interface to hibernate and version 3.2.6

This obviously has to do with the threads stepping on each-other's toes, so my question is what is the best way to handle this type of scenario?

Thanks


Top
 Profile  
 
 Post subject: Re: Need help with thread related problem.
PostPosted: Wed Apr 07, 2010 5:32 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Do both threads work with the same session ? (it is generally not recommended to share sessions over different threads)
Or do you re-attach/merge the objects from the queue in thread2 to the thread2-own-session before saving them?


Top
 Profile  
 
 Post subject: Re: Need help with thread related problem.
PostPosted: Wed Apr 07, 2010 11:22 am 
Newbie

Joined: Tue Apr 06, 2010 6:47 pm
Posts: 3
It doesn't look like they are. I'm using the HibernateEntityManager interface to hibernate so it's a little difficult to tell, but a quick debug shows that the threads are using different sessions.

I don't really use the hibernate session object in my code, except when saving because saveOrUpdate is a lot easier then trying to do a merge or persist using the JPA interface. In that case i just get the session from the HibernateEntityManager injected in my session bean.

Perhaps this is not the best way? Would manually evicting the saved object from the session make any difference?
I'm sure someone has dealt with similar situations. What is the recommended/best way to deal with something like this?


Top
 Profile  
 
 Post subject: Re: Need help with thread related problem.
PostPosted: Wed Apr 07, 2010 6:28 pm 
Newbie

Joined: Tue Apr 06, 2010 6:47 pm
Posts: 3
EDIT: Deleted my explanation because it was wrong


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.