Java/Spring捕获异常/日志记录和更新状态



假设从spring服务,我想通过客户端调用外部系统,如果调用失败(抛出未检查的异常),我想记录它,在DB中更新订单状态并重新抛出异常,以便事务回滚…

我知道下面的示例代码被认为是一个反模式,但我想不出更好的实现这一点…有什么意见吗?

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

最新更新