ExceptionMapper映射异常时,Spring事务不回滚 [英] Spring transactional dont rollback when exception is mapped by ExceptionMapper

查看:324
本文介绍了ExceptionMapper映射异常时,Spring事务不回滚的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个RESTFul服务(由jersey实现).该服务标有@Transactional.

I have a RESTFul service (implemented by jersey) . The service is marked with @Transactional.

我这样声明了ExceptionMapper:

I declared an ExceptionMapper like this:

@Provider
public class ThrowableMapper implements ExceptionMapper<Throwable> {
    private static final Logger log = Logger.getLogger(ThrowableMapper.class);

    public Response toResponse(Throwable ex) {
        log.error("throwable", ex);
        return Response.status(500).entity("Internal Error").type("text/plain").build();
    }
}

未声明exceptionmapper时回滚事务. 但是,当我有一个ExceptionMapper事务时,提交无需回滚.

when exceptionmapper is not declared than transaction is rollback. However, when i have an ExceptionMapper transaction is commit without rollback.

我认为事务不回滚的原因是因为ExceptionMapper捕获了异常,而不是Spring事务代理dosnt检测到引发了异常,所以事务不回滚.

I assume the reason for for transaction not being rollback is because when exception is caught by ExceptionMapper than spring transaction proxy dosnt detect that exception was thrown ,so transaction is not rollback.

有办法克服吗?

推荐答案

这不是最干净的解决方案,但没有发现其他任何东西. 我将此添加到ExceptionMapper:

This is not the cleanest solution, but didn't find anything else. I add this to the ExceptionMapper:

TransactionAspectSupport.currentTransactionStatus( ).setRollbackOnly();

这篇关于ExceptionMapper映射异常时,Spring事务不回滚的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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