Home >Database >Mysql Tutorial >What is dirty reading?
What is a dirty read?
Dirty reading, also known as the reading of invalid data, means that during database access, transaction T1 modifies a certain value, and then transaction T2 reads the value, and then T1 cancels the modification for some reason. The modification of this value causes the data read by T2 to be invalid. It is worth noting that dirty reading is generally targeted at update operations.
Solution
MySQL database defines four isolation levels:
serializable: can avoid dirty reads, non-repeatable reads, and virtual reads.
repeatable read: It can avoid dirty reads and non-repeatable reads.
read committed: Dirty reads can be avoided.
read uncommitted: lowest level, will happen.
Note: The isolation level of a transaction is restricted by the database, and the isolation levels supported by different databases are not necessarily the same.
Dirty read: Modification When adding an exclusive lock, it will not be released until the transaction is committed. After adding a shared lock when reading (so that other transactions will not modify the data while transaction 1 reads the data), no transaction is allowed to operate on the data. It can only be read. If there is an update operation in transaction 1, it will be converted into an exclusive lock, and other transactions will not have the right to participate in reading and writing. This prevents dirty read problems
But when transaction 1 reads the data, , it is possible that other transactions have also read the data. After the reading is completed, the shared lock is released. At this time, transaction 1 modifies the data and commits the transaction after the modification. When other transactions read the data again and find that the data is inconsistent, a non-repeatable error will occur. Read problems, so this cannot avoid non-repeatable read problems
When executing different isolation levels, a variety of different problems may occur. The following summarizes them and illustrates them with examples:
Dirty reading occurs when one transaction A reads data that has been modified by another transaction B but has not yet been committed. If B rolls back, transaction A reads invalid data. This is similar to a non-repeatable read, but the second transaction does not need to commit.
Recommended tutorials: "PHP Tutorial" "MySQL Tutorial"
The above is the detailed content of What is dirty reading?. For more information, please follow other related articles on the PHP Chinese website!