Skip to content
This repository has been archived by the owner on Jan 28, 2023. It is now read-only.

Commit

Permalink
#52 lockmode - 기본, pessimistic 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
Hyune-c committed Jun 18, 2022
1 parent 93f4d77 commit e2da222
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit e2da222

Please sign in to comment.