Hibernate StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect)
Environment
- Red Hat JBoss Enterprise Application Platform (EAP)
- 5
- 6
- Hibernate
- Hibernate 3 / JBossCache
- Hibernate 4 / Infinispan
Issue
- Clustering has been configured for multiple server JVMs
- Level 2 caching is enabled for persistence units deployed in each server
- When entities are concurrently updated in multiple server JVMs, some JVMs encounter repeated
StaleObjectStateExceptionfailures for specific entities-
Affected entities remain in the level 2 cache until they are explicitly or implicitly (e.g. due to timeout) evicted
org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [com.abc.domain.entitity.ObjectA#123456] at org.hibernate.persister.entity.AbstractEntityPersister.check(AbstractEntityPersister.java:1782) at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2425) at org.hibernate.persister.entity.AbstractEntityPersister.updateOrInsert(AbstractEntityPersister.java:2325) at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2625) at org.hibernate.action.EntityUpdateAction.execute(EntityUpdateAction.java:115) at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:278) at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:262) at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:167) at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321) at org.hibernate.event.def.DefaultAutoFlushEventListener.onAutoFlush(DefaultAutoFlushEventListener.java:64) at org.hibernate.impl.SessionImpl.autoFlushIfRequired(SessionImpl.java:999) at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1185) at org.hibernate.impl.QueryImpl.list(QueryImpl.java:102) at org.hibernate.ejb.QueryImpl.getSingleResult(QueryImpl.java:116) ...
-
Resolution
-
For EAP 6, this issue1 is resolvable in This content is not included.EAP 6.4 cumulative patch (CP) 7 and later releases using the
infinispan.invalidation.on_commitsystem property. -
For EAP 5 a This content is not included.custom entity persister may be used to automatically evict entities for which
StaleObjectStateExceptionis raised-
Subsequent access attempts will result in a reload of the latest persisted state of these entities
-
The custom persister may be associated with an entity using
@Entity ... @Persister(impl=org.hibernate.util.AutoStaleObjectEvictingPersister.class) public class EventEntity { ... }
-
-
EAP 7.0/Hibernate 5.0/Infinispan 8.1 and later are Content from hibernate.atlassian.net is not included.not susceptible to this vulnerability.
-
See also Stale Hibernate entities are found in some clustered nodes in EAP
Diagnostic Steps
- Enable Hibernate logging, including both the SQL and values.
- Check whether multiple threads in the JVM are updating the same entity concurrently
- Check whether multiple nodes/JVMs may be updating the same entity concurrently
- Enable transaction logging for EAP 5 and EAP 6
- See Monitoring clustered level 2 cache invalidation for Hibernate / Infinispan in EAP 6
- See Monitoring clustered level 2 cache invalidation for Hibernate / JBossCache in EAP 5
This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.