Skip to content

Commit

Permalink
Revise: Override the findById method in BookRepository & Add Pessimis…
Browse files Browse the repository at this point in the history
…tic Write lock
  • Loading branch information
AhmetAksunger committed Sep 16, 2023
1 parent 7659d95 commit 8d0255b
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/main/java/com/example/demo/repository/BookRepository.java
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);
}

0 comments on commit 8d0255b

Please sign in to comment.