0x0 背景
在項目中,往往需要執行數據庫操作後,發送消息或事件來異步調用其他組件執行相應的操作,例如:
用戶註冊後發送激活碼;
配置修改後發送更新事件等。
但是,數據庫的操作如果還未完成,此時異步調用的方法查詢數據庫發現沒有數據,這就會出現問題。僞代碼如下:
void saveUser(User u) {
//保存用戶信息
userDao.save(u);
//觸發保存用戶事件
applicationContext.publishEvent(new SaveUserEvent(u.getId()));
}
@EventListener
void onSaveUserEvent(SaveUserEvent event) {
//獲取事件中的信息(用戶id)
Integer id = event.getEventData();
//查詢數據庫,獲取用戶(此時如果用戶還未插入數據庫,則返回空)
User u = userDao.getUserById(id);
//這裏可能報空指針異常!
String phone = u.getPhoneNumber();
MessageUtils.sendMessage(phone);
}
0x1 解決方法
爲了解決上述問題,Spring爲我們提供了兩種方式:
(1) @TransactionalEventListener
註解
(2) 事務同步管理器TransactionSynchronizationManager
1.1 @TransactionalEventListener使用方法
仍舊是上述給用戶發短信的例子,代碼如下:
@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
void onSaveUserEvent(SaveUserEvent event) {
Integer id = event.getEventData();
User u = userDao.getUserById(id);
String phone = u.getPhoneNumber();
MessageUtils.sendMessage(phone);
}
這樣,只有當前事務提交之後,纔會執行事件監聽器的方法。其中參數phase默認爲AFTER_COMMIT,共有四個枚舉:
/**
* Fire the event before transaction commit.
* @see TransactionSynchronization#beforeCommit(boolean)
*/
BEFORE_COMMIT,
/**
* Fire the event after the commit has completed successfully.
* <p>Note: This is a specialization of {@link #AFTER_COMPLETION} and
* therefore executes in the same after-completion sequence of events,
* (and not in {@link TransactionSynchronization#afterCommit()}).
* @see TransactionSynchronization#afterCompletion(int)
* @see TransactionSynchronization#STATUS_COMMITTED
*/
AFTER_COMMIT,
/**
* Fire the event if the transaction has rolled back.
* <p>Note: This is a specialization of {@link #AFTER_COMPLETION} and
* therefore executes in the same after-completion sequence of events.
* @see TransactionSynchronization#afterCompletion(int)
* @see TransactionSynchronization#STATUS_ROLLED_BACK
*/
AFTER_ROLLBACK,
/**
* Fire the event after the transaction has completed.
* <p>For more fine-grained events, use {@link #AFTER_COMMIT} or
* {@link #AFTER_ROLLBACK} to intercept transaction commit
* or rollback, respectively.
* @see TransactionSynchronization#afterCompletion(int)
*/
AFTER_COMPLETION
1.2 TransactionSynchronizationManager方法
仍舊是上述案例,代碼如下:
@EventListener
void onSaveUserEvent(SaveUserEvent event) {
TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {
@Override
public void afterCommit() {
Integer id = event.getEventData();
User u = userDao.getUserById(id);
String phone = u.getPhoneNumber();
MessageUtils.sendMessage(phone);
}
});
}
其實,@TransactionalEventListener底層也是這樣實現的。