Search code examples
spring-bootspring-data-jpaisolation-levelcockroachdb

Do we need to add any configuration on client side to avoid transaction retry errors in cockroach database


when there is concurrent updates happening on the same record, transaction 1 is able to update but as per the cockroach database documentation transaction t2 should be on queue, but transaction t2 is immediately failing. Do we need to add any other configuration to make transaction wait in queue instead of it throwing an immedaite retry transaction error. Thanks in advance


Solution

  • As per CockroachDB Documentation

    CockroachDB always uses SERIALIZABLE isolation, which is the strongest of the four transaction isolation levels defined by the SQL standard and is stronger than the SNAPSHOT isolation level developed later. SERIALIZABLE isolation guarantees that even though transactions may execute in parallel, the result is the same as if they had executed one at a time, without any concurrency.

    I am assuming you are using Spring Boot for you application and Spring Data for Database operations.

    If there are concurrent updates in the database, it's always good to use SELECT FOR UPDATE when you are using sql query. Similarly, @Locks which does the same in JPA.

    @Lock(LockModeType.PESSIMISTIC_READ)
    @QueryHints({@QueryHint(name = "javax.persistence.lock.timeout", value = "3000")})
    public Optional<Employee> findById(Long employeeId);
    

    For more information you can refer documentation.