如何向Atomikos TransactionStateHandler添加同步

时间:2018-11-12 14:24:22

标签: atomikos

如何在atomikos中向TransactionStateHandler添加同步?默认情况下,它仅包含JdbcRequeueSynchronization,其中有空的beforeCompletion方法

private Throwable notifyBeforeCompletion() {
    Throwable cause = null;
    Synchronization sync = localPopSynchronization();
    while ( sync != null ) {
        try {
            sync.beforeCompletion ();
        } catch ( RuntimeException error ) {
            // see case 24246: rollback only
            setRollbackOnly();
            // see case 115604
            // transport the first exception here as return value
            if (cause == null) {
                cause = error;
            } else {
                // log the others which may still happen as error - cf. case 115604
                LOGGER.logError("Unexpected error in beforeCompletion: ", error);
            }               
        }
        sync = localPopSynchronization();
    }
    return cause;
}

1 个答案:

答案 0 :(得分:0)

您可以通过Transaction对象(在JTA API中)执行此操作:

UserTransactionManager utm = new UserTransactionManager();
utm.begin(); //optional, skip if you already have a transaction
Transaction tx = utm.getTransaction();
tx.registerSynchronization(...);
...
//commit / rollback per your requirements

希望有帮助