我们是否需要在客户端添加任何配置以避免 cockroach 数据库中的事务重试错误

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

当同一记录上发生并发更新时,事务 1 能够更新,但根据 cockroach 数据库文档,事务 t2 应该在队列中,但事务 t2 立即失败。我们是否需要添加任何其他配置以使事务在队列中等待,而不是抛出立即重试事务错误。提前致谢

是的,客户端应该处理事务重试错误。请注意,只要有可能,CockroachDB 就会 auto-retry a transaction internally without notifying the client. CockroachDB will only send a serialization error to the client when it cannot resolve the error automatically without client-side intervention. In this case, cockroach provides client side tools to manually or automatically retry the txn. See the docs here.

根据 CockroachDB 文档

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.

我假设您将 Spring Boot 用于您的应用程序,将 Spring Data 用于数据库操作。

如果数据库中有并发更新,在使用sql查询时使用SELECT FOR UPDATE总是好的。同样,@Locks 在 JPA 中做同样的事情。

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

更多信息请参考documentation