-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revise: Override the findById method in BookRepository & Add Pessimis…
…tic Write lock
- Loading branch information
1 parent
7659d95
commit 8d0255b
Showing
1 changed file
with
17 additions
and
0 deletions.
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
src/main/java/com/example/demo/repository/BookRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,27 @@ | ||
package com.example.demo.repository; | ||
|
||
import com.example.demo.model.Book; | ||
import jakarta.annotation.Nonnull; | ||
import jakarta.persistence.LockModeType; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.jpa.repository.Lock; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import java.util.Optional; | ||
|
||
@Repository | ||
public interface BookRepository extends JpaRepository<Book, String> { | ||
|
||
/** | ||
* <p>Retrieves a Book entity by its unique identifier while applying | ||
* a pessimistic write lock on the database record to prevent concurrent updates.</p> | ||
* <p>Note: A pessimistic write lock ensures that only one transaction can write to | ||
* this specific database record at a time, preventing conflicts from concurrent updates. | ||
* It helps maintain data integrity but may introduce slight performance overhead.</p> | ||
* @param id The unique identifier of the Book. | ||
* @return An Optional containing the Book entity if found, or an empty Optional if not found. | ||
*/ | ||
@Lock(LockModeType.PESSIMISTIC_WRITE) | ||
@Nonnull // Added to suppress warning | ||
Optional<Book> findById(@Nonnull String id); | ||
} |