[英]Java/Spring catching Exceptions/Logging and Update status
Lets say that from a spring service, i want to invoke through a client an external system, if that invocation fails (throws unchecked exception) i want to log it, update order status in DB and rethrow the exception for the transaction to be rollbacked... 可以说,我想从Spring服务中通过客户端调用一个外部系统,如果该调用失败(引发未经检查的异常),我想记录该事件,更新DB中的订单状态,并重新抛出该异常以使事务回滚。 ..
I know that below sample code is considered to be an anti-pattern but i cannot think of something better to achieve this...any comments pls? 我知道下面的示例代码被认为是一种反模式,但是我想不出什么更好的方法来实现...任何评论,请问?
public class Service {
@Autowired(required = true)
private Client client;
@Autowired(required = true)
private DAO d;
@Transactional
@Override
public void register(String id) {
try{
client.invoke(id);//throws Client unchecked exception
}
catch (ClientException e){
LOG.error (e);
d.updateStatus(id,"failed");
throw e;
}
}
}
如果抛出异常只是为了使事务回滚,则最好使用它并调用setRollbackOnly
TransactionInterceptor.currentTransactionStatus().setRollbackOnly();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.