Understand transactions
A transaction is a series of operations performed as a single logical unit of work. It can be an SQL statement or multiple SQL statements.
Transactions have four features
Atomicity: it cannot be separated.
Consistency: when the transaction is completed, all data must be consistent.
Isolation: independent execution does not interfere with each other. Modifications made by a concurrent firm must be isolated from those made by any other concurrent firm.
Durability: after the service is completed, its impact on the system is permanent. This modification will be maintained even if a system failure occurs.
Application Program The transaction is controlled by specifying the start time and end time of the transaction.
Start transaction: You can use API functions and transact-SQL statements to start transactions explicitly, automatically, or implicitly.
End transaction: You can use a Commit (SUCCESS) or rollback (failure) statement, or use an API function to end the transaction.
Transaction modes include: Display transaction mode, implicit transaction mode, and automatic transaction mode. Commonly Used in SQL is the display mode.
Problems arising from concurrent transaction processing:
1. Update loss
When two or more transactions select the same row and update the row based on the originally selected value, the update will be lost.
Every transaction does not know the existence of other transactions. The last update will overwrite the updates made by other firms, which will lead to data loss.
2. Dirty read
When the second transaction selects another row being updated, unconfirmed correlation issues will occur.
The data being read by the second transaction has not been confirmed and may be changed by the transaction that updates this row.
3. Repeated read is not allowed.
When the second transaction accesses the same row multiple times and reads different data each time, an inconsistent analysis problem occurs.
The inconsistent analysis is similar to the unconfirmed correlation because other transactions are also changing the data being read by the second transaction.
However, in an inconsistent analysis, the data read by the second transaction is committed by a transaction that has been changed. Furthermore, the inconsistent analysis involves reading the same row multiple times (twice or more) and the information is changed by other transactions each time. Therefore, the row is read non-repeatedly.
4. Phantom reading
A phantom reading problem occurs when a row is inserted or deleted and the row belongs to the row being read by a transaction.
The row range for the first read of the transaction shows that one row no longer exists in the second read or subsequent read because the row has been deleted by other transactions. Similarly, due to the insert operation of other transactions, the second or subsequent read of the transaction shows that a row does not exist in the original read.
Transaction isolation level
This isolation level defines the degree to which a transaction must be isolated from other transactions for resource or data changes. Transaction isolation level control:
Whether the lock is used and the requested lock type when reading data.
The time when the lock is read.
Whether to reference the read operations of rows modified by other transactions:
Other transactions are blocked before the exclusive lock on the row is released.
Retrieves the committed version of the row that exists during the start statement or transaction.
Read uncommitted data changes.
Transaction isolation level
SQL statements can use SET transaction isolation level to set the isolation level of transactions.
1. Read uncommitted: the lowest level of transaction isolation only ensures that no illegal data is read during the read process. Appeals may occur in four cases of uncertainty.
2. Read committed: the default transaction level of most mainstream databases, which ensures that one transaction does not read the modified but not committed data of another parallel transaction, avoiding "Dirty reading ". This level applies to most systems.
The first query transaction
SET transaction isolation level read committed
Begin tran
Update Cate set sname = sname + 'B' where id = 1
Select * From cate where id = 1
Waitfor delay '00: 00: 6'
Rollback tran -- roll back the transaction
Select getdate ()
Select * From cate where id = 1
Second query transaction
SET transaction isolation level read committed -- replace committed with read uncommitted to see an example of "Dirty read.
Select * From cate where id = 1
Select getdate ()
We can see that the use of Read committed successfully avoids "Dirty reading ".
3. Repeatable read: ensures that a transaction does not modify the data that has been read by another transaction but has not been committed (rolled back. This avoids "Dirty reading" and "non-repeated reading", but results in more performance losses.
The first query transaction
SET transaction isolation level Repeatable read -- replace Repeatable read with read committed. You can see the example of "non-repeated read ".
Begin tran
Select * From cate where id = 33 -- read data for the first time
Waitfor delay '00: 00: 6'
Select * From cate where id = 33 -- data is read for the second time and cannot be read repeatedly
Commit
Second query transaction
SET transaction isolation level read committed
Update Cate set sname = sname + 'jd 'Where id = 33
Select * From cate where ID> 30
4. serializable: the highest level of transaction isolation. The above three uncertainties will be circumvented. This level will simulate the serial execution of the transaction.
Run the following command in the first query window:
SET transaction isolation level serializable -- replace serializable with Repeatable read to see the "Phantom read" Example
Begin tran
Select * From cate where ID> 30 -- the first time you read data, an example of "Phantom read"
Waitfor delay '00: 00: 6' -- 6 seconds of read Delay
Select * From cate where ID> 30 -- read data for the first time
Commit
Second query transaction
SET transaction isolation level read committed
Delete from Cate where ID> 33
Select * From cate where ID> 30
Create a transaction
SET transaction level: Set transaction isolation level
Start transaction: Begin tran
Commit transaction: commit
Rollback transaction: rollback
Create a transaction save point: save transaction savepoint_name
Rollback to transaction point: rollback transaction savepoint_name
Principles for creating transactions:
It is important to keep the transaction as short as possible. After the transaction starts, the database management system (DBMS) must reserve a lot of resources before the transaction ends to ensure the correct and safe execution of the transaction.
Especially in a large number of concurrent systems, it is more important to keep the transaction brief to reduce the competition for Lock of concurrent resources.
1. transaction processing, prohibiting interaction with users, and completing user input before the transaction starts.
2. Do not open transactions when Browsing data.
3. Keep transactions as short as possible.
4. Use Snapshot isolation for read-only queries to reduce blocking.
5. flexible use of lower transaction isolation levels.
6. flexible use of lower cursor concurrency options, such as open concurrency options.
7. Minimize the amount of data accessed in transactions.