Spring data CrudRepository 和悲观锁

30

我正在使用:

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

我想在我的 Spring Data 存储库中拥有一个悲观锁的 findOne 方法,该方法与已提供的 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);
}

这几乎可以使用。

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

  • 第二个等待第一个事务提交
  • 第二个没有考虑到第一个所做的更改。

因此导致了错误的行为。

你有什么线索知道为什么@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);
    }
}

你必须展示出改变实体值的代码。为什么在一个只读实体的方法上要使用“PESSIMISTIC_WRITE”锁定表呢? - Tobias Otto
我在一个被注解为@Transactional的方法中使用代码,读取实体,更新它并将其写回。非常标准。我想避免此操作上的并发,因此我想使用悲观锁。我只想在update之前执行select for update - rcomblen
1
如果我指定悲观锁,我希望CrudRepository从一级缓存中清除该实体。 - rcomblen
你不需要第一次获取。删除它即可。你的Registration实体有任何二级缓存控制吗? - Arnold Galovics
1
问题不在于悲观锁在我的情况下有多少相关性,而在于为什么Spring-Data-JPA不会使用数据库中锁定的值来刷新缓存...顺便说一句,我使用悲观锁是因为对我的状态更新(例如发送电子邮件)产生了副作用,这些副作用无法回滚。 - rcomblen
显示剩余3条评论
1个回答

10

您需要使用由Spring为您创建的entityManger transaction

    @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);
    }

}

5
如果我使用entityManager.refresh,它就可以正常工作。但是我本来不希望需要这样做。目前这段代码在生产环境中运行良好。 - rcomblen
@RahulGupta 看起来没有问题,rcomblen 只是好奇为什么他必须手动刷新。 - hipokito

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接