简体   繁体   English

spring aop如何使用jdk代理处理@Transactional?

[英]how spring aop handle @Transactional with jdk proxy?

If one Service class has method with @Transactional, then spring will use proxy to handle it.如果一个 Service 类具有带有 @Transactional 的方法,那么 spring 将使用代理来处理它。 But if one Transactional method call another one但是如果一个事务方法调用另一个

@Transactional
public FeedBackModel getOne() {
    ///..
    return getTwo();
}
@Transactional
public FeedBackModel getTwo() {
    return null;
}

like this.像这样。

if it is jdk proxy, then second @Transactional will not work.如果是 jdk 代理,那么第二个 @Transactional 将不起作用。 But spring PROPAGATION will handle this correctly.但是 spring PROPAGATION 会正确处理这个问题。 How it works?它是如何工作的?

If you are trying to commit a transaction in getTwo() which is called from getOne() , that will not work, not even when both are @Transactional.如果你想在提交事务getTwo()这是从所谓的getOne()这是行不通的,即使是在两者都是@Transactional。 Refer to the documentation :请参阅文档

...please do take the Spring team's advice and only annotate concrete classes (and the methods of concrete classes) with the @Transactional annotation. ...请接受 Spring 团队的建议,只使用 @Transactional 注释来注释具体类(以及具体类的方法)。

Note: Since this mechanism is based on proxies, only 'external' method calls coming in through the proxy will be intercepted.注意:由于此机制基于代理,因此只会拦截通过代理传入的“外部”方法调用。 This means that 'self-invocation' , ie a method within the target object calling some other method of the target object, won't lead to an actual transaction at runtime even if the invoked method is marked with @Transactional!这意味着'self-invocation' ,即目标对象中的一个方法调用目标对象的其他方法,即使被调用的方法用@Transactional 标记,也不会在运行时导致实际的事务

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM