tcc-transaction笔记(2)-TransactionManager

参考:
https://mp.weixin.qq.com/s?__biz=MzUzMTA2NTU2Ng==&mid=2247484038&idx=1&sn=fb9b8f91146eed545a5464bd85675d15&chksm=fa497d37cd3ef4212df9d16cd685273c01ba992f59ef50b838970004153ca4364fd163eb7c06&scene=21#wechat_redirect

tcc-transaction笔记(2)-TransactionManager_第1张图片

整个事务管理器就是对Transaction的管理,主要如下几个特性

1.结合Transaction的操作,使用TransactionRepository进行更新操作
2.使用ExecutorService进行异步更新操作

看下面代码,都是使用TransactionRepository进行的操作

    public Transaction begin() {

        Transaction transaction = new Transaction(TransactionType.ROOT);
        transactionRepository.create(transaction);
        registerTransaction(transaction);
        return transaction;
    }
    public void rollback(boolean asyncRollback) {

        final Transaction transaction = getCurrentTransaction();
        transaction.changeStatus(TransactionStatus.CANCELLING);

        transactionRepository.update(transaction);

        if (asyncRollback) {

            try {
                executorService.submit(new Runnable() {
                    @Override
                    public void run() {
                        rollbackTransaction(transaction);
                    }
                });
            } catch (Throwable rollbackException) {
                logger.warn("compensable transaction async rollback failed, recovery job will try to rollback later.", rollbackException);
                throw new CancellingException(rollbackException);
            }
        } else {

            rollbackTransaction(transaction);
        }
    }

你可能感兴趣的:(tcc-transaction笔记(2)-TransactionManager)