-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: TransactionEventHandler 클래스 추가 (#231)
- Loading branch information
1 parent
e8927f1
commit 4310ab1
Showing
4 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
src/main/java/com/dnd/runus/application/config/TransactionEventHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/com/dnd/runus/global/event/AfterTransactionEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() {} | ||
} |
3 changes: 3 additions & 0 deletions
3
src/main/java/com/dnd/runus/global/event/BeforeTransactionEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {} |
5 changes: 5 additions & 0 deletions
5
src/main/java/com/dnd/runus/global/event/TransactionEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.dnd.runus.global.event; | ||
|
||
interface TransactionEvent { | ||
void invoke(); | ||
} |