在没有版本注释或OptimisticLocking策略的情况下获取ObjectOptimisticLockingFailureException

时间:2019-03-01 16:00:41

标签: java spring postgresql jpa spring-data

我正在获得乐观的锁定异常(如下所述),但是奇怪的是我们还没有使用@version注释或OptimisticLocking指定任何实体,所以只是想知道什么会导致该异常?我们正在使用JPA,休眠,spring数据和spring。数据库是PostgreSQL。

System exception occurred while processing request, ERROR_CODE: a18d5739 org.springframework.orm.ObjectOptimisticLockingFailureException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1; nested exception is org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:301)
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:225)
at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:521)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:761)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:730)
at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:485)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:291)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)

1 个答案:

答案 0 :(得分:0)

这与乐观锁无关。在通过ID删除/更新根本不存在的记录时引发此异常。因此,请检查数据库中是否确实存在您要更新/删除的记录。

但是,要更好地了解导致问题的原因,您可以:

1)将show_sql设置为true 2)将Spring和Hibernate的日志级别设置为DEBUG

这将帮助您了解问题并解决。