带有Spring的Apache Camel-路由事务管理

时间:2019-02-28 16:18:17

标签: java apache-camel spring-camel

我有一条路线

from("direct:my-route")
        .routeId("id")
        .process(log(...))
        .transacted()            // Begin transaction
        .bean(...)   
        .bean(...)      
        .process(...)
        .bean(...)     
        .to("direct:end");       // End transaction
如您所见,

正在交易中。在BeansProcessors内部使用JdbcTemplate执行的所有操作都将被处理。

我们有一个自定义的ErrorHandler,它扩展了DeadLetterChannel,我注意到当引发异常时,无论如何都会进行事务处理。

TransactionTemplate#execute

TransactionStatus status = this.transactionManager.getTransaction(this);
T result;

try {
    result = action.doInTransaction(status);
}
catch (RuntimeException | Error ex) {
    // Transactional code threw application exception -> rollback
    rollbackOnException(status, ex);
    throw ex;
}
catch (Throwable ex) {
    // Transactional code threw unexpected exception -> rollback
    rollbackOnException(status, ex);
    throw new UndeclaredThrowableException(ex, "TransactionCallbac
}
this.transactionManager.commit(status);   <--- after doInTransaction
return result;

调用action.doInTransaction(status);之后,即使我抛出了通用的this.transactionManager.commit(status),代码流也只会进入RuntimeException

action.doInTransaction(status)的调用转到了TransactionErrorHandler,特别是这段代码

transactionTemplate.execute(new TransactionCallbackWithoutResult() {
    protected void doInTransactionWithoutResult(TransactionStatus status) {
        // wrapper exception to throw if the exchange failed
        // IMPORTANT: Must be a runtime exception to let Spring regard it as to do "rollback"
        RuntimeException rce;

        // and now let process the exchange by the error handler
        processByErrorHandler(exchange);

        // after handling and still an exception or marked as rollback only then rollback
        if (exchange.getException() != null || exchange.isRollbackOnly()) {
...

processByErrorHandler只需将Exchange的例外设置为null,所以它不会被重新抛出。


抱歉,此“令人困惑”的简介。我的问题是,在出现异常的情况下如何显式回滚事务?这里有什么更好的策略?

0 个答案:

没有答案