From Wikipedia(其中有很棒的和详细的例子):
A non-repeatable read occurs, when during the course of a transaction, a row is retrieved twice and the values within the row differ between reads.
和
A phantom read occurs when, in the course of a transaction, two identical queries are executed, and the collection of rows returned by the second query is different from the first.
简单示例:
>用户A运行同一个查询两次。
>在其间,用户B运行事务并提交。
>不可重复读:用户A已查询的A行第二次有不同的值。
>幻像读取:查询中的所有行之前和之后都具有相同的值,但正在选择不同的行(因为B已删除或插入一些行)。示例:从表中选择sum(x)将返回不同的结果,即使没有受影响的行本身已更新,如果行已添加或删除。
In the above example,which isolation level to be used?
您需要什么隔离级别取决于您的应用程序。 “更好”的隔离级别(例如降低的并发性)具有高成本。
在您的示例中,您不会有幻像读取,因为您只选择单个行(通过主键标识)。你可以有不可重复的读取,所以如果这是一个问题,你可能想要有一个隔离级别,以防止。在Oracle中,事务A也可以发出SELECT FOR UPDATE,然后事务B不能更改行,直到A完成。