当前位置: 首页 > 知识库问答 >
问题:

Spring data CrudRepository和悲观锁

胡玉书
2023-03-14

我在用

  • Spring Boot 1.4.2
  • Spring Data JPA 1.10.5
  • PostgreSQL 9.5数据库
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);
}

不幸的是,这不会刷新实体管理器缓存中我的实体的上一个实例。我有两个同时更新注册状态的请求

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

从而破坏行为。

下面是请求的示例代码:

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

共有1个答案

路雅懿
2023-03-14

您需要使用Spring为您创建的EntityManger事务:

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

}
 类似资料:
  • 悲观锁:假定会发生并发冲突,屏蔽一切可能违反数据完整性的操作 乐观锁:假设不会发生并发冲突,只在提交操作时检查是否违反数据完整性。 乐观锁与悲观锁的具体区别: http://www.cnblogs.com/Bob-FD/p/3352216.html

  • 本文向大家介绍说一下乐观锁和悲观锁?相关面试题,主要包含被问及说一下乐观锁和悲观锁?时的应答技巧和注意事项,需要的朋友参考一下 乐观锁:每次去拿数据的时候都认为别人不会修改,所以不会上锁,但是在提交更新的时候会判断一下在此期间别人有没有去更新这个数据。 悲观锁:每次去拿数据的时候都认为别人会修改,所以每次在拿数据的时候都会上锁,这样别人想拿这个数据就会阻止,直到这个锁被释放。 数据库的乐观锁需要自

  • 问题内容: 我在java下有spring项目,使用hibernate查询,我喜欢使用悲观锁定。 在Spring + Hibernate中如何进行悲观锁定? 编辑: 问题: 我想在一个方法中使用悲观锁定,并且我将此方法称为从不同的方法。当我从第一个方法调用它时,悲观的工作效果很好,但是当我从第二个方法调用它时,它给出了(无法提交事务) 例外: 问题答案: http://www.amicabile.c

  • 嗨, 我们有一个应用程序(J2EE/Hibernate/JPA),其中有几个用户在一个公共实体上执行操作。 句子是单独的实体 几个用户同时更新同一句话的几率很低 在这种情况下,其中一个用户可以收到消息“对不起另一个用户试图编辑相同的句子” 到目前为止还不错。 但是现在,我们已经为这个应用程序添加了后台进程(相当快的进程)。他们经常做出改变(假设它将一个词的出现替换为另一个词)。 这是不能接受的向用

  • 对于使用C11的能否提高性能这一问题,有人发表了评论?这赢得了许多选票,并表明“不太可能无意中悲观”作为答案。我以前从未注意过这个词。我想这是优化的反面。 有人能给出更详细的定义吗?在编程环境中它意味着什么?悲观的代码会是什么样子?

  • 我试图理解Hibernate中的悲观锁定机制(通过MySQL DB)。 我尝试运行以下示例: 但它并没有给我一个错误,而是执行得很好。是不是我误解了什么概念。这种行为正常吗? 我能够完美地测试乐观锁定,那么对于悲观锁定,是对概念有一些误解,还是我的代码缺少了一些东西。