transaction ISOLATION Level transaction_isolation
I. Questions raised
The following problems occur when manipulating a database
Update missing (Lost update)
All two transactions update one row of data at the same time, but the second transaction fails to exit halfway, resulting in the failure of two changes to the data. This is because the system does not perform any lock operations, so concurrent transactions are not isolated.
Dirty Read (Dirty reads)
A transaction started reading a row of data, but another transaction has updated the data but has not been able to submit it in time. This is quite dangerous because it is possible that all operations are rolled back.
Non-repeatable read (non-repeatable reads)
A transaction repeatedly reads the same row of data two times, but it gets different results. It consists of the following:
(1) Transaction T1 after reading a data, the transaction T2 modified it to get a different value from the previous one when the transaction T1 read the data again.
(2) Phantom reading (Phantom reads): Transactions are queried two times during the operation, and the results of the second query include data that did not appear in the first query or missing data from the first query (this does not require the same SQL statement for the two queries). This is due to the fact that another transaction was inserted into the data during the two-time query.
Ii. Solutions
In the standard SQL specification, 4 transaction isolation levels are defined to resolve the above issues
Unauthorized read, (READ UNCOMMITTED):
Dirty reads are allowed, but updates are not allowed to be lost. If a transaction has started to write data, another data does not allow write operations at the same time, but allows other transactions to read the row data. The isolation level can be implemented through exclusive write locks.
Authorized Read (Read Committed):
Non-repeatable reads are allowed, but dirty reads are not allowed. This can be achieved by "instantaneous shared read lock" and "exclusive write lock". Transactions that read data allow other transactions to continue accessing the row data, but uncommitted write transactions will prevent other transactions from accessing the row.
REPEATABLE READ (Repeatable Read):
Non-repeatable read and dirty reads are prohibited, but phantom data can sometimes occur. This can be achieved through shared read locks and exclusive write locks. Transactions that read data will prohibit write transactions (but read transactions are allowed), and write transactions prohibit any other transaction.
Serialization (Serializable)
Provides strict transaction isolation. It requires transaction serialization execution, which can only be performed one after another, but not concurrently. If transaction serialization is not possible only through row-level locks, additional mechanisms must be used to ensure that the newly inserted data is not accessed by the transaction that just performed the query operation.
Third, Summary:
The higher the isolation level, the more secure the integrity and consistency of the data, but the greater the impact on concurrency performance. For most applications, it is a priority to set the isolation level of the database system to read committed. It can avoid dirty reading and has better concurrency performance. Although it can result in the concurrency problems of non repeatable read, virtual read, and second class loss updates, the application can be controlled by pessimistic locks or optimistic locks on individual occasions where such problems may occur.