View Javadoc
1   /*
2    * Copyright (C) 2023 B3Partners B.V.
3    *
4    * SPDX-License-Identifier: MIT
5    */
6   package org.tailormap.api.repository;
7   
8   import jakarta.persistence.LockModeType;
9   import java.util.Optional;
10  import org.springframework.data.jpa.repository.JpaRepository;
11  import org.springframework.data.jpa.repository.Lock;
12  import org.springframework.data.jpa.repository.Query;
13  import org.springframework.data.repository.history.RevisionRepository;
14  import org.springframework.data.repository.query.Param;
15  import org.tailormap.api.persistence.Catalog;
16  import org.tailormap.api.security.annotation.PreAuthorizeAdmin;
17  
18  @PreAuthorizeAdmin
19  public interface CatalogRepository extends JpaRepository<Catalog, String>, RevisionRepository<Catalog, String, Long> {
20    /**
21     * Find a catalog by id with a pessimistic write lock. This will prevent other transactions from modifying the
22     * catalog until the current transaction is complete.
23     *
24     * @param id the id of the catalog
25     * @return an Optional containing the catalog if found, or empty if not found
26     */
27    @Query("SELECT c FROM Catalog c WHERE c.id = :id")
28    @Lock(LockModeType.PESSIMISTIC_WRITE)
29    Optional<Catalog> findByIdWithLock(@Param("id") String id);
30  }