乐观锁定失败;使用Spring Data的CrudRepository

时间:2016-06-27 23:06:32

标签: mysql hibernate spring-data optimistic-locking

我上周刚学会了Hibernate的乐观锁定,并将它介绍给我用Spring JPA和MySQL DB编写的应用程序。

我的实体看起来像这样,刚刚添加了Version注释,

@Entity    
public class Instance { 
    ...

    @javax.persistence.Version
    private Date updateTime;

    ...

    pubic Instance() {
        this.updateTime = new Date();
    }

}

我有org.springframework.data.repository.CrudRepository用于保存数据。虽然我验证了更新现有记录的工作正常,但问题是它在持久化新对象时会抛出异常,

inst = new Inst(); 
instanceRepo.save(inst);

抛出的异常是

org.springframework.orm.ObjectOptimisticLockingFailureException: Object of class [Instance] with identifier [a5deddb9-d76c-433f-8b0d-e50cbf8f601e]: optimistic locking failed; nested exception is org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect) : 
    at org.hibernate.persister.entity.AbstractEntityPersister.check(AbstractEntityPersister.java:2541)
    at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:3285)
    at org.hibernate.persister.entity.AbstractEntityPersister.updateOrInsert(AbstractEntityPersister.java:3183)
    at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:3525)
    at org.hibernate.action.internal.EntityUpdateAction.execute(EntityUpdateAction.java:159)
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:465)
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:351)
    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:1258)
    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)
    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)
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:131)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:208)
    at com.sun.proxy.$Proxy139.save(Unknown Source)

这只是第一次持久性调用,我不认为有多个线程试图更新此记录,所以我很困惑为什么抛出此异常。

此外,我试图追溯到底层代码,这里有一些调查结果可能会有所帮助,

  1. 当调用下面粘贴的基础org.springframework.data.jpa.repository.support.SimpleJpaRepository#save(S)时,代码将转到合并逻辑,即使这是一条新记录,

    public <S extends T> S save(S entity) { if (entityInformation.isNew(entity)) { em.persist(entity); return entity; } else { return em.merge(entity); } }

  2. 尝试提交事务并刷新数据库时发生异常。

2 个答案:

答案 0 :(得分:1)

JPA规范说 The following types are supported for version properties: int, Integer, short, Short, long, Long, java.sql.Timestamp.

使用java.util.Date类型时可能会出现问题。

答案 1 :(得分:1)

如果你的JPA提供者支持,那么@Version注释也可以用于日期字段。但你必须使用@Temporal注释映射日期字段,如下所示

@javax.persistence.Version
@Temporal(TemporalType.TIMESTAMP)
private Date updateTime;

有关详细信息,请访问以下链接

http://www.byteslounge.com/tutorials/jpa-entity-versioning-version-and-optimistic-locking