Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
带有蟑螂B的简单CRUD应用程序中的org.hibernate.TransactionException_Hibernate_Jpa_Spring Boot_Kotlin_Cockroachdb - Fatal编程技术网

带有蟑螂B的简单CRUD应用程序中的org.hibernate.TransactionException

带有蟑螂B的简单CRUD应用程序中的org.hibernate.TransactionException,hibernate,jpa,spring-boot,kotlin,cockroachdb,Hibernate,Jpa,Spring Boot,Kotlin,Cockroachdb,以下使用Spring Boot、Hibernate、JpaRepository、CockroachDB和Kotlin的最小CRUD示例生成org.springframework.orm.jpa.JpaSystemException/org.Hibernate.TransactionException 所讨论的实体对象只有两个字段: @Entity data class Thing ( @Id var id: Long, var value: String ) 为了缩短这

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

所讨论的实体
对象
只有两个字段:

@Entity
data class Thing (
    @Id
    var id: Long,
    var value: String
)
为了缩短这篇文章的篇幅,我将实际的源文件存储在GIST中:

有了这些文件,就可以使用以下命令重现这个问题(在我的例子中是Ubuntu 16.04)

下载并初始化蟑螂数据库:

# 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\u 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
显示更多详细信息:

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
没有并发写入正在进行。所有写入都严格按顺序进行。只有当并发读取起作用时,问题才会发生。但是,我认为这不应导致需要重试任何事务。
我的数据库连接的配置是否有问题,或者可能是什么问题?

HandledRetryableTXN错误表明。当在事务之间检测到冲突时,这会发生在可序列化的隔离级别

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


您可以在上找到多种语言的客户端重试示例,包括。

通过添加
org.springframework.retry
作为依赖项(
org.springframework.retry:spring retry:1.2.2.RELEASE
)导入所需的周年纪念来解决此问题

import org.springframework.retry.annotation.EnableRetry
import org.springframework.retry.annotation.Retryable
替换

@SpringBootApplication


我不明白为什么只进行严格的顺序写入(并发读取)时就需要这样做,但至少它似乎可以工作。

运行jar的用户有写入权限吗?@MikeTung:大多数对数据库的写入都成功。仅当同时发送
GET
请求时,才会发生此错误。即使这样,也只有一些PUTs失败。谢谢。我用一些神奇的注释解决了这个问题,这些注释支持客户端重试。然而,我不明白为什么在我的案例中需要这样做(顺序写入、并发读取)。你知道为什么吗?
@SpringBootApplication
@EnableRetry
@PutMapping("/thing/")
@PutMapping("/thing/")
@Retryable