diff --git a/src/main/java/com/example/CatchStudy/global/redisson/DistributeLock.java b/src/main/java/com/example/CatchStudy/global/redisson/DistributeLock.java index f1f6d3d..3e9ad2f 100644 --- a/src/main/java/com/example/CatchStudy/global/redisson/DistributeLock.java +++ b/src/main/java/com/example/CatchStudy/global/redisson/DistributeLock.java @@ -15,7 +15,7 @@ TimeUnit timeUnit() default TimeUnit.SECONDS; - long waitTime() default 6L; //락 획득 대기 시간 + long waitTime() default 0L; //락 획득 대기 시간 long leaseTime() default 5L; //락 임대 시간 } diff --git a/src/main/java/com/example/CatchStudy/service/BookingService.java b/src/main/java/com/example/CatchStudy/service/BookingService.java index 698a4b2..574a1af 100644 --- a/src/main/java/com/example/CatchStudy/service/BookingService.java +++ b/src/main/java/com/example/CatchStudy/service/BookingService.java @@ -56,24 +56,12 @@ public class BookingService { private final RedissonService redissonService; - @Transactional(propagation = Propagation.REQUIRES_NEW) + @Transactional public Long saveSeatBooking(SeatBookingDto dto,Users user,StudyCafe studyCafe){ Payment payment = null; + checkAvailableSeatsTime(studyCafe,dto.getTime()); + payment = redissonService.saveSeatBooking(dto,dto.getSeatId(),user,studyCafe); - checkAvailableSeatsTime(studyCafe, dto.getTime()); - - try { - Seat seat = seatRepository.findBySeatIdOptimisticLock(dto.getSeatId()).orElseThrow(() -> new CatchStudyException(ErrorCode.SEAT_NOT_FOUND)); - if(!seat.getIsAvailable()){ - throw new CatchStudyException(ErrorCode.BOOKING_NOT_AVAILABLE); - } - seat.updateSeatStatus(false); - Booking booking = bookingRepository.save(Booking.of(dto.getTime(), user, studyCafe, seat)); - payment =paymentRepository.save(Payment.of(dto.getPaymentType(), booking, PaymentStatus.ready)); - - }catch (ObjectOptimisticLockingFailureException e){ - throw new CatchStudyException(ErrorCode.BOOKING_NOT_AVAILABLE); - } return payment.getPaymentId(); } diff --git a/src/main/java/com/example/CatchStudy/service/PaymentService.java b/src/main/java/com/example/CatchStudy/service/PaymentService.java index 94e1c4d..5c86a16 100644 --- a/src/main/java/com/example/CatchStudy/service/PaymentService.java +++ b/src/main/java/com/example/CatchStudy/service/PaymentService.java @@ -116,6 +116,7 @@ public KakaoApproveResponseDto kakaoPayApprove(String pgToken, Long userId, Long try { quartzSchedulerService.scheduleBookingSeatStatusCheck(booking.getBookingId(), payment); //30 분 후 좌석 상태 확인 작업 스케줄; } catch (SchedulerException e) { + log.error(e.getMessage()); throw new CatchStudyException(ErrorCode.QUARTZ_SCHEDULER_ERROR); } diff --git a/src/main/java/com/example/CatchStudy/service/RedissonService.java b/src/main/java/com/example/CatchStudy/service/RedissonService.java index 1180858..8e67ab2 100644 --- a/src/main/java/com/example/CatchStudy/service/RedissonService.java +++ b/src/main/java/com/example/CatchStudy/service/RedissonService.java @@ -6,10 +6,7 @@ import com.example.CatchStudy.global.exception.CatchStudyException; import com.example.CatchStudy.global.exception.ErrorCode; import com.example.CatchStudy.global.redisson.DistributeLock; -import com.example.CatchStudy.repository.BookedRoomInfoRepository; -import com.example.CatchStudy.repository.BookingRepository; -import com.example.CatchStudy.repository.PaymentRepository; -import com.example.CatchStudy.repository.RoomRepository; +import com.example.CatchStudy.repository.*; import lombok.RequiredArgsConstructor; import org.redisson.api.RedissonClient; import org.springframework.stereotype.Service; @@ -28,6 +25,8 @@ public class RedissonService { private final BookedRoomInfoRepository bookedRoomInfoRepository; private final BookingRepository bookingRepository; private final PaymentRepository paymentRepository; + private final SeatRepository seatRepository; + @DistributeLock(lockName = "roomLock",identifier = "roomId") public Payment saveRoomBooking(SeatBookingDto dto, Long roomId, Users user){ @@ -57,4 +56,15 @@ public Payment saveRoomBooking(SeatBookingDto dto, Long roomId, Users user){ Booking booking = bookingRepository.save(Booking.of(user,time,room.getStudyCafe(),bookedRoomInfo,bookingStartTime,bookedEndTime)); return paymentRepository.save(Payment.of(dto.getPaymentType(), booking, PaymentStatus.ready)); } + + @DistributeLock(lockName = "seatLock",identifier = "seatId") + public Payment saveSeatBooking(SeatBookingDto dto, Long seatId, Users user,StudyCafe studyCafe){ + Seat seat = seatRepository.findBySeatId(dto.getSeatId()).orElseThrow(() -> new CatchStudyException(ErrorCode.SEAT_NOT_FOUND)); + if(!seat.getIsAvailable()){ + throw new CatchStudyException(ErrorCode.BOOKING_NOT_AVAILABLE); + } + seat.updateSeatStatus(false); + Booking booking = bookingRepository.save(Booking.of(dto.getTime(), user, studyCafe, seat)); + return paymentRepository.save(Payment.of(dto.getPaymentType(), booking, PaymentStatus.ready)); + } }