javax.persistence.OptimisticLockException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect)
Issue
- Two processes are running parallel but one process, with a easy query, wait till the first process is finished.
- The first log message is visible and the second not. Sometimes it takes one minute till the query execute.
log.info("test get start");
Query query = unitLoadService.getEntityManager().createQuery("select p from employee p where p.name = :name");
query.setParameter("name", name);
List<Employee> employees = query.getResultList();
log.info("test get finish");
- We don't lock enities. Change to a native query don't help.
- Database is sql server 2012.
- We use option read committed snapshot in the DB.
- We use hibernate
<property name="hibernate.connection.isolation" value="4096" />
- We got the below exception:
Caused by: javax.persistence.OptimisticLockException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect)
Caused by: org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect):
- OptimisticLockException in Hibernate , we are repeatedly getting an
javax.persistence.OptimisticLockExceptionwhile doing an update/delete to tables. We tried enabling the spy option as well as debugged through the code. What I discovered is that it fails at the following point in class in ResultSetReturnImpl-
public int executeUpdate(PreparedStatement statement) {
try {
jdbcCoordinator.getTransactionCoordinator().getTransactionContext().startStatementExecution();
return statement.executeUpdate();
}
catch (SQLException e) {
throw sqlExceptionHelper.convert( e, "could not execute statement" );
}
finally {
jdbcCoordinator.getTransactionCoordinator().getTransactionContext().endStatementExecution();
}
}
- The executeUpdate in above case always returns 0 (and no exceptions) even though the same SQL run on SQL Server 2014 directly updates one row. The PreparedStatement implementation is coming from
ironjacamar-jdbc-1.0.28.Final-redhat-1.jar.
Environment
- Red Hat JBoss Enterprise Application Platform(EAP)
- 6.3.3
- Hibernate 4.x
Subscriber exclusive content
A Red Hat subscription provides unlimited access to our knowledgebase of over 48,000 articles and solutions.
Welcome! Check out the Getting Started with Red Hat page for quick tours and guides for common tasks.
