In SQL Server 2005 and 2008, how to tell I'm using pessimistic concurrency model or optimistic one?
-
09-06-2021 - |
Question
I know SQL Server 2000 has a pessimistic concurrency model. And the optimistic model was added in SQL Server 2005. So how do I tell whether I'm using the pessimistic concurrency model or the optimistic one in SQL Server 2005 and 2008?
Thanks.
Solution
SQL 2005 (and 2008) introduces SNAPSHOT issolation. This is the way to move to optimistic concurrency. Take a look to Transaction Isolation and the New Snapshot Isolation Level article:
Isolation level Dirty Reads Non-repeatable Phantom reads Concurrency
reads control
READ UNCOMMITTED Yes Yes Yes Pessimistic
READ COMMITTED No Yes Yes Pessimistic
(with locking)
READ COMMITTED No Yes Yes Optimistic
(with snapshot)
REPEATABLE READ No No Yes Pessimistic
SNAPSHOT No No No Optimistic
SERIALIZABLE No No No Pessimistic
OTHER TIPS
After reading some articles and documents from Microsoft. I got the following conclusion.
On SQL Server 2005+
- If you are using read uncommitted, repeatable read or serializable isolation level, you are using pessimistic concurrency model.
- If you are using snapshot isolation level, you are using optimistic concurrency model.
- If you are using read committed isolation level and the database setting read_committed_snapshot is ON, then you are using optimistic concurrency model
- If you are using read committed isolation level and the database setting read_committed_snapshot is OFF, then you are using pessimistic concurrency model
However, I still need confirmation. Also, If there are some code to test the concurrency model that would be great.
Basically:
Pessimistic: you lock the record only for you until you have finished with it. So read committed transaction isolation level. (not uncommitted as you said)
Optimistic concurrency control works on the assumption that resource conflicts between multiple users are unlikely, and it permits transactions to execute without locking any resources. The resources are checked only when transactions are trying to change data. This determines whether any conflict has occurred (for example, by checking a version number). If a conflict occurs, the application must read the data and try the change again. Optimistic concurrency control is not provided with the product, but you can build it into your application manually by tracking database access. (Source)