diff --git a/src/main/java/kusitms/gallae/repository/program/ProgramRespository.java b/src/main/java/kusitms/gallae/repository/program/ProgramRespository.java index f586c88..e9c8a8f 100644 --- a/src/main/java/kusitms/gallae/repository/program/ProgramRespository.java +++ b/src/main/java/kusitms/gallae/repository/program/ProgramRespository.java @@ -1,9 +1,13 @@ package kusitms.gallae.repository.program; +import jakarta.persistence.LockModeType; import kusitms.gallae.domain.Program; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Lock; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; import java.util.List; import java.util.Optional; @@ -12,13 +16,9 @@ public interface ProgramRespository extends JpaRepository { @Override Optional findById(Long programId); - Page findAllByProgramTypeOrderByCreatedAtDesc(String programType , Pageable pageable); - - Page findProgramByProgramNameContaining(String programName, Pageable pageable); - - List findTop4ByOrderByCreatedAtDesc(); - - List findTop4ByOrderByProgramLikeDesc(); + @Lock(LockModeType.PESSIMISTIC_WRITE) + @Query("select p from Program p where p.id = :programId") + Optional findWithIdForUpdate(@Param("programId") Long programId); Program findByUserIdAndStatus(Long id, Program.ProgramStatus programStatus); diff --git a/src/main/java/kusitms/gallae/service/favorite/FavoriteServiceImpl.java b/src/main/java/kusitms/gallae/service/favorite/FavoriteServiceImpl.java index 8a1efd0..9cad426 100644 --- a/src/main/java/kusitms/gallae/service/favorite/FavoriteServiceImpl.java +++ b/src/main/java/kusitms/gallae/service/favorite/FavoriteServiceImpl.java @@ -57,7 +57,7 @@ public class FavoriteServiceImpl implements FavoriteService { @Override public void postFavorite(String username, Long programId) { User user = userRepository.findById(Long.valueOf(username)).get(); - Program program = programRespository.findById(programId).get(); + Program program = programRespository.findWithIdForUpdate(programId).get(); Favorite favorite = favoriteRepository.findByUserAndProgram(user,program).orElse(null); if(favorite == null ) { program.setProgramLike(program.getProgramLike() + 1);