如何使用 Spring boot JPA/hibernate 将 pessimistic_write 锁与 Spanner 一起使用

发布于 2025-01-19 06:12:46 字数 1001 浏览 1 评论 0原文

我正在使用带有 Spring boot 和 JPA 的 Spanner 数据库。 我有一个要求,每当从员工表中读取员工信息时,在事务完成之前没有人可以读取/更新/删除同一行信息。

我知道出于这种目的,JPA 中使用了“PESSIMISTIC_WRITE”锁。 (参考了这个链接 具有多个实例的应用程序上的 JPA PESSIMISTIC_WRITE

我尝试使用通过使用下面的代码,但它没有持有锁。 当我尝试从其他实例执行相同的查询时,它会给出结果。理想情况下,它应该等到第一个事务完成但仍然给出结果。

您能给我建议任何解决方案吗?

我正在使用下面的代码来实现相同的目的。

@Repository
public interface EmployeeRepository extends JpaRepository<EmployeeEntity, Integer> {

    @lock(LockModeType.PESSIMISTIC_WRITE)
    @query("SELECT c FROM Employee c WHERE c.empId = :empId)
    public EmployeeEnity getEmployee(@param("empId") Integer empId);
}

@Service
public class EmpService implement IEmpService {

@Autowired
EmployeeRepository  empRepository;

@Transactional
@Override
    public EmployeeEntity getEmployee(Integer empId) {

        return empRepository.getClientId(clientId);
        
    }
}

I am using spanner database with Spring boot and JPA.
I have a requirement like, whenever employee information is read from employee table, no-one can read/update/delete the the same row information until transaction gets completed.

I got to know for such purpose "PESSIMISTIC_WRITE" lock is used in JPA.
(referred this link
JPA PESSIMISTIC_WRITE on applications with multiple instances)

I tried to use it by using below code but its not holding the lock.
When I try to hit the same query from other instance its giving result. Ideally It should wait until first transaction gets completed but still its giving the result.

Could you please suggest me any solution for it.

I am using below code to achieve the same.

@Repository
public interface EmployeeRepository extends JpaRepository<EmployeeEntity, Integer> {

    @lock(LockModeType.PESSIMISTIC_WRITE)
    @query("SELECT c FROM Employee c WHERE c.empId = :empId)
    public EmployeeEnity getEmployee(@param("empId") Integer empId);
}

@Service
public class EmpService implement IEmpService {

@Autowired
EmployeeRepository  empRepository;

@Transactional
@Override
    public EmployeeEntity getEmployee(Integer empId) {

        return empRepository.getClientId(clientId);
        
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

躲猫猫 2025-01-26 06:12:46

你是说你需要一把锁

在事务完成之前没有人可以读取/更新/删除同一行信息

这对我来说听起来像是排他锁。 Cloud Spanner 中不提供此功能。

问题是在这种情况下这是否是您真正需要的。 Cloud Spanner 中的读/写事务具有外部一致性。总结一下,对于您的具体问题,可以归结为以下内容:

  1. 事务 A 读取行 R1。
  2. 交易 A 改变了 R1 的值。
  3. 事务 A 提交。

Cloud Spanner 保证在事务 A 提交时,R1 的值仍与其在步骤 1 中读取的值相同。如果您还有一个事务 B 几乎同时执行以下操作:

  1. 事务 B 读取行 R1。
  2. 交易 B 改变了 R1 的值。
  3. 事务 B 提交。

(因此两个事务都读取并修改同一行)

那么可以保证这样做的效果将是事务 A 先执行,然后事务 B。或者先执行事务 B,然后事务 A。(即;在所有情况下,两个事务都保证它们在步骤 1 中看到的值在提交时仍然未被其他任何人修改。

You are saying that you need a lock where

no-one can read/update/delete the the same row information until transaction gets completed

That sounds like an exclusive lock to me. That is not available in Cloud Spanner.

The question is whether that is what you really need in this case. Read/write transactions in Cloud Spanner have external consistency. To summarize that very shortly for your specific question this boils down to the following:

  1. Transaction A reads row R1.
  2. Transaction A changes the value of R1.
  3. Transaction A commits.

Cloud Spanner guarantees that at the moment Transaction A commits, the value of R1 is still the same as it read at step 1. If you also have a transaction B that does the following at approximately the same time:

  1. Transaction B reads row R1.
  2. Transaction B changes the value of R1.
  3. Transaction B commits.

(So both transactions read and modify the same row)

Then it is guaranteed that the effect of this will be that either transaction A executed first, and then transaction B. Or that transaction B executed first, and then transaction A. (That is; it will seem that the transactions executed sequentially.) In all cases both transactions are guaranteed that the value they saw at step 1 is still unmodified by anyone else when it commits.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文