DB commit을 하고나서 commit이 완료했을 때 받을 수 있는 이벤트
즉, DB commit을 보장한 데이터에 대한 이후 처리 가능
DB commit을 했다는건 해당 데이터/변경사항이 DB에 저장이 되었다는 확신
method
afterCommit() | Invoked after transaction commit. | |
afterCompletion(int status) | Invoked after transaction commit/rollback. | |
beforeCommit(boolean readOnly) | Invoked before transaction commit (before "beforeCompletion"). | |
beforeCompletion() | Invoked before transaction commit/rollback. | |
flush() | Flush the underlying session to the datastore, if applicable: for example, a Hibernate/JPA session. | |
resume() | Resume this synchronization. | |
suspend() | Suspend this synchronization. |
즉,
afterCommit은 commit 이후
afterComplete는 commit/rollback이던 완료후 상태를 받음.
적용 사례
@Transactional public UnloadWork saveAndTriggerEvent(UnloadWork unloadWork) { Prerequisites.checkObjectNotNull(unloadWork); UnloadWork unloadWorkSaved = unloadRepository.save(unloadWork); if (TransactionSynchronizationManager.isSynchronizationActive()) { TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() { @Override public void afterCompletion(int status) { if (status == TransactionSynchronization.STATUS_COMMITTED) { log.error("UNLOAD WORK SAVE : COMMITED"); } else if (status == TransactionSynchronization.STATUS_ROLLED_BACK) { log.error("UNLOAD WORK SAVE : ROLLED_BACK"); } else { // TransactionSynchronization.STATUS_UNKNOWN log.error("UNLOAD WORK SAVE : UNKNOWN"); } } @Override public void afterCommit() { // DB 로 부터 commit 완료 Long unloaWorkId = unloadWorkSaved.getId(); unloadFinishEventService.publishEvent(unloaWorkId); try { Thread.sleep(100L); log.warn("unloaWork finish event triggered : id " + unloaWorkId); } catch (Exception e) { log.warn("unloaWork finish event triggered error : id " + unloaWorkId, e); } } }); } return unloadWorkSaved; } |
TransactionSynchronization (Spring Framework 5.3.16 API)
Invoked after transaction commit/rollback. Can perform resource cleanup after transaction completion. NOTE: The transaction will have been committed or rolled back already, but the transactional resources might still be active and accessible. As a conseque
docs.spring.io
TransactionSynchronization (Spring Framework 5.3.16 API)
Invoked after transaction commit/rollback. Can perform resource cleanup after transaction completion. NOTE: The transaction will have been committed or rolled back already, but the transactional resources might still be active and accessible. As a conseque
docs.spring.io