SQL Server transactions: insert causes locks?

◇◆丶佛笑我妖孽 提交于 2019-12-23 05:04:23

问题


I'm having the following issue: I'm inserting a row to table (say, 'temp') inside a transaction (default ADO.NET transaction). Until that transaction is committed, the command 'select * from temp', running from another session (SQL Server 2008 management studio), is blocked and cannot be completed.

This seems weird to me. Shouldn't the read command return all the previous table rows (everything but the newly inserted row) and not get blocked?


回答1:


The default behaviour in SQL Server is to take locks, and these will not be released until the transaction is committed.

There are 3 main ways around this - the READPAST hint (which will skip the locked row(s)), the READ UNCOMMITTED isolation level (or NOLOCK hint) (which will return all rows, including the newly inserted one(s), or the SNAPSHOT isolation level - but for snapshot to work, this needs to be enabled at the server level



来源:https://stackoverflow.com/questions/3099682/sql-server-transactions-insert-causes-locks

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