您的位置:首页 > 数据库

Sql Server Transaction Isolation Level

2017-05-09 18:30 477 查看
并发事务引起的问题

问题

描述

结果

解决

丢失更新

A读—B读—A改—B改

A更改丢失

READ UNCOMMITTED

脏读

A改—B读—A回滚

B读无效值

READ COMMITTED

不可重读

A读—B改—A读

A读不一致

REPEATABLE READ

不可重读

A读—B改—A读

A读不一致

SNAPSHOT

幻读

A读—B增删—A读

A读或多或少

SERIALIZABLE

Transaction isolation level decides how integral and consistent your reading record result is, with higher isolation level, more secured you read the latest version of data, but also it comes with trade off in performance because higher isolation level comes with cost of blocking other transaction that update or insert the data to the same data table you are reading from:

1. Read Uncommitted, you might read the data that changed within the scope of another transaction but later being rolled back.

2. Read Committed, you read the latest data at a cost of wait until the transaction that update the records you read completed.

3. Repeatable Read, you read the data and also block the other transaction that write to the same rows of data until your transaction is committed.

4. Snapshot, you read the data and NOT blocking other transaction that change the same rows of data, at a cost of versioning the data rows you read into a temp db, if there are any change to the same data rows, your transaction will be forced to roll back and need to be re-processed.

5. Serializable, the strictiest isolation level that lock the whole table when you read the data and prevent any insert or deletion of the table rows you are querying.

Transaction deadlock are caused by the transaction that wait for other transaction finish execution has timeout, a chosen victim is picked by the system.

Key to mitigate deadlock is reduce the time for reading and writing data and release the lock on the impacted data rows. adding non-cluster index and optimize the sql statement can be options.

Further reading:

http://stackoverflow.com/questions/4034976/difference-between-read-commit-and-repeatable-read

https://msdn.microsoft.com/en-us/library/tcbchxcb(v=vs.110).aspx

Persistimic lock - Read Committed cannot resolve the issue of NOT ABLE TO REPEATABLE READ

Optimstic lock - leverage a added column for version in data table.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: