@AfterReturning方面在切入点方法的同一事务中执行? [英] @AfterReturning aspect executes in same transaction of pointcut method?

查看:1220
本文介绍了@AfterReturning方面在切入点方法的同一事务中执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在执行功能后执行任务.我已经为此使用了Aspect.但我有些困惑.

I have a requirement to do a task after execution of a function. I have used Aspect for this. but I have some confusion.

我在spring服务中有一个函数A().

I have a function A() in a spring service.

@Transactional(readOnly = false,
               isolation = Isolation.DEFAULT,
               propagation = Propagation.REQUIRED,
               rollbackFor = {Exception.class})
void A() {
 //Do Something
}

我在此功能上有一个@Afterreturning方面.

I have an @Afterreturning aspect on this function.

 @AfterReturning(pointcut = "execution(Sevice.saverecord(..)) ")
 public void processNotifications(JoinPoint jp) {}

我想知道这方面是使用metod A()事务还是它将自动创建一个新事务.由于我尚未对此方面方法定义任何交易.

I want to know whether this aspect will use the transaction of metod A() or it will create a new transaction automatically. As I have not defined any transaction on this aspect method.

我试图在两种方法中都使用TransactionAspectSupport.currentTransactionStatus().toString()打印交易明细.我得到了两个不同的ID.

I tried to print the transaction detail using TransactionAspectSupport.currentTransactionStatus().toString() in both method.I got two different IDs.

任何人都可以解释一下,这是否意味着有两笔交易,如果是,第二笔交易来自何处?

Can anybody explain Does it mean there are two transactions, If yes where this second transaction came from?

推荐答案

@AfterReturning方面的执行与建议方法的事务相同.

Yes the @AfterReturning aspect executes in the same transaction as that of the advised method.

下图描述了调用事务方法时的执行顺序( ref-spring

The execution sequence while invoking a transactional method is depicted in below diagram (ref - spring docs)

方面(即自定义顾问)可以配置为在交易建议之前或之后运行,默认情况下为之后.

The aspect (i.e. custom advisor) can be configured to run before or after the transaction advice, which by default is after.

有关如何配置定制顾问程序以在交易建议之前使用

For further reference on how to configure the custom advisor to run before the transaction advice make use of the Ordered as described here.

这篇关于@AfterReturning方面在切入点方法的同一事务中执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆