TransactionScope with IsolationLevel set to Serializable is locking all SQL SELECTs

*爱你&永不变心* 提交于 2019-12-25 04:55:26

问题


I'm using PowerShell transactions; which create a CommittableTransaction with an IsolationLevel of Serializable. The problem is that when I am executing a Transaction in this context all SELECTs are blocked on the tables affected by the transaction on any connection besides the one executing the transaction. I can perform gets from within the transaction but not anywhere else. This includes SSMS and other cmdlets executions. Is this expected behavior? Seems like I'm missing something...

PS Script:

Start-Transaction
Add-Something -UseTransaction 
Get-Something #hangs here until timeout
Add-Something -UseTransaction
Undo-Transaction

回答1:


Serializable transactions will block any updates on the ranges scanned under this isolation. By itself the serialization isolation level does not block reads. If you find that reads are blocked, something else must be at play and it depends on what you do in those scripts.




回答2:


Sounds as if your database has ALLOW_SNAPSHOT_ISOLATION=OFF. This setting controls the concurrency mechanism used by the database:

  • ALLOW_SNAPSHOT_ISOLATION=OFF: This is the traditional mode of SQL Server, with lock based concurrency. This mode may lead to locking problems.

  • ALLOW_SNAPSHOT_ISOLATION=ON: This is avaliable since SQL Server 2005, and uses MVCC, pretty similar to what Oracle or Postgresql do. This is better for concurrency as readers do not block writers and writers do not block readers.

Note that this two modes do not behave in the same way, so you must code your transactions for assuming one mode or the other.



来源:https://stackoverflow.com/questions/3628620/transactionscope-with-isolationlevel-set-to-serializable-is-locking-all-sql-sele

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!