generated from pricelees/issue-pr-template
feat: 트랜잭션 분리를 위한 별도 유틸 클래스 생성
This commit is contained in:
parent
d19973978f
commit
5fdb69ac70
@ -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 <T> 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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user