From e2da22206177a835e5ee44b90e27886983fcd585 Mon Sep 17 00:00:00 2001 From: hyune Date: Sun, 19 Jun 2022 04:36:57 +0900 Subject: [PATCH] =?UTF-8?q?#52=20lockmode=20-=20=EA=B8=B0=EB=B3=B8,=20pess?= =?UTF-8?q?imistic=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../vote/adapter/in/EventVoteController.kt | 3 ++ .../event/vote/domain/EventVoteService.kt | 34 ++++++++++++++++++- 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/module-api/src/main/kotlin/com/example/event/vote/adapter/in/EventVoteController.kt b/module-api/src/main/kotlin/com/example/event/vote/adapter/in/EventVoteController.kt index fff2e84..2b7f8c5 100644 --- a/module-api/src/main/kotlin/com/example/event/vote/adapter/in/EventVoteController.kt +++ b/module-api/src/main/kotlin/com/example/event/vote/adapter/in/EventVoteController.kt @@ -16,8 +16,11 @@ class EventVoteController( @PostMapping(path = ["/api/event/vote"]) fun vote(@Valid @RequestBody req: EventVoteRequest) { when (req.lockmode) { + LockModeType.READ -> eventVoteService.readVote(req.id) + LockModeType.WRITE -> eventVoteService.writeVote(req.id) LockModeType.PESSIMISTIC_WRITE -> eventVoteService.pessimisticWriteVote(req.id) LockModeType.PESSIMISTIC_READ -> eventVoteService.pessimisticReadVote(req.id) + LockModeType.PESSIMISTIC_FORCE_INCREMENT -> eventVoteService.pessimisticForceIncrementVote(req.id) else -> eventVoteService.vote(req.id) } } diff --git a/module-api/src/main/kotlin/com/example/event/vote/domain/EventVoteService.kt b/module-api/src/main/kotlin/com/example/event/vote/domain/EventVoteService.kt index 3e8e2bf..8af10dd 100644 --- a/module-api/src/main/kotlin/com/example/event/vote/domain/EventVoteService.kt +++ b/module-api/src/main/kotlin/com/example/event/vote/domain/EventVoteService.kt @@ -1,6 +1,7 @@ package com.example.event.vote.domain import com.example.event.vote.adapter.out.EventVoteRepository +import org.slf4j.Logger import org.springframework.data.jpa.repository.Lock import org.springframework.stereotype.Service import org.springframework.transaction.annotation.Transactional @@ -9,11 +10,31 @@ import javax.persistence.LockModeType @Service class EventVoteService( - private val eventVoteRepository: EventVoteRepository + private val eventVoteRepository: EventVoteRepository, + private val log: Logger ) { @Transactional fun vote(id: UUID) { + log.info("### NONE") + val eventVoteEntity = eventVoteRepository.findById(id).orElse(EventVoteEntity(id = id)) + eventVoteEntity.addCount() + eventVoteRepository.save(eventVoteEntity) + } + + @Transactional + @Lock(value = LockModeType.READ) + fun readVote(id: UUID) { + log.info("### READ") + val eventVoteEntity = eventVoteRepository.findById(id).orElse(EventVoteEntity(id = id)) + eventVoteEntity.addCount() + eventVoteRepository.save(eventVoteEntity) + } + + @Transactional + @Lock(value = LockModeType.WRITE) + fun writeVote(id: UUID) { + log.info("### WRITE") val eventVoteEntity = eventVoteRepository.findById(id).orElse(EventVoteEntity(id = id)) eventVoteEntity.addCount() eventVoteRepository.save(eventVoteEntity) @@ -22,6 +43,7 @@ class EventVoteService( @Transactional @Lock(value = LockModeType.PESSIMISTIC_WRITE) fun pessimisticWriteVote(id: UUID) { + log.info("### PESSIMISTIC_WRITE") val eventVoteEntity = eventVoteRepository.findById(id).orElse(EventVoteEntity(id = id)) eventVoteEntity.addCount() eventVoteRepository.save(eventVoteEntity) @@ -30,6 +52,16 @@ class EventVoteService( @Transactional @Lock(value = LockModeType.PESSIMISTIC_READ) fun pessimisticReadVote(id: UUID) { + log.info("### PESSIMISTIC_READ") + val eventVoteEntity = eventVoteRepository.findById(id).orElse(EventVoteEntity(id = id)) + eventVoteEntity.addCount() + eventVoteRepository.save(eventVoteEntity) + } + + @Transactional + @Lock(value = LockModeType.PESSIMISTIC_FORCE_INCREMENT) + fun pessimisticForceIncrementVote(id: UUID) { + log.info("### PESSIMISTIC_FORCE_INCREMENT") val eventVoteEntity = eventVoteRepository.findById(id).orElse(EventVoteEntity(id = id)) eventVoteEntity.addCount() eventVoteRepository.save(eventVoteEntity)