Spring数据CrudRepository和悲观锁

rco*_*len 24 java spring spring-data spring-data-jpa

我正在使用

  • Spring Boot 1.4.2
  • Spring Data JPA 1.10.5
  • PostgreSQL 9.5数据库

我希望findOne在我的Spring Data存储库中有一个带有悲观锁的findOne方法,该方法与已经提供的方法分开.

根据这个答案,我写道:

public interface RegistrationRepository extends CrudRepository<Registration, Long> {
    @Lock(LockModeType.PESSIMISTIC_WRITE)
    @Query("select r from Registration r where r.id = ?1")
    Registration findOnePessimistic(Long id);
}
Run Code Online (Sandbox Code Playgroud)

这几乎可行.

不幸的是,这不会刷新实体管理器缓存中我的实体的先前实例.我有两个并发请求更新我的注册状态

  • 第二个等待第一个提交的事务
  • 第二个没有考虑第一个的变化.

因此破坏了行为.

任何线索为什么@Lock没有开箱即用刷新实体经理?

更新

这是请求的示例代码:

public interface RegistrationRepository extends CrudRepository<Registration, Long> {

    @Lock(LockModeType.PESSIMISTIC_WRITE)
    @Query("select r from registration_table r where r.id = ?1")
    Registration findOnePessimistic(Long id);

}

public void RegistrationService {

    @Transactional
    public void doSomething(long id){
        // Both threads read the same version of the data 
        Registration registrationQueriedTheFirstTime = registrationRepository.findOne(id);

        // First thread gets the lock, second thread waits for the first thread to have committed
        Registration registration = registrationRepository.findOnePessimistic(id);
        // I need this to have this statement, otherwise, registration.getStatus() contains the value not yet updated by the first thread
        entityManager.refresh(registration);

        registration.setStatus(newStatus);
        registrationRepository.save(registration);
    }
}
Run Code Online (Sandbox Code Playgroud)

Dan*_*aub 8

您需要使用的entityManger transactionSpring为你创建:

    @Transactional
    public void doSomething(long id){
        // Both threads read the same version of the data 
        Registration registrationQueriedTheFirstTime = registrationRepository.findOne(id);

        // First thread gets the lock, second thread waits for the first thread to have committed
        Registration registration = registrationRepository.findOnePessimistic(id);
        // I need this to have this statement, otherwise, registration.getStatus() contains the value not yet updated by the first thread
        entityManager.refresh(registration);

        EntityManager em = EntityManagerFactoryUtils.getTransactionalEntityManager(<Your entity manager factory>);
        em.refresh(registration);
        registration.setStatus(newStatus);
        registrationRepository.save(registration);
    }

}
Run Code Online (Sandbox Code Playgroud)

  • 如果我使用`entityManager.refresh`它只是工作正常.我只是希望我不需要那个.这个代码现在正在生产中运行良好. (5认同)