From 5fdb69ac70577745ec2f3c574ed421facd030fb8 Mon Sep 17 00:00:00 2001 From: pricelees Date: Sun, 17 Aug 2025 21:18:42 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=ED=8A=B8=EB=9E=9C=EC=9E=AD=EC=85=98=20?= =?UTF-8?q?=EB=B6=84=EB=A6=AC=EB=A5=BC=20=EC=9C=84=ED=95=9C=20=EB=B3=84?= =?UTF-8?q?=EB=8F=84=20=EC=9C=A0=ED=8B=B8=20=ED=81=B4=EB=9E=98=EC=8A=A4=20?= =?UTF-8?q?=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/util/TransactionExecutionUtil.kt | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 src/main/kotlin/roomescape/common/util/TransactionExecutionUtil.kt diff --git a/src/main/kotlin/roomescape/common/util/TransactionExecutionUtil.kt b/src/main/kotlin/roomescape/common/util/TransactionExecutionUtil.kt new file mode 100644 index 00000000..28a980e5 --- /dev/null +++ b/src/main/kotlin/roomescape/common/util/TransactionExecutionUtil.kt @@ -0,0 +1,31 @@ +package roomescape.common.util + +import io.github.oshai.kotlinlogging.KLogger +import io.github.oshai.kotlinlogging.KotlinLogging +import org.springframework.stereotype.Component +import org.springframework.transaction.PlatformTransactionManager +import org.springframework.transaction.TransactionDefinition +import org.springframework.transaction.support.TransactionTemplate +import roomescape.common.exception.CommonErrorCode +import roomescape.common.exception.RoomescapeException + +private val log: KLogger = KotlinLogging.logger {} + +@Component +class TransactionExecutionUtil( + private val transactionManager: PlatformTransactionManager +) { + + fun withNewTransaction(isReadOnly: Boolean, action: () -> T): T { + val transactionTemplate = TransactionTemplate(transactionManager).apply { + this.isReadOnly = isReadOnly + this.propagationBehavior = TransactionDefinition.PROPAGATION_REQUIRES_NEW + } + + return transactionTemplate.execute { action() } + ?: run { + log.error { "[TransactionExecutionUtil.withNewTransaction] 트랜잭션 작업 중 예상치 못한 null 반환 " } + throw RoomescapeException(CommonErrorCode.UNEXPECTED_SERVER_ERROR) + } + } +}