Spring 事务结束后进行耗时操作

Spring Spring Boot 事务 About 2,955 words

TransactionSynchronizationManager

使用TransactionSynchronizationManager类可以注册事务的事件监听,比如提交后完成后等事件。

TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() {
    @Override
    public void afterCommit() {
        // 处理业务逻辑,比如:日志记录、耗时操作(耗时操作建议配合 @Async 异步处理)
        System.out.println("afterCommit#" + Thread.currentThread().getId() + " " + Thread.currentThread().getName());
    }

    @Override
    public void afterCompletion(int status) {
        // int STATUS_COMMITTED = 0;
        // int STATUS_ROLLED_BACK = 1;
        // int STATUS_UNKNOWN = 2;

        System.out.println("afterCompletion#" + status + ": " + Thread.currentThread().getId() + " " + Thread.currentThread().getName());
    }
});

示例

@Transactional
public void tx1() {

    TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() {
        @Override
        public void afterCommit() {
            // 处理业务逻辑,比如:日志记录、耗时操作(耗时操作建议配合 @Async 异步处理)
            System.out.println("afterCommit#" + Thread.currentThread().getId() + " " + Thread.currentThread().getName());
        }

        @Override
        public void afterCompletion(int status) {
            // int STATUS_COMMITTED = 0;
            // int STATUS_ROLLED_BACK = 1;
            // int STATUS_UNKNOWN = 2;

            System.out.println("afterCompletion#" + status + ": " + Thread.currentThread().getId() + " " + Thread.currentThread().getName());
        }
    });
    System.out.println("111111111111#" + Thread.currentThread().getId() + " " + Thread.currentThread().getName());

    // 模拟异常
    int i = 1 / 0;

    System.out.println("222222222222#" + Thread.currentThread().getId() + " " + Thread.currentThread().getName());
}

正常输出

111111111111#29 http-nio-8080-exec-1
222222222222#29 http-nio-8080-exec-1
afterCommit#29 http-nio-8080-exec-1
afterCompletion#0: 29 http-nio-8080-exec-1

异常输出

111111111111#29 http-nio-8080-exec-1
afterCompletion#1: 29 http-nio-8080-exec-1

@TransactionalEventListener

Spring提供的@TransactionalEventListener事务监听器。

示例

public class TxEvent extends ApplicationEvent {

    public TxEvent(BizData source) {
        super(source);
    }

}

@Service
public class BizService {

    @Resource
    ApplicationEventPublisher applicationEventPublisher;

    @Transactional
    public void tx2() {
        applicationEventPublisher.publishEvent(new TxEvent(new BizData(1, "张三")));

        System.out.println("111111111111#" + Thread.currentThread().getId() + " " + Thread.currentThread().getName());

        // int i = 1 / 0;

        System.out.println("222222222222#" + Thread.currentThread().getId() + " " + Thread.currentThread().getName());
    }

    @TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
    public void afterCommit(TxEvent event) {
        System.out.println("afterCommit#" + event.getSource().toString() + " " + Thread.currentThread().getId() + " " + Thread.currentThread().getName());
    }

}
Views: 1,445 · Posted: 2022-10-01

————        END        ————

Give me a Star, Thanks:)

https://github.com/fendoudebb/LiteNote

扫描下方二维码关注公众号和小程序↓↓↓

扫描下方二维码关注公众号和小程序↓↓↓


Today On History
Browsing Refresh