Proxied REQUIRES_NEW方法不回滚(弹簧)

时间:2015-04-16 16:29:18

标签: java spring proxy transactions

我让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");
    }

}

1 个答案:

答案 0 :(得分:1)

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

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