简体   繁体   English

SQL Server事务:插入会导致锁定?

[英]SQL Server transactions: insert causes locks?

I'm having the following issue: I'm inserting a row to table (say, 'temp') inside a transaction (default ADO.NET transaction). 我遇到以下问题:我在事务(默认的ADO.NET事务)内的表(例如'temp')中插入一行。 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. 在提交该事务之前,从另一个会话(SQL Server 2008 Management Studio)运行的命令'select * from temp'被阻止并且无法完成。

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? 读取命令是否应该返回所有先前的表行(除了新插入的行以外的所有行),并且不被阻塞?

The default behaviour in SQL Server is to take locks, and these will not be released until the transaction is committed. SQL Server中的默认行为是采取锁定,并且只有在提交事务后才会释放这些锁定。

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 有3种主要解决方法-READPAST提示 (将跳过锁定的行), READ UNCOMMITTED隔离级别(或NOLOCK提示)(将返回所有行,包括新插入的行),或SNAPSHOT隔离级别 -但是要使快照正常工作,需要在服务器级别启用它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM