-->
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.  [ 3 posts ] 
Author Message
 Post subject: URGENT : StaleStateException: Batch update error
PostPosted: Wed Jul 30, 2008 2:08 am 
Beginner
Beginner

Joined: Thu Oct 25, 2007 1:21 pm
Posts: 29
Hi,

I need to create a new record in a table when there is no specific record exist in it. But first time where there is no record exist in the table, there could be a possibility that multiple threads running simultaneously could create multiple records at the same time. In order to maintain uniqueness, i am also doing a check if there are multiple records created for the same criteria, then i am updating all the records except the first latest one with an inactive state.

It works fine with one thread. The issue comes when there are multiple threads involved and doing any record updations. Any ideas? Is this the problem in the hibernate code itself. Otherwise, why it is not able to handle these concurrency issues properly. Did i miss anything here? Thanks in advance for any help.


org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1
at org.hibernate.jdbc.Expectations$BasicExpectation.checkBatched(Expectations.java:61)
at org.hibernate.jdbc.Expectations$BasicExpectation.verifyOutcome(Expectations.java:46)
at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:24)
at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2338)
at org.hibernate.persister.entity.AbstractEntityPersister.updateOrInsert(AbstractEntityPersister.java:2242)
at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2542)
at org.hibernate.action.EntityUpdateAction.execute(EntityUpdateAction.java:92)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:248)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:232)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 06, 2008 10:38 am 
Newbie

Joined: Wed Jul 30, 2008 8:00 am
Posts: 3
I was having similar problem. First of all you need to properly handle StaleStateException because it is going to happen a lot if your threads modify the same data.

When more than 2 threads check at the same if a record exist, they will get the same answer. If it does not exist they will both add the new entry. If you want to make sure that 2 thread cannot add the same data at the same time, you have to add in your model a "unique constrain" on some attributes of the class you want to persist. With a unique constrains, when a thread commit a transaction it will fail if another thread has already done it for the same unique identifier. This is another exception that needs to be taken care of.

Now to prevent that from happening, what you can do is carefully dispatch to threads information to be persisted in way that will avoid those conflicts.

When I get a StaleStateException or Constrain exception, what I do is rollback, clear and close the session, create a new Session and start over. With a well design dispatcher, it almost never happen.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 06, 2008 11:39 am 
Beginner
Beginner

Joined: Thu Oct 25, 2007 1:21 pm
Posts: 29
Intersting. let me see if that would solve my problem or not. great. thanks for the help.

thanks


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