Skip to content

Commit

Permalink
feat: use pessimistic locking when retrieving user's specific product…
Browse files Browse the repository at this point in the history
… category
  • Loading branch information
inpink committed Sep 14, 2024
1 parent 5b7ac47 commit ad76014
Showing 1 changed file with 7 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
import app.cardcapture.payment.business.domain.ProductCategory;
import app.cardcapture.payment.business.domain.entity.UserProductCategory;
import app.cardcapture.user.domain.entity.User;
import jakarta.persistence.LockModeType;
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 org.springframework.stereotype.Repository;

import java.util.List;
Expand All @@ -15,5 +19,7 @@ public interface UserProductCategoryRepository extends JpaRepository<UserProduct

boolean existsByUserAndProductCategory(User user, ProductCategory productCategory);

Optional<UserProductCategory> findByUserAndProductCategory(User user, ProductCategory productCategory);
@Lock(LockModeType.PESSIMISTIC_WRITE)
@Query("SELECT upc FROM UserProductCategory upc WHERE upc.user = :user AND upc.productCategory = :productCategory")
Optional<UserProductCategory> findByUserAndProductCategoryWithLock(@Param("user") User user, @Param("productCategory") ProductCategory productCategory);
}

0 comments on commit ad76014

Please sign in to comment.