Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: TransactionEventHandler 클래스 추가 #231

Merged
merged 1 commit into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}