gpt4 book ai didi

java - Postgresql 提交的数据不可见

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:09:33 25 4
gpt4 key购买 nike

我注意到我的应用程序中有奇怪的行为。提交后提交的数据似乎不可见。算法看起来像这样:

connection1 - insert into table row with id = 5
connection1 - commit, close
connection2 - open
connection2 - select from table row with id = 5 (no results)
connection2 - insert into table row with id = 5 (PRIMARY KEY VIOLATION, result is in db)

如果 connection2 上的选择没有返回任何结果,那么我会插入,否则它会更新。服务器有很多数据库(~200),看起来提交已经完成,但稍后会在数据库中进行更改。我使用 java 和 jdbc。任何想法将不胜感激。

最佳答案

此行为对应于 REPEATABLE READ 隔离模式,参见 SET TRANSACTION :

REPEATABLE READ
All statements of the current transaction can only see rows committed before the first query or data-modification statement was executed in this transaction.

尝试 connection.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED) 看看它是否有所作为。

关于java - Postgresql 提交的数据不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15829933/

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