Spring数据CrudRepository和悲观锁 [英] Spring data CrudRepository and pessimistic lock

查看:330
本文介绍了Spring数据CrudRepository和悲观锁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用


  • Spring Boot 1.4.2

  • Spring Data JPA 1.10。 5

  • PostgreSQL 9.5数据库

我想要一个 findOne 我的Spring Data存储库中带有悲观锁的方法,该方法与已经提供的 findOne 方法分开。

I want to have a findOne method with pessimistic lock in my Spring Data repository that is separate from the findOne method that is already provided.

关于这个答案,我写道:

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

这几乎有效。

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

Unfortunately, this does not refresh previous instance of my entity in the entity manager cache. I have two concurrent requests updating the status of my registration


  • 第二个请求等待第一个提交的交易

  • 第二个考虑第一个所做的更改。

  • the second one waits for the transaction of the first one to commit
  • the second one does not take into account the changes made by the first one.

因此破坏了行为。

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

Any clue why @Lock does not out of the box refresh the entity manager?

更新

以下是请求的示例代码:

Here is the requested example code:

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


推荐答案

你需要使用 entityManger交易 Spring 为您创建:

You need to use the entityManger transaction that Spring creates for you :

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

}

这篇关于Spring数据CrudRepository和悲观锁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆