gpt4 book ai didi

hibernate - 带有CockroachDB的简单CRUD应用程序中的org.hibernate.TransactionException

转载 作者:行者123 更新时间:2023-12-02 12:42:47 24 4
gpt4 key购买 nike

以下使用Spring Boot,Hibernate,JpaRepository,CockroachDB和Kotlin的最小CRUD示例生成org.springframework.orm.jpa.JpaSystemException / org.hibernate.TransactionException

有问题的Thing实体只有两个字段:

@Entity
data class Thing (
@Id
var id: Long,
var value: String
)

为了简短起见,我将实际的源文件存储在要点中:

./src/main/kotlin/ThingService.kt

./src/main/resources/application.properties

./build.gradle.kts

./stress_test.py

使用这些文件,可以使用以下命令来重现该问题(在我的情况下为Ubuntu 16.04)。

下载并初始化CockroachDB:
# download
wget -qO- https://binaries.cockroachdb.com/cockroach-v1.1.5.linux-amd64.tgz | tar xvz

# start
./cockroach-v1.1.5.linux-amd64/cockroach start --insecure
# leave terminal open in background

# init
cockroach sql --insecure -e "CREATE USER root WITH PASSWORD '123';"
cockroach sql --insecure -e "CREATE DATABASE things_db;"
cockroach sql --insecure -e "GRANT ALL ON DATABASE things_db TO root;"

运行数据服务:
gradle bootRun
# leave terminal open in background

运行压力测试:
python3 stress_test.py
stress_test.py同时向服务发送 PUT请求和 GET请求(按值查找)。大多数请求都可以正常工作,但是在两者之间的输出如下所示:
PUT OK
find OK
PUT OK
find OK
find OK
find OK
PUT ERROR: {"timestamp":"2018-03-17T16:00:24.616+0000","status":500,"error":"Internal Server Error","message":"Unable to commit against JDBC Connection; nested exception is org.hibernate.TransactionException: Unable to commit against JDBC Connection","path":"/thing/"}
find OK
PUT OK

Logs of the Spring application显示更多详细信息:
2018-03-17 17:00:24.615 ERROR 3547 --- [nio-8082-exec-6] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.orm.jpa.JpaSystemException: Unable to commit against JDBC Connection; nested exception is org.hibernate.TransactionException: Unable to commit against JDBC Connection] with root cause

org.postgresql.util.PSQLException: ERROR: restart transaction: HandledRetryableTxnError: TransactionRetryError: retry txn (RETRY_SERIALIZABLE): "sql txn" id=1cb57665 key=/Table/51/1/11125601/0 rw=true pri=0.04354217 iso=SERIALIZABLE stat=PENDING epo=0 ts=1521302424.604752770,1 orig=1521302424.604725980,0 max=1521302424.604725980,0 wto=false rop=false seq=3

没有并发写入。所有写入均严格按顺序进行。仅当并发读取起作用时,才会出现此问题。但是,我认为这不应导致需要重试任何事务。
我的数据库连接的配置是否有问题,或者可能是什么问题?

最佳答案

HandledRetryableTxnError表示transaction should be retried。当在事务之间检测到冲突时,这种情况发生在SERIALIZABLE隔离级别。

蟑螂会自动重试一些,但不是全部重试,需要客户的参与。

您可以在docs site(包括java)上找到多种语言的客户端重试示例。

关于hibernate - 带有CockroachDB的简单CRUD应用程序中的org.hibernate.TransactionException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49339135/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com