Skip to content

Commit

Permalink
Feat: TransactionEventHandler 클래스 추가 (#231)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaewon-pro authored Sep 26, 2024
1 parent e8927f1 commit 4310ab1
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.dnd.runus.application.config;

import com.dnd.runus.global.event.AfterTransactionEvent;
import com.dnd.runus.global.event.BeforeTransactionEvent;
import org.springframework.core.annotation.Order;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.transaction.event.TransactionalEventListener;

import static org.springframework.transaction.event.TransactionPhase.*;

@Component
public class TransactionEventHandler {

private static final int AFTER_COMMIT_ORDER = 1;
private static final int AFTER_ROLLBACK_ORDER = AFTER_COMMIT_ORDER + 1;
private static final int AFTER_COMPLETION_ORDER = AFTER_COMMIT_ORDER + 2;

/**
* 트랜잭션이 commit되기 전에 실행
*/
@Async
@TransactionalEventListener(phase = BEFORE_COMMIT)
public void processBeforeCommit(BeforeTransactionEvent event) {
event.invoke();
}

/**
* 트랜잭션이 commit된 후에 실행
*/
@Async
@Order(AFTER_COMMIT_ORDER)
@TransactionalEventListener(phase = AFTER_COMMIT)
public void processAfterCommit(AfterTransactionEvent event) {
event.invoke();
}

/**
* 트랜잭션이 rollback된 후에 실행
*/
@Async
@Order(AFTER_ROLLBACK_ORDER)
@TransactionalEventListener(phase = AFTER_ROLLBACK)
public void processAfterRollback(AfterTransactionEvent event) {
event.onTransactionRollback();
}

/**
* 트랜잭션이 commit 또는 rollback된 후에 실행
*/
@Async
@Order(AFTER_COMPLETION_ORDER)
@TransactionalEventListener(phase = AFTER_COMPLETION)
public void processAfterCompletion(AfterTransactionEvent event) {
event.onComplete();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.dnd.runus.global.event;

public interface AfterTransactionEvent extends TransactionEvent {
default void onTransactionRollback() {}

default void onComplete() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.dnd.runus.global.event;

public interface BeforeTransactionEvent extends TransactionEvent {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.dnd.runus.global.event;

interface TransactionEvent {
void invoke();
}

0 comments on commit 4310ab1

Please sign in to comment.