The phenomena which are prohibited at various levels are:
A transaction reads data written by a concurrent uncommitted transaction.
A transaction re-reads data it has previously read and finds that data has been modified by another transaction (that committed since the initial read).
A transaction re-executes a query returning a set of rows that satisfy a search condition and finds that the set of rows satisfying the condition has changed due to another recently-committed transaction.
The result of successfully committing a group of transactions is inconsistent with all possible orderings of running those transactions one at a time.
| Isolation Level | Dirty Read | Nonrepeatable Read | Phantom Read | Serialization Anomaly |
|---|---|---|---|---|
| Read uncommitted | Allowed, but not in PG | Possible | Possible | Possible |
| Read committed | Not possible Possible | Possible | Possible | |
| Repeatable read | Not possible | Not possible | Allowed, but not in PG | Possible |
| Serializable | Not possible | Not possible | Not possible | Not possible |
级别越高,执行效率越低
会发生dirty read(脏读)
公司发工资了,把50000元打到我的账号上,但是该事务并未提交,而我正好去查看账户,看到50000元,非常高兴。但是公司发现金额不对,是2000元,于是回滚事务,修改金额后,将事务提交,最后我实际入账只有2000元。
可以避免dirty read,但会发生nonrepeatable read(不可重复读)
我拿着工资卡去消费,系统读取到卡里有2000元,而此时LP也正好在网上转账,把2000元转到她账户,并在我之前提交了事务,当我扣款时,系统检查到工资卡已经没有钱,扣款失败,十分纳闷,明明卡里有钱
可避免nonrepeatable read,但会发生Phantom read(幻读)
当我拿着工资卡去消费时,一旦系统开始读取工资卡信息(即事务开始),LP就不可能对该记录进行修改,也就是不能在此时转账。
幻读:LP正查询到我当月信用卡的总消费金额select sum(amount) from transaction where month = 本月为80元,而我此时正好在收银台买单,消费1000元,即新增了一条1000元的消费记录insert transaction ...,并提交了事务,随后LP将我的当月信用卡消费的明细打印到A4纸上,却发现消费总额为1080元,很诧异,以为出现了幻觉
避免上述各种状况