@事务方面的建议可能吗? [英] @Transactional on aspect advice possible?

查看:97
本文介绍了@事务方面的建议可能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以将@Transactional标记应用于方面建议吗?我正在尝试使用方面将所有对服务层的调用(com.mycompany.app.myapp.service.*)进行包装.我的方面是正确拦截对服务层的调用,但是我不知道如何开始事务.我以为我可以应用@Transactional标记,因为我已经有了该标记,所以它将拿起它并开始交易.我想念什么?

Can I apply the @Transactional tag to an aspect advice? I'm trying to wrap all calls to the service layer (com.mycompany.app.myapp.service.*) in a transaction using aspects. My aspect is properly intercepting the calls to the service layer, but I can't figure out how to start a transaction. I thought I could apply the @Transactional tag and because I've got the tag, it'd pick it up and begin the transaction. What am I missing?

XML配置:

XML configuration:

<bean id="systemArchitectureAspect" class="com.mycompany.app.myapp.aspect.SystemArchitecture"/>
<bean id="transactionAspect" class="com.mycompany.app.myapp.aspect.MyAspect"/>

<tx:annotation-driven transaction-manager="transactionManager" />

<bean id="transactionManager"  
    class="org.springframework.transaction.jta.JtaTransactionManager"> 
    <property name="transactionManager" ref="AtomikosTransactionManager" /> 
    <property name="userTransaction" ref="AtomikosUserTransaction" /> 
</bean> 

<bean id="AtomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager"  
    init-method="init" destroy-method="close"> 

    <property name="forceShutdown" value="false" /> 
</bean> 

<bean id="AtomikosUserTransaction"  
    class="com.atomikos.icatch.jta.UserTransactionImp"> 
    <property name="transactionTimeout" value="10" />
</bean> 

<tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
        <tx:method name="*" propagation="REQUIRED" />
    </tx:attributes>
</tx:advice>

使用切入点的方式:

package com.mycompany.app.myapp.aspect;

@Aspect
public class SystemArchitecture {
    @Pointcut( "execution(* com.mycompany.app.myapp.service..*.*(..))" )
    public void inServiceLayer() {};

    @Pointcut( "execution(* com.mycompany.data..*.*(..))" )
    public void inDataAccessLayer() {};
}

我要应用于切入点的建议

The advice I'm trying to apply to my pointcuts:

package com.mycompany.app.myapp.aspect;

@Aspect
public class TransactionAspect {

    @Transactional
    @Around( "com.mycompany.app.myapp.aspect.SystemArchitecture.inServiceLayer()" )
    public Object interceptServiceLayer( ProceedingJoinPoint pjp ) throws Throwable
    {
        return pjp.proceed();
    }
}

推荐答案

下面,我有一个示例,展示了如何将@TransactionalinServiceLayer() Pointcut一起使用.我选择将正常流程与异常流程分开.这就是为什么我不使用@Around建议的原因.

Below I have an example that shows how you can use @Transactional together with your inServiceLayer() Pointcut. I have chosen to separate the normal flow from the exception flow. That is why I do not use the @Around advice.

@Aspect
public class TransactionAspect {
    private TransactionService transactionService = new TransactionServiceNull();

    @Pointcut( "execution(* com.mycompany.app.myapp.service..*.*(..))" )
    public void inServiceLayer() {};

    @Pointcut("execution(@org.springframework.transaction.annotation
        .Transactional * *(..))")
    public void transactionalMethod() {}

    @Before("transactionalMethod() && inServiceLayer()")
    public void beforeTransactionalMethod(JoinPoint joinPoint) {
        transactionService.beginTransaction();
    }

    @AfterReturning("transactionalMethod() && inServiceLayer()")
    public void afterTransactionalMethod(JoinPoint joinPoint) {
        transactionService.commit();
    }

    @AfterThrowing(pointcut = "transactionalMethod() && inServiceLayer()", 
         throwing = "e")
    public void afterThrowingFromTransactionalMethod(JoinPoint joinPoint, 
         RuntimeException e) {
        transactionService.rollback();
    }

    public void setTransactionService(
        final TransactionService transactionService) {
        this.transactionService = transactionService;
    }
}

快速浏览代码后,我不得不问为什么用@Transactional注释Pointcut?您应该只用它标记要在事务中执行的业务方法.

After a quick look on your code I have to ask why you have annotated your Pointcut with @Transactional? You should only mark your business methods that you want to be executed in a transaction with that.

我希望这会有所帮助!

这篇关于@事务方面的建议可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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