Search code examples
javaspring-bootspring-data-jdbcpessimistic-locking

Pessimistic Locking in Spring Data JDBC


In my SeatDAO I declare a findAll method with derived query. The DAO looks like this:

public interface SeatDAO extends CrudRepository<SeatDTO, Integer> {

    @Lock(LockMode.PESSIMISTIC_READ)
    Collection<SeatDTO> findAll();

}

When I now start the spring boot application along the PostgreSQL database, it gives me these errors:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'seatDAO' defined in demo.adapters.database.SeatDAO defined in @EnableJdbcRepositories declared on JdbcRepositoriesRegistrar.EnableJdbcRepositoriesConfiguration: Could not create query for public abstract java.util.Collection demo.adapters.database.SeatDAO.findAll(); Reason: No property 'findAll' found for type 'SeatDTO'

Caused by: org.springframework.data.repository.query.QueryCreationException: Could not create query for public abstract java.util.Collection demo.adapters.database.SeatDAO.findAll(); Reason: No property 'findAll' found for type 'SeatDTO'

Caused by: org.springframework.data.mapping.PropertyReferenceException: No property 'findAll' found for type 'SeatDTO'

When I leave out pessimistic locking on findAll by dropping @Lock, the application runs without any problems.

What is the problem here and how can I solve this?

Thank you in advance.


Solution

  • The core issue lies in the @Lock(LockMode.PESSIMISTIC_READ) annotation being incorrectly applied to the findAll() method. Spring Data JPA's @Lock annotation is intended for locking individual entity instances, not for locking entire query results. The framework is attempting to interpret findAll() as a property of the SeatDTO entity, leading to the

    "No property 'findAll' found" error.

    Remove the @Lock annotation from the findAll() method:

    public interface SeatDAO extends CrudRepository<SeatDTO, Integer> {
    
        Collection<SeatDTO> findAll();
    
    }
    

    Alternatives for Pessimistic Locking:

    Lock individual entities within a transaction:

    Java - Inside a transactional context

    for (SeatDTO seat : seatDAO.findAll()) {
        // Lock each seat individually
         transactionManager.lock(seat, LockMode.PESSIMISTIC_READ);
        // Perform operations on the locked seat
    }