问题描述
我有一个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事务不回滚的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!