fix: 코루틴 내 트랜잭션 간 조회 안 되는 문제 해결 #318

Merged
klaus merged 3 commits from test into main 2025-05-22 04:31:43 +00:00
1 changed files with 17 additions and 2 deletions
Showing only changes of commit af352256e9 - Show all commits

View File

@ -1,7 +1,9 @@
package kr.co.vividnext.sodalive.useraction
import kotlinx.coroutines.CoroutineExceptionHandler
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.cancel
import kotlinx.coroutines.launch
import kr.co.vividnext.sodalive.content.order.OrderRepository
import kr.co.vividnext.sodalive.fcm.FcmService
@ -14,6 +16,8 @@ import org.slf4j.LoggerFactory
import org.springframework.stereotype.Service
import org.springframework.transaction.support.TransactionTemplate
import java.time.LocalDateTime
import javax.annotation.PreDestroy
import javax.persistence.EntityManager
@Service
class UserActionService(
@ -24,10 +28,15 @@ class UserActionService(
private val memberPointRepository: MemberPointRepository,
private val transactionTemplate: TransactionTemplate,
private val fcmService: FcmService
private val fcmService: FcmService,
private val entityManager: EntityManager
) {
private val coroutineScope = CoroutineScope(Dispatchers.IO)
private val coroutineScope = CoroutineScope(
Dispatchers.IO + CoroutineExceptionHandler { _, e ->
logger.error("포인트 지급 또는 알림 실패: ${e.message}")
}
)
fun recordAction(
memberId: Long,
@ -53,6 +62,7 @@ class UserActionService(
if (isAuth) {
try {
transactionTemplate.execute {
entityManager.clear()
val policy = policyRepository.findByActionTypeAndIsActiveTrue(actionType, now)
if (policy != null) {
val policyType = policy.policyType
@ -146,6 +156,11 @@ class UserActionService(
}
}
@PreDestroy
fun onDestroy() {
coroutineScope.cancel("UserActionService 종료")
}
companion object {
private val logger = LoggerFactory.getLogger(UserActionService::class.java)
}