Skip to content

Commit

Permalink
fix : DB 병목 개선 - FindingGameWinner에 데이터 넣을 때 NativeQuery사용 (CC-176) (#47
Browse files Browse the repository at this point in the history
)

* fix : DB 병목 개선 - 프록시 객체 사용 (CC-176)

* fix : FindingGameWinner에 데이터 넣을 때 NativeQuery사용 (CC-176)

* fix : 스케쥴링 시간 수정 (CC-176)

* fix : 사용하지 않는 import 제거 (CC-176)

* fix : 트랜잭션 에러 처리 수정 (CC-176)
  • Loading branch information
putdata authored Aug 22, 2024
1 parent 411e3d9 commit da18f6d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
package ai.softeer.caecae.findinggame.repository;

import ai.softeer.caecae.findinggame.domain.entity.FindingGameWinner;
import io.lettuce.core.dynamic.annotation.Param;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

@Repository
public interface FindingGameWinnerRepository extends JpaRepository<FindingGameWinner, Integer> {
@Modifying
@Query(value = """
INSERT IGNORE INTO finding_game_winner (user_id, finding_game_id)
VALUES (:userId, :gameId)
""", nativeQuery = true)
void insertWinner(@Param("userId") Integer userId, @Param("gameId") Integer gameId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@
import ai.softeer.caecae.findinggame.repository.FindingGameDbRepository;
import ai.softeer.caecae.findinggame.repository.FindingGameRedisRepository;
import ai.softeer.caecae.findinggame.repository.FindingGameWinnerRepository;
import ai.softeer.caecae.racinggame.repository.RacingGameRepository;
import ai.softeer.caecae.user.domain.entity.User;
import ai.softeer.caecae.user.repository.UserRepository;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

Expand All @@ -30,7 +28,7 @@ public class FindingGameScheduleService {
private final int TRANSACTION_COUNT = 100;

@Transactional
@Scheduled(cron = "*/2 * * * * *")
@Scheduled(cron = "*/1 * * * * *")
public void insertWinnerToDatabaseScheduler() {
int completed = 0;
List<FindingGameWinner> winners = new ArrayList<>();
Expand All @@ -39,21 +37,27 @@ public void insertWinnerToDatabaseScheduler() {
if (realWinner == null) break;
Integer gameId = realWinner.getGameId();
String phone = realWinner.getPhone();
FindingGameWinner winner = FindingGameWinner.builder()
.user(userRepository.findByPhone(phone).orElseGet(() -> userRepository.save(
User.builder()
.phone(phone)
.build()
)))
.findingGame(findingGameDbRepository.findById(gameId).get())
.build();
winners.add(winner);
try {
insertWinner(gameId, phone);
} catch (Exception e) {
log.error("선착순 인원 처리 에러 - gameId:{}, phone:{}, 처리 완료된 건: {}", gameId, phone, completed);
return;
}
findingGameRedisRepository.popRealWinner();
completed++;
}
findingGameWinnerRepository.saveAll(winners);
if (completed > 0) {
log.info("선착순 인원 스케쥴링 처리 완료 - {} 건", completed);
}
}

@Transactional(Transactional.TxType.REQUIRES_NEW)
protected void insertWinner(Integer gameId, String phone) {
Integer userId = userRepository.findByPhone(phone).orElseGet(() -> userRepository.save(
User.builder()
.phone(phone)
.build()
)).getId();
findingGameWinnerRepository.insertWinner(userId, gameId);
}
}

0 comments on commit da18f6d

Please sign in to comment.