hibernate/hibernate-orm · error · StaleObjectStateException
Row was already updated or deleted by another transaction
Error message
Row was already updated or deleted by another transaction
What it means
Error "Row was already updated or deleted by another transaction" thrown in hibernate/hibernate-orm.
Source
Thrown at hibernate-core/src/main/java/org/hibernate/event/internal/DefaultFlushEntityEventListener.java:758
);
}
}
@Nullable
private static Object[] getDatabaseSnapshot(
@Nonnull EntityPersister persister,
@Nonnull Object id,
@Nonnull SessionImplementor session) {
final var persistenceContext = session.getPersistenceContextInternal();
if ( persister.isSelectBeforeUpdateRequired() ) {
final Object[] snapshot = persistenceContext.getDatabaseSnapshot( id, persister );
if ( snapshot == null ) {
//do we even really need this? the update will fail anyway
final var statistics = session.getFactory().getStatistics();
if ( statistics.isStatisticsEnabled() ) {
statistics.optimisticFailure( persister.getEntityName() );
}
throw new StaleObjectStateException( persister.getEntityName(), id );
}
return snapshot;
}
// TODO: optimize away this lookup for entities w/o unsaved-value="undefined"
return persistenceContext.getCachedDatabaseSnapshot( session.generateEntityKey( id, persister ) );
}
}
View on GitHub (pinned to fad1729dce)
Solutions
- Use optimistic locking (@Version) and catch OptimisticLockException to retry the operation with fresh data.
- Reload the entity from the database before modifying it if another transaction may have changed it.
- Shorten transaction scope to reduce the window for concurrent modifications.
When it happens
Trigger: Thrown when a flush detects that the database row was concurrently modified or removed by another transaction (stale optimistic state).
Common situations: Typical situations: concurrent transactions updating the same row; long-running transactions with optimistic locking; reusing stale entities across transactions.
AI-assisted analysis of hibernate/hibernate-orm@fad1729dce (2026-08-22).
Data as JSON: /api/errors/799c03a7d18d4b44.
Report an issue: GitHub.