简体   繁体   中英

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). 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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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