In MS SQL Server, how I can unlock the reading lock during a transaction in case of ReadCommitted?

StackOverflow https://stackoverflow.com/questions/22276582

  •  11-06-2023
  •  | 
  •  

Domanda

When I start a transaction in MS SQL Server 2008 and update a table record the SQL server locks other users from reading that record (in case of Isolation level = Read Committed) unless I commit the transaction.

How I can allow other users from reading the original value during my transaction and before I commit it (In case of ReadCommitted)?

For example: if I start a transaction on person table and update person name from "Alex1" to "Alex2", in case of Readcommitted isolation level the other users can not read the record unless I commit the transaction! so how can I allow the other users from reading the original value which is "Alex1" (I don't want them to read the uncommitted values) during my transaction and when I commit it then they can read the committed value which is "Alex2"?

Thanks,

È stato utile?

Soluzione

I found the answer :)

There is a property "Is Read Committed Snapshot On" (It can be set by right click on the database -> options -> Is Read Committed Snapshot On: True). This property should be true, then other users can read the current modifying records (read committed not dirty read), and they can't modify until transaction committed.

Altri suggerimenti

Quoting from the very source about READ COMMITTED TRANSACTION ISOLATION LEVEL:

Specifies that statements cannot read data that has been modified but not committed by other transactions. This prevents dirty reads. Data can be changed by other transactions between individual statements within the current transaction, resulting in nonrepeatable reads or phantom data. This option is the SQL Server default.

The behavior of READ COMMITTED depends on the setting of the READ_COMMITTED_SNAPSHOT database option:

If READ_COMMITTED_SNAPSHOT is set to OFF (the default), the Database Engine uses shared locks to prevent other transactions from modifying rows while the current transaction is running a read operation. The shared locks also block the statement from reading rows modified by other transactions until the other transaction is completed. The shared lock type determines when it will be released. Row locks are released before the next row is processed. Page locks are released when the next page is read, and table locks are released when the statement finishes.

In my interpretation it means that you can still read data with READ COMMITTED during your transaction before it is committed, provided you set the shared lock type to row lock.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top