开发者

Do DB locks require transactions?

开发者 https://www.devze.com 2022-12-19 10:55 出处:网络
Is it true that \"Every statement (select/insert/delete/update) has an isolation level regardless of transactions\"?

Is it true that "Every statement (select/insert/delete/update) has an isolation level regardless of transactions"?

I have a scenario in which I have set update of statements inside a tr开发者_如何学Pythonansaction (ReadCommitted). And another set not in a transaction (select statements).

  1. In this case when first set is executing another waits.
  2. If I set READ_COMMITTED_SNAPSHOT for DB Deadlock occurs.

    ALTER DATABASE Amelio SET ALLOW_SNAPSHOT_ISOLATION ON
    ALTER DATABASE Amelio SET READ_COMMITTED_SNAPSHOT ON
    

To solve this problem, do I need to put "Select" statements in TransactionScope?


On SQL Server every transaction has an implicit or explicit transaction level. Explicit if called with BEGIN/COMMIT/ROLLBACK TRANSACTION, implicit if nothing like this is issued.

Start your snapshot before the update query starts. Otherwise you give SQL Server no chance to prepare the changed rows into tempdb and the Update query still has the lock open.

Another way without creating a snapshot isolation is to use SELECT <columns> FROM <table> WITH (NOLOCK) which is the way to tell SQL Server to get the rows no matter what (aka READ_UNCOMMITED). As it is a query hint it changes the isolation level even with your settings. Can work if you are not bothered which state of the row is queried - however caution needs to be used when evaluating the data received.

0

精彩评论

暂无评论...
验证码 换一张
取 消