ACID
Atomic
all statements in the transaction either completed successfully or they were all rolled back. The task that the set of operations represents is either accomplished or not, but in any case not left half done.
Consistent
All data touched by the transaction is left in a logically consistent state. For example, if stock available numbers are decremented from product-table, then, there has to be a related entry in sales table. The inventory can't disappear.
Isolated
The transaction must affect data without interfering with other concurrent transactions, or being interfered with by them. This prevents transactions from making changes to data based on uncommitted information, for example changes to a record that are subsequently rolled back. Most databases use locking to maintain transaction isolation.
Durable
Once a change is made, it is permanent. If a system error or power failure occurs before a set of commands is complete, those commands are undone and the data is restored to its original state once the system begins running again.
isolation level
definition
Controls the locking and row versioning behavior of Transact-SQL statements issued by a connection to SQL Server.
syntax
SET TRANSACTION ISOLATION LEVEL
{ READ UNCOMMITTED
| READ COMMITTED
| REPEATABLE READ
| SNAPSHOT
| SERIALIZABLE
}
READ UNCOMMITTED
It is the least restrictive of the isolation levels.
Specifies that statements can read rows that have been modified by other transactions but not yet committed.
READ UNCOMMITTED level do not issue shared locks to prevent other transactions from modifying data read by the current transaction.
READ UNCOMMITTED transactions are also not blocked by exclusive locks that would prevent the current transaction from reading rows that have been modified but not committed by other transactions.
When this option is set, it is possible to read uncommitted modifications, which are called dirty reads. Values in the data can be changed and rows can appear or disappear in the data set before the end of the transaction. This option has the same effect as setting NOLOCK on all tables in all SELECT statements in a transaction.
dirty read
t1 更新数据
t2可以查看这个表格,而且是更新后的数据
t1 rollback之后,看的数据就是之前的了,这就导致t2看的不是正确的数据
READ COMMITTED
- definition
It is the default isolation level.
It specifies that statements cannot read data that has been modified but not committed by other transactions. This prevents dirty reads
Data can be changed by other transactions between individual statements within the current transaction, resulting in non-repeatable reads or phantom data.
t1 更新table1,没有commit,t2查看table1,看不了
read committed snapshot
REPEATABLE READ
SNAPSHOT ISOLATION
SERIALIZABLE
problem
Dirty Read – A Dirty read is the situation when a transaction reads a data that has not yet been committed. For example, Let’s say transaction 1 updates a row and leaves it uncommitted, meanwhile, Transaction 2 reads the updated row. If transaction 1 rolls back the change, transaction 2 will have read data that is considered never to have existed.
Non Repeatable read – Non Repeatable read occurs when a transaction reads same row twice, and get a different value each time. For example, suppose transaction T1 reads data. Due to concurrency, another transaction T2 updates the same data and commit, Now if transaction T1 rereads the same data, it will retrieve a different value.
Phantom Read – Phantom Read occurs when two same queries are executed, but the rows retrieved by the two, are different. For example, suppose transaction T1 retrieves a set of rows that satisfy some search criteria. Now, Transaction T2 generates some new rows that match the search criteria for transaction T1. If transaction T1 re-executes the statement that reads the rows, it gets a different set of rows this time.
lost update