【问题标题】:Proxied REQUIRES_NEW method doesn't rollback (spring)代理的 REQUIRES_NEW 方法不回滚(弹簧)
【发布时间】:2017-05-23 04:46:55
【问题描述】:

我有 spring 将服务注入到自身中,以允许服务对其自身进行事务调用。不幸的是,我发现抛出 NullPointerException 并被捕获的 requires_new 方法并没有回滚新事务。外部事务没有中断,这是我想要的,但我无法解释为什么需要的新事务没有回滚。有什么想法吗?

@Service(value="orderService")
@Transactional
public class OrderServiceImpl implements OrderService {

    @Resource
    private OrderService orderService; // transactional reference to this service

    public void requiredTransMethod(){
        try {
            orderService.requiresNewTransMethod();
        }catch(Throwable t){
            LOG.error("changes from requiresNewTransMethod call should be rolled back right?", t);
        }
    }

    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void requiresNewTransMethod() {
        // database changes that are NOT rolled back
        throw new NullPointerException("bla bla bla");
    }

}

【问题讨论】:

  • 我注意到,在 requiredTransMethod() 返回控制权之前,不会提交未回滚的数据库更改。这让我相信 requiresNewTransMethod() 正在使用调用者的事务。在实际实现中调用的其他 requires_new 方法会在将控制权返回到 requiredTransMethod() 时立即提交。不确定 requires_new 方法有效和无效之间有什么区别。
  • 即使 requiredTransMethod 使用 Propagation.NOT_SUPPORTED 进行注释,在 requiredTransMethod() 返回之前不会提交 requiresNewTransMethod() 更改。

标签: java spring proxy transactions


【解决方案1】:

这可能是transactional annotations not working because you are calling them from within the same class 的一个实例。

Spring 的 AOP 实现的工作方式(默认情况下)是使用代理类,will not work 与同一类中的方法调用一样。

【讨论】:

    猜你喜欢
    • 2018-09-13
    • 2021-01-18
    • 1970-01-01
    • 2020-06-12
    • 2012-12-31
    • 2018-12-09
    • 2018-05-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多