简体   繁体   English

从服务类中调用时,Spring @Transactional不适用于带注释的方法

[英]Spring @Transactional doesn't work for an annotated method when called from within service class

In below code , when methodInner() is called from within methodOuter , should be under transaction bounds. 在下面的代码中,当从methodOuter中调用methodInner()时,应该在事务边界下。 But it is not. 但事实并非如此。 But when methodInner() is called directly from MyController class , it is bound by transaction. 但是当直接从MyController类调用methodInner()时,它受到事务的约束。 Any explanations? 有什么解释吗?

This is controller class. 这是控制器类。

@Controller
public class MyController {

    @Autowired
    @Qualifier("abcService")
    private MyService serviceObj;

    public void anymethod() {
        // below call cause exception from methodInner as no transaction exists  
        serviceObj.methodOuter(); 
    }

}

This is service class . 这是服务类

@Service("abcService")
public class MyService {

    public void methodOuter() {
        methodInner();
    }

    @Transactional
    public void methodInner() {
    .....
    //does db operation.
    .....
    }
}

Spring uses Java proxies by default to wrap beans and implement annotated behavior. Spring默认使用Java代理来包装bean并实现带注释的行为。 When doing calls within a service you bypass proxy and run method directly, so annotated behavior is not triggered. 在服务中进行调用时,您可以直接绕过代理和运行方法,因此不会触发带注释的行为。

Possible solutions: 可能的解决方案:

  1. Move all @Transactional code to separate service and always do calls to transactional methods from outside 将所有@Transactional代码移动到单独的服务,并始终从外部调用事务方法

  2. Use AspectJ and weaving to trigger annotated behavior even within a service 即使在服务中,也可以使用AspectJ和编织来触发带注释的行为

@Transactional添加到methodOuter()并且它可以正常工作。

暂无
暂无

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

相关问题 Spring 没有为 @service 注释的 class 创建 bean,当它的方法之一用 @transactional 注释时 - Spring is not creating the bean for @service annotated class when one of its method is annotated with @transactional 从Spring Boot测试调用的@Caching方法[注有@Transactional]无法正常工作 - @Caching method called from spring boot test [annotated with @Transactional] not working 如果在内部@Spring注释了方法,则Spring @Transactional注释方法是否会覆盖@Transactional(readOnly = true)方法? - Does a Spring @Transactional annotated method overwrite an @Transactional(readOnly=true) method if called within it? Spring AOP不适用于包含@Transactional方法的类 - Spring AOP doesn`t work with class comprising @Transactional method Spring 为在 @Transactional 注释方法中调用的每个 JpaRepository 方法打开一个新事务 - Spring opens a new transaction for each JpaRepository method that is called within an @Transactional annotated method 春天@transactional不起作用 - Spring @transactional doesn't work Spring 4 @Transactional无法正常工作 - Spring 4 @Transactional doesn't work 从另一个方法调用的@Transactional 方法不获取事务 - @Transactional method called from another method doesn't obtain a transaction 来自带有@Scheduled注释的Cron方法的Spring MVC Access Service类 - Spring MVC Access Service class from a Cron Method annotated with @Scheduled 为什么 Hibernate 从 @Transactional 方法(Spring boot)返回对象时不能完全解析我的对象? - Why doesn't Hibernate fully resolve my object when returning it from a @Transactional method (Spring boot)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM