在类和方法上定义@Transactional有什么区别 [英] What is the difference between defining @Transactional on class vs method
问题描述
@Transactional
public class UserServiceImpl implements UserService {
...................
public void method1(){
try{
method2();
}catch(Exception e){
}
}
public void method2(){
}
}
Case2
Case2
public class UserServiceImpl implements UserService {
...................
public void method1(){
try{
method2();
}catch(Exception e){
}
}
@Transactional
public void method2(){
}
}
在case1中,如果发生任何异常,则回滚正在运行,但在情况2中它是不工作如果我遵循case1会有任何性能问题吗?
In case1 if any exception occurs it rollback is working, but in case 2 it's not working. Is there any performance issues if I follow the case1?
推荐答案
@Transactional
on class适用于服务上的每个方法。这是一条捷径。通常,如果您知道所有方法都将访问存储库层,则可以在服务类上设置 @Transactional(readonly = true)
。然后,您可以使用 @Transactional
覆盖模型中执行更改的方法的行为。 1)和2)之间的性能问题未知。
@Transactional
on a class applies to each method on the service. It is a shortcut. Typically, you can set @Transactional(readonly = true)
on a service class, if you know that all methods will access the repository layer. You can then override the behavior with @Transactional
on methods performing changes in your model. Performance issues between 1) and 2) not known.
这篇关于在类和方法上定义@Transactional有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!