春季事务流程

huangapple go评论100阅读模式
英文:

Spring Transactions Flow

问题

Sure, here's the translated code snippet:

  1. 需要了解是否所有的lockData流程都在一个事务中当流程返回到Class **A1**我能否看到Class **C1**employee表上添加的行锁
  2. 当前的流程是`A1`类的一个实例调用`B1`类的一个实例后者又调用`C1`类的一个实例
  3. ```java
  4. class A1 {
  5. @Transactional(propagation = Propagation.REQUIRES_NEW)
  6. public void lockData() {
  7. B1 classBObj = ctx.getBean("B1");
  8. boolean locked = classBObj.lockData();
  9. // 检查员工表上的锁是否仍然存在。
  10. }
  11. }
  12. class B1 {
  13. @Transactional(propagation = Propagation.REQUIRED)
  14. public boolean lockData() {
  15. C1 classCObj = ctx.getBean("C1");
  16. classCObj.lockData();
  17. return true;
  18. }
  19. }
  20. class C1 {
  21. public void lockData() {
  22. executeQuery("select * from employee where emp_id=1 for update");
  23. }
  24. }
英文:

Need to understand if all the three flows of lockData are in one transaction. Can I see the row lock added by Class C1 on table employee when the flow comes back to Class A1?

Flow currently is: An instance of A1 calls an instance of B1 which in turn calls an instance of C1

  1. class A1 {
  2. @Transactional(propagation = Propagation.REQUIRES_NEW)
  3. public void lockData(){
  4. B1 classBObj = ctx.getBean("B1");
  5. boolean locked = classBObj.lockData();
  6. //Check if lock on employee table is still there.
  7. }
  8. }
  9. class B1 {
  10. @Transactional(propagation = Propagation.REQUIRED)
  11. public void lockData(){
  12. C1 classCObj = ctx.getBean("C1");
  13. classCObj.lockData();
  14. return true;
  15. }
  16. }
  17. class C1 {
  18. public void lockData(){
  19. executeQuery("select * from employee where emp_id=1 for update");
  20. return true;
  21. }
  22. }

答案1

得分: 0

如果使用的 A1 实例是一个 Spring bean,并且通过 Spring Bean 上下文注入,那么是的,对 a1.lockData() 的调用(以及其所有嵌套调用)将在一个事务中执行。

为了完整起见,我建议给 C1:lockData 加上 @Transactional(propagation = Propagation.REQUIRED) 注解。

英文:

If the instance of A1 used is a spring-bean and injected through the spring bean context then yes, a call to a1.lockData() (and all its nested calls) will be executed in one transaction.

For completeness sake, I would suggest annotating C1:lockData with @Transactional(propagation = Propagation.REQUIRED).

huangapple
  • 本文由 发表于 2020年8月30日 19:56:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/63657163.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定