Skip to content

Commit

Permalink
feat: 비관적 lock으로 이용권 동시성 문제 해결
Browse files Browse the repository at this point in the history
  • Loading branch information
GGHDMS committed Oct 19, 2024
1 parent afc38c3 commit da54313
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,7 @@ interface UserRepository : JpaRepository<User, Long> {
fun findByCode(code: String): User?

fun findTop15ByOrderByCreatedAtDescIdDesc(): List<User>

@Query("SELECT * FROM users WHERE users.o_auth_name= :oauthName FOR UPDATE", nativeQuery = true)
fun findByOauthNameWithLock(oauthName: String): User?
}
22 changes: 12 additions & 10 deletions src/main/kotlin/com/yourssu/ssudateserver/service/SSUDateService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ class SSUDateService(
private val followRepository: FollowRepository,
) {
@Transactional
fun recentSearch(): List<SearchResponseDto> {
return userRepository.findTop15ByOrderByCreatedAtDescIdDesc()
fun recentSearch(): List<SearchResponseDto> =
userRepository
.findTop15ByOrderByCreatedAtDescIdDesc()
.map { user ->
SearchResponseDto(
animals = user.animals,
Expand All @@ -32,14 +33,14 @@ class SSUDateService(
weight = user.weight,
)
}
}

fun search(
gender: Gender,
animals: Animals,
): List<SearchResponseDto> {
return if (animals == Animals.ALL) {
userRepository.getRandomUserWithGender(gender.toString())
): List<SearchResponseDto> =
if (animals == Animals.ALL) {
userRepository
.getRandomUserWithGender(gender.toString())
.map { user ->
SearchResponseDto(
animals = user.animals,
Expand All @@ -51,7 +52,8 @@ class SSUDateService(
)
}
} else {
userRepository.getRandomUserWithGenderAndAnimals(gender.toString(), animals.toString())
userRepository
.getRandomUserWithGenderAndAnimals(gender.toString(), animals.toString())
.map { user ->
SearchResponseDto(
animals = user.animals,
Expand All @@ -63,15 +65,15 @@ class SSUDateService(
)
}
}
}

fun searchContact(oauthName: String): List<SearchContactResponseDto> {
val user =
userRepository.findByOauthName(oauthName) ?: throw UserNotFoundException("해당 oauthName인 유저가 없습니다.")

val toUserIdList: List<Long> = followRepository.findAllByFromUserId(user.id!!).map { it.toUserId }

return userRepository.findAllByIdIn(toUserIdList)
return userRepository
.findAllByIdIn(toUserIdList)
.map {
SearchContactResponseDto(
animals = it.animals,
Expand All @@ -91,7 +93,7 @@ class SSUDateService(
nickName: String,
): ContactResponseDto {
val fromUser =
userRepository.findByOauthName(oauthName) ?: throw UserNotFoundException("해당 oauthName인 유저가 없습니다.")
userRepository.findByOauthNameWithLock(oauthName) ?: throw UserNotFoundException("해당 oauthName인 유저가 없습니다.")

if (fromUser.nickName == nickName) {
throw SelfContactException("본인의 프로필은 조회할 수 없어요.")
Expand Down

0 comments on commit da54313

Please sign in to comment.