How to avoid StaleObjectStateException when transaction updates thousands of entities?

Posted by ThinkFloyd on Stack Overflow See other posts from Stack Overflow or by ThinkFloyd
Published on 2012-07-04T15:12:35Z Indexed on 2012/07/04 15:15 UTC
Read the original article Hit count: 187

Filed under:
|
|
|
|

We are using Hibernate 3.6.0.Final with JPA 2 and Spring 3.0.5 for a large scale enterprise application running on tomcat 7 and MySQL 5.5. Most of the transactions in application, lives for less than a second and update 5-10 entities but in some use cases we need to update more than 10-20K entities in single transaction, which takes few minutes and hence more than 70% of times such transaction fails with StaleObjectStateException because some of those entities got updated by some other transaction.

We generally maintain version column in all tables and in case of StaleObjectStateException we generally retry but since these longs transactions are anyways very long so if we keep on retrying then also I am not very sure that we'll be able to escape StaleObjectStateException.

Also lot of activities keep updating these entities in busy hours so we cannot go with pessimistic approach because it can potentially halt many activities in system.

Please suggest how to fix such long transaction issue because we cannot spawn thousands of independent and small transactions because we cannot afford messed up data in case of some failed & some successful transactions.

© Stack Overflow or respective owner

Related posts about java

Related posts about spring