如何在同一事务中保留Advice和targetObject

时间:2017-05-17 06:57:16

标签: java spring aop spring-transactions

请帮忙,如何将建议和targetObject保留在同一笔交易中?

我正在使用spring-boot:1.4.0.RELEASE,并使用@Transactional来控制事务,代码如下,

@Transactional
public String doA() {
    insertA();
    insertB();
    insertC();
    return null;
}

我已经定义了建议,并希望将Advice和doA()保留在同一个事务中。

@Aspect
@Component
public class DoAAspect implements Ordered {
    @Pointcut(value = "execution(* doA()")
    public void pointcut(){
    }
    @Around(value = "pointcut()")
    public Object around(ProceedingJoinPoint jointPoint) throws Throwable {
        try {
            Object returnVal = jointPoint.proceed();
            if(true)
                throw new RuntimeException("xxxx");
            return returnVal;
        } catch (Throwable tx) {
            tx.printStackTrace();
            throw tx;
        }
    }
    public int getOrder() {
        return 0; // I’m trying to return Ordered.HIGHEST_PRECEDENCE or Ordered.LOWEST_PRECEDENCE
    }
}

我希望它可以在抛出新的RuntimeException()时回滚insertA()insertB(),insertC()的事务。但无论我怎么做,都失败了。呼叫如下: DoAAspect.around() - > TransactionAspectSupport.invokeWithinTransaction() - > doA() - > TransactionAspectSupport.invokeWithinTransaction() - > DoAAspect.around() colud如何保持建议和doA()在同一个交易中?请,thx

0 个答案:

没有答案