Delaying sending of mail until transaction commits

前端 未结 4 511
再見小時候
再見小時候 2021-01-23 06:01

Does anyone have a good tutorial or some advice on how to implement one\'s own XAResource? I need Spring\'s MailSender to be transactional, so that the mail will only be sent on

4条回答
  •  北海茫月
    2021-01-23 06:35

    You can use a TransactionSynchronizationManager.registerSynchronization (like gpeche mentioned) with a TransactionSynchronizationAdapter which has a variety of methods that are called at various stages of the current transaction. I think the most suitable method for the question is the afterCommit.

    TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {
        @Override
        public void afterCommit() {
            super.afterCommit();
            sendEmail();
        }
    });
    

提交回复
热议问题