Spring transactional dont rollback when exception is mapped by ExceptionMapper

守給你的承諾、 提交于 2019-12-11 00:11:54

问题


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

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();
    }
}

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

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.

Is there a way to overcome this?


回答1:


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

TransactionAspectSupport.currentTransactionStatus( ).setRollbackOnly();


来源:https://stackoverflow.com/questions/18427512/spring-transactional-dont-rollback-when-exception-is-mapped-by-exceptionmapper

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!