将方法放在方面之后,Spring @Transactional不会回滚 [英] Spring @Transactional wont rollback after putting aspect around method

查看:471
本文介绍了将方法放在方面之后,Spring @Transactional不会回滚的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两种相互之间的事务处理方法.当围绕方法未设置围绕方面时事务注释工作良好.调用methodA之后,我们调用methodB,methodB在DB中写一些东西,然后返回methodA,抛出异常,然后methodB回滚. 但是,当我将自己的观点放在方法A周围时,方法B不会回滚.我不知道那里发生了什么.我尝试了传播属性的许多组合,但似乎都没有用. 提前致谢. 我使用Spring 2.5.4

I have two transactional methods one inside other. When around aspect is not set around methodA Transactional anotation work well. After calling methodA we call methodB, methodB write something in DB, then we return in methodA, throw an exception and then methodB roolback. But when I put my aspect around methodA, methodB wont rollback. I cant figure out what is happening there. I tried many combination of propagation attributes but none seems to work. Thanks in advance. I use Spring 2.5.4

我在applicationContext.xml中具有此配置:

I have this configuration in applicationContext.xml :

<!-- Aspect -->
<bean id="logAspect" class="LoggingAspect" />
<aop:config>
    <aop:aspect id="aspectLoggging" ref="logAspect" >
        <aop:pointcut id="testAround" expression="execution(* methodA(..))" />
        <!-- @Around -->
        <aop:around method="logProcess" pointcut-ref="testAround" />
    </aop:aspect>
</aop:config>

我的LoggingAspect类是这样的:

My LoggingAspect class is like this :

@Aspect
public class LoggingAspect {
    public void logProcess(ProceedingJoinPoint joinPoint) throws Throwable {
        <!-- some code before -->
        try {
            Object result = joinPoint.proceed();
        } catch (Exception e) {
            log.info(e.getMessage());
            <!-- some code here -->     
        }   

        <!-- some code after -->
    }
}

MethodA是这样的:

MethodA is like this :

@Transactional(rollbackFor=Exception.class,propagation=Propagation.REQUIRED)
public something methodA() throws Exception {
    methodB();
    ...
    throw new Exception("message ...");
    ...
}

MethodB就像这样:

MethodB is like this :

@Transactional(rollbackFor=Exception.class,propagation=Propagation.REQUIRED)
public void methodB() throws Exception {
    <!-- insert something in db --<
}

推荐答案

如果有缺陷,您的方面

  1. 您必须始终从环绕"方面返回Object
  2. 永远不要捕获并吞下异常


public void logProcess(ProceedingJoinPoint joinPoint) throws Throwable  { ... }

您的方面有一个void方法,该方法应该为Object,并且您应始终将调用结果返回给proceed().

Your aspect has a void method this should be Object and you should always return the result of the call to proceed().

接下来,如果有异常发生,您将捕获并吞下该异常,如果不这样做会破坏适当的发送,管理,则应始终抛出异常.

Next you are catching and swallowing the exception if any would occur, you should always rethrow the exception if you don't this breaks proper tx, management.

您的方面应该看起来更像这样.

Your aspect should look more liks this.

@Aspect
public class LoggingAspect {
    public Object logProcess(ProceedingJoinPoint joinPoint) throws Throwable {
        <!-- some code before -->
        try {
            Object result = joinPoint.proceed();
            <!-- some code after -->
            return result;
        } catch (Exception e) {
            log.info(e.getMessage());
            <!-- some code here -->     
            throw e;
        }   
    }
}

这篇关于将方法放在方面之后,Spring @Transactional不会回滚的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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