如何使用事务java抛出错误
我在做这样的事情:如何使用事务java抛出错误,java,exception,exception-handling,Java,Exception,Exception Handling,我在做这样的事情: try { client.restoreFromClusterSnapshot(req); } catch (AmazonRedshiftException e) { txUtils.execute((ts) -> { redshiftDto.setStatus(ResourceStatus.FAILED); redshiftDto.setSt
try {
client.restoreFromClusterSnapshot(req);
} catch (AmazonRedshiftException e) {
txUtils.execute((ts) -> {
redshiftDto.setStatus(ResourceStatus.FAILED);
redshiftDto.setStatusDetails(e.getMessage());
redshiftDto.setUpdatedOn(Timestamp.from(Instant.now()));
this.rdao.merge(redshiftDto);
return null;
});
LOGGER.error("CANNOT START REDSHIFT- " + e.getErrorMessage());
throw new AmazonRedshiftException( "CANNOT START REDSHIFT- "
+ e.getErrorMessage());
}
在这段代码中,如果抛出错误,我无法设置数据库变量,因为它将终止我的事务。如果我对该抛出进行注释,它将起作用,并且将设置我的数据库值。但是我不能扔任何东西。我怎样做?(抛出和设置DB中的值)我要做的是使用
finally
子句
AmazonRedshiftException exception = null;
try {
cluster = client.restoreFromClusterSnapshot(req);
} catch (AmazonRedshiftException e) {
exception = e;
LOGGER.error("CANNOT START REDSHIFT- " + e.getErrorMessage());
throw new AmazonRedshiftException( "CANNOT START REDSHIFT- "
+ e.getErrorMessage());
} finally {
if(exception != null) {
txUtils.execute((ts) -> {
redshiftDto.setStatus(ResourceStatus.FAILED);
redshiftDto.setStatusDetails(exception.getMessage());
redshiftDto.setUpdatedOn(Timestamp.from(Instant.now()));
this.rdao.merge(redshiftDto);
return null;
});
}
}
这可能是由于
txUtils
对抛出的异常做出反应,抛出了一个txUtils
无法处理的异常。另外,请指定txUtils所属的类。为什么必须在那里执行该事务,而不是在处理异常的位置执行该事务?尝试调试代码,这将帮助您检查txUtils实际在做什么,控件是否返回到代码行“抛出新的AmazonRedshiftException…”?我正在执行该事务,因为如果restoreFromClusterSnapshot失败,那么它应该将失败设置为db中的红移状态,并且我可以通过抛出它在UI中显示为失败,我将让UI显示错误这可以使用。但是初始化AmazonRedshiftException,然后使用它,行越来越多。我们可以使用像生成BaseException类然后抛出它这样的方法吗