Skip to content

Commit

Permalink
Merge pull request #284 from fourix4/dev
Browse files Browse the repository at this point in the history
좌석 예약 시 redisson 적용으로 변경
  • Loading branch information
llynn97 authored Aug 7, 2024
2 parents 54d270e + f01a23d commit 17eda7b
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

TimeUnit timeUnit() default TimeUnit.SECONDS;

long waitTime() default 6L; //락 획득 대기 시간
long waitTime() default 0L; //락 획득 대기 시간

long leaseTime() default 5L; //락 임대 시간
}
18 changes: 3 additions & 15 deletions src/main/java/com/example/CatchStudy/service/BookingService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
18 changes: 14 additions & 4 deletions src/main/java/com/example/CatchStudy/service/RedissonService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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){
Expand Down Expand Up @@ -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));
}
}

0 comments on commit 17eda7b

Please sign in to comment.