From f7a1cfec94818565cbac4e7b315ff1f0744e2afb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EB=8B=A4=EC=9D=80?= Date: Wed, 7 Aug 2024 11:25:34 +0900 Subject: [PATCH] =?UTF-8?q?[#282]=20fix=20:=20=EC=A2=8C=EC=84=9D=20?= =?UTF-8?q?=EC=98=88=EC=95=BD=20=EC=8B=9C=20redisson=20=EC=A0=81=EC=9A=A9?= =?UTF-8?q?=EC=9C=BC=EB=A1=9C=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../global/redisson/DistributeLock.java | 2 +- .../CatchStudy/service/BookingService.java | 18 +++--------------- .../CatchStudy/service/PaymentService.java | 1 + .../CatchStudy/service/RedissonService.java | 18 ++++++++++++++---- 4 files changed, 19 insertions(+), 20 deletions(-) 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)); + } }