例如,我有一个类似这样的代码
@Transactional(propogation=PROPOGATION.REQUIRED)
public class codeForTest {
public void doSomethins(){
User user = userRepository.finduserById(userId);
updateUserDetails(user);
updateFewOtherDetails(user);
}
}
public class DifferentClass{
@Transactional(propogation=PROPOGATION.REQUIRED)
updateUserDetails() {
//UPDATES user object
}
}
在上述几种情况下,我得到了以下几点。我在本地计算机上没有收到错误。我的问题是,如果updateUserDetails()将提交事务,并且在执行updateFewOtherDetails()时,由于提交了事务,是否会引发异常?我怀疑这是否在造成问题,因为在哪里用@Transactional(propogation = PROPOGATION.REQUIRED)对其进行注释
org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1
at org.hibernate.jdbc.Expectations$BasicExpectation.checkBatched(Expectations.java:81)
at org.hibernate.jdbc.Expectations$BasicExpectation.verifyOutcome(Expectations.java:73)
at org.hibernate.engine.jdbc.batch.internal.BatchingBatch.checkRowCounts(BatchingBatch.java:151)
at org.hibernate.engine.jdbc.batch.internal.BatchingBatch.performExecution(BatchingBatch.java:128)
at org.hibernate.engine.jdbc.batch.internal.BatchingBatch.doExecuteBatch(BatchingBatch.java:111)
at org.hibernate.engine.jdbc.batch.internal.AbstractBatchImpl.execute(AbstractBatchImpl.java:163)
at org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl.executeBatch(JdbcCoordinatorImpl.java:226)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:482)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:349)
at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:350)
at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:56)
at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1222)
at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:425)
at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101)
at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:177)
at org.hibernate.jpa.internal.TransactionImpl.commit(TransactionImpl.java:77)
at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:517)
... 130 common frames omitted
Wrapped by: 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:297)
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:221)
at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:521)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:765)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:734)
at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:518)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:292)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)
at com.sun.proxy.$Proxy1125.createUserSession(Unknown Source)
答案 0 :(得分:0)
您正在将JPA(休眠)与optimistic locking一起使用。实际的例外:
org.springframework.orm.ObjectOptimisticLockingFailureException:批处理更新从更新[0]返回了意外的行数;实际行数:0;预期:1;嵌套的异常是org.hibernate.StaleStateException:批处理更新从更新[0]返回了意外的行数;实际行数:0;预期:1
表示当您的事务正在运行另一个事务时,更新了相同的行,Hibernate检测到它们的陈旧状态并回滚当前事务以避免数据一致性问题。乐观地锁定竞争的资源可能会发生这种情况,但这并不坏,因为由于回滚,数据库中的数据仍然保持一致。
有几种方法可以解决此问题:
ObjectOptimisticLockingFailureException
的逻辑,并希望下次交易不会受到竞争。答案 1 :(得分:0)
这两种方法一旦标记为@Transactional
,都将在单个事务的边界内执行。
user
对象已加载到不同事务的边界中或从缓存中获取,以防.finduserById()
使用该对象。
确保您也标记了@Transactional
第二个.updateFewOtherDetails()
方法。
通过传播REQUIRED
,直到调用者序列结束,事务才提交。