春季事务流程

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

Spring Transactions Flow

问题

Sure, here's the translated code snippet:

需要了解是否所有的lockData流程都在一个事务中当流程返回到Class **A1**我能否看到Class **C1**在employee表上添加的行锁

当前的流程是:`A1`类的一个实例调用`B1`类的一个实例后者又调用`C1`类的一个实例

```java
class A1 {
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void lockData() {
        B1 classBObj = ctx.getBean("B1");
        boolean locked = classBObj.lockData();
        // 检查员工表上的锁是否仍然存在。
    }
}

class B1 {
    @Transactional(propagation = Propagation.REQUIRED)
    public boolean lockData() {
        C1 classCObj = ctx.getBean("C1");
        classCObj.lockData();
        return true;
    }
}

class C1 {
    public void lockData() {
        executeQuery("select * from employee where emp_id=1 for update");
    }
}
英文:

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

class A1 {
	@Transactional(propagation = Propagation.REQUIRES_NEW)
	public void lockData(){	
		B1 classBObj = ctx.getBean("B1");
		boolean locked = classBObj.lockData();		
		//Check if lock on employee table is still there.
	}	
}

class B1 {
	@Transactional(propagation = Propagation.REQUIRED)
	public void lockData(){	
		C1 classCObj = ctx.getBean("C1");
		classCObj.lockData();	
		return true;
	}
}

class C1 {	
	public void lockData(){	
		executeQuery("select * from employee where emp_id=1 for update");
		return true;
	}
}

答案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:

确定