postgresql - 选择使用已提交读,请澄清

标签 postgresql isolation-level isolation

http://www.postgresql.org/docs/9.1/static/transaction-iso.html 关于读提交隔离级别的文档说

Read Committed is the default isolation level in PostgreSQL. When a transaction uses this isolation level, a SELECT query (without a FOR UPDATE/SHARE clause) sees only data committed before the query began; it never sees either uncommitted data or changes committed during query execution by concurrent transactions. In effect, a SELECT query sees a snapshot of the database as of the instant the query begins to run. However, SELECT does see the effects of previous updates executed within its own transaction, even though they are not yet committed. Also note that two successive SELECT commands can see different data, even though they are within a single transaction, if other transactions commit changes during execution of the first SELECT.

最后一句话让我很困惑,这是什么意思?如果我在一个事务中没有 2 个选择,而是 3 个选择,并且还有一些计算,并且在它们之间没有使用 DML,该怎么办?每个选择都会在每个查询开始时看到它自己的快照?

最佳答案

最后一句就是指这种情况。

假设在两个事务开始之前有一个表 foo 包含一行:

Transaction 1            Transaction 2
-------------------------------------------------------
begin transaction;
select *
from foo;
--> returns 1 rows
                         begin transaction;
                         insert into foo values (2);
                         commit;
select * 
from foo;
--> now returns 2 rows

(请注意,事务1在第一次选择后尚未提交)

如果您不想在事务 1 中看到新的(已提交的)行,则需要使用称为“可重复读”的隔离级别。该名称源于这样一个事实:您可以一次又一次重复相同的查询,并且您将重复看到相同的数据。

关于postgresql - 选择使用已提交读,请澄清,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23441243/

相关文章:

java - htop 和 top 显示进程的多个实例?

ruby-on-rails - 为什么 pg_search 前缀不能像我预期的那样工作?

java - Spring事务隔离不起作用

postgresql - 查询根据时间戳将两个表合并为一个表

postgresql - sqlx postgres 扫描方法失败

java - H2 是否支持可序列化隔离级别?

MySQL:事务隔离级别、死锁

c# - 如何验证 TransactionScope 是否适用于 MySQL?

java - 如何在 Java EE 中隔离用户 session ?