英文:
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)
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论