Java 不可重复读与脏读的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18297626/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between non-repeatable read vs dirty read
提问by Rollerball
From thisoracle java tutorial:
从这个oracle java教程:
A non-repeatable read occurs when transaction A retrieves a row, transaction B subsequently updates the row, and transaction A later retrieves the same row again. Transaction A retrieves the same row twice but sees different data.
当事务 A 检索一行,事务 B 随后更新该行,事务 A 稍后再次检索同一行时,就会发生不可重复读取。事务 A 检索同一行两次,但看到的数据不同。
What's the difference between a dirty read and the non-repeatable read? Is it not the same thing? reading the wrong result due to others' updating?
脏读和不可重复读有什么区别?这不是一回事吗?由于其他人的更新而读取错误的结果?
Thanks in advance.
提前致谢。
采纳答案by JB Nizet
The exact same page explains what a dirty read is:
完全相同的页面解释了脏读是什么:
Accessing an updated value that has not been committed is considered a dirty read because it is possible for that value to be rolled back to its previous value. If you read a value that is later rolled back, you will have read an invalid value.
访问尚未提交的更新值被视为脏读,因为该值有可能回滚到其先前值。如果您读取了稍后回滚的值,您将读取一个无效值。
So, non-repeatable read consists in reading two different committed values, whereas dirty read consists in reading a value that hasn't been committed yet. Quite different.
因此,不可重复读包括读取两个不同的提交值,而脏读包括读取尚未提交的值。很不一样。
回答by Rahul Tripathi
From here:-
从这里:-
Dirty Readsoccur when one transaction reads data written by another, uncommitted, transaction. The danger with dirty reads is that the other transaction might never commit, leaving the original transaction with "dirty" data.
Non Repeatable Readsoccur when one transaction attempts to access the same data twice and a second transaction modifies the data between the first transaction's read attempts. This may cause the first transaction to read two different values for the same data, causing the original read to be non-repeatable.
当一个事务读取另一个未提交的事务写入的数据时,会发生脏读。脏读的危险在于另一个事务可能永远不会提交,从而使原始事务具有“脏”数据。
当一个事务尝试两次访问相同的数据并且第二个事务在第一个事务的读取尝试之间修改数据时,就会发生不可重复读取。这可能会导致第一个事务读取同一数据的两个不同值,从而导致原始读取不可重复。
回答by Vlad Mihalcea
A picture is worth 1000 words.
一张图片值1000字。
As I explained in this article, in the diagram above, the flow of statements goes like this:
- Alice and Bob start two database transactions.
- Alice modifies the title of a given post record.
- Bob reads the uncommitted post record.
- If Alice commits her transaction, everything is fine. But if Alice rolls back, then Bob will see a record version which no longer exists in the database transaction log.
- Alice 和 Bob 启动了两个数据库事务。
- Alice 修改给定帖子记录的标题。
- Bob 读取未提交的帖子记录。
- 如果 Alice 提交了她的交易,一切都会好起来的。但是如果 Alice 回滚,那么 Bob 将看到数据库事务日志中不再存在的记录版本。
This anomaly is only permitted by the Read Uncommitted isolation level, and, because of the impact on data integrity, most database systems offer a higher default isolation level.
这种异常只有 Read Uncommitted 隔离级别允许,并且由于对数据完整性的影响,大多数数据库系统提供更高的默认隔离级别。
回答by touchstone
i had the same confuse as you do before.
我和你以前有同样的困惑。
after i read the answers in your post, i decide to find out from mysql doc.
在我阅读了您帖子中的答案后,我决定从 mysql doc 中找出答案。
after read the doc from mysql, i think what confuse us is the understading angle. we think that "tran A change one record with no commit, and tran B reads two different data before and after, which indeed a 'dirty data' and 'non-repeatable read'", what we confuse is because we learn it from the result of the two transactions' behavior.
从mysql阅读文档后,我认为让我们困惑的是understading角度。我们认为“tran A 更改了一条记录而没有提交,并且 tran B 前后读取了两个不同的数据,这确实是‘脏数据’和‘不可重复读取’”,我们混淆的是因为我们从两个交易行为的结果。
BUT, the correct angle is: "dirty read"' is a TWO-trasanction thing,whereas "non-repeatable read" is totally a ONE-transaction thing.
但是,正确的角度是:“脏读”是两笔交易,而“不可重复读”完全是一笔交易。
What that means? for exsample, if you are a transaction, and i am a transaction after you. you read a X, and i update it to Y, then you read again.
那是什么意思?例如,如果您是交易,而我是您之后的交易。你读了一个X,我把它更新为Y,然后你又读了一遍。
TO US, you have read a dirty data, because i didn't commit, maybe i want to rollback. i make you read the dity data.
对我们来说,你读到了脏数据,因为我没有提交,也许我想回滚。我让你阅读 dity 数据。
TO YOU youself, in your own transaction, you read two different data, it's a non repeatable data.
对于您自己,在您自己的事务中,您读取了两个不同的数据,这是不可重复的数据。
a little bit verbose. may it helps.
有点冗长。可能有帮助。
refs:
1. https://dev.mysql.com/doc/refman/8.0/en/glossary.html#glos_dirty_read
2. https://dev.mysql.com/doc/refman/8.0/en/glossary.html#glos_non_repeatable_read
参考: 1. https://dev.mysql.com/doc/refman/8.0/en/glossary.html#glos_dirty_read
2. https://dev.mysql.com/doc/refman/8.0/en/glossary.html #glos_non_repeatable_read