DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
11 Monitoring and Observability Tools for 2023
Learn more
  1. DZone
  2. Data Engineering
  3. Databases
  4. Optimistic Locking

Optimistic Locking

Joydeep Das user avatar by
Joydeep Das
·
Jan. 14, 15 · Interview
Like (0)
Save
Tweet
Share
6.42K Views

Join the DZone community and get the full member experience.

Join For Free

Introduction


We all know about Isolation level of MS SQL Server. But there are certain confusion related to Optimistic Locking technique. This article is for the advance SQL Developer to learn and understand Optimistic Locking system. If you are new in Isolation level please read my previous article named Isolation Level of MS SQL Server. Hope it will be knowledgeable and helpful. 

How to Find Current Isolation Level

To find the current Isolation level at your database use this SQL Statement:

 SELECT CASE transaction_isolation_level
                  WHEN 0 THEN 'Unspecified'
                  WHEN 1 THEN 'ReadUncommitted'
                  WHEN 2 THEN 'ReadCommitted'
                  WHEN 3 THEN 'Repeatable'
                  WHEN 4 THEN 'Serializable'
                  WHEN 5 THEN 'Snapshot' END AS TRANSACTION_ISOLATION_LEVEL
FROM  sys.dm_exec_sessions
WHERE session_id = @@SPID;

Problem with Locking

As we all know that to read data from database (SELECT…) the Shared Lock is established and when we perform the DML operation or Writing something in Database we used Exclusive Lock. Both Shared and Exclusive lock are not compatible to each other that means When we Write something we cannot Read or When we Read we cannot Write.

Off course we can do the Dirty Read by Isolation Level READ UNCOMMITED or WITH(NOLOCK) operation hint. But it is Not at all recommended and all the developers know why?

So What the Solutions

From MS SQL Server 2005 Microsoft provide us two type of Optimistic Lock to get this solution.

1.  SNAPSHOT

2.  READ COMMITED SNAPSHOT

Both the isolation level uses the row version technique and use the TempDB Databse.

The benefits that we get from this Isolation level is Read do not Block Write and Write do not Block Read in case of OLTP environment.

Implementing Optimistic Locking

To implement optimistic locking we have to plan it well others suffer big performance problems, loss of availability, and incorrect query results.

So we discuss it by our old fashion called step by step process

Seep-1 [ Our Production Environment can Handel this Load ]                                           

When we change the Isolation level to SNAPSHOT, MS SQL Server waits for running Transaction to complete and starts version controlling for data modification. It starts using extra 14 bytes per row in a table for that.

With this version are created in tempDB databse to hold the previous version, so we need a big size tempDB database to maintain the version control by Snapshot Isolation level.

Another aspect is Log Version chain making query so slow.

We have to monitor our system for that. We can use SQL server performance counter for that.

Step-2 [ We have to Choose ]

We have to choose carefully between Snapshot and Read Committed Snapshot.

To implement the SNAPSHOT isolation level, we have to Implemented in Database first like this.

 ALTER DATABASE IsolationTests 
SET ALLOW_SNAPSHOT_ISOLATION ON

Then we have to use the Snapshot isolation level by Transaction wise like this:

 SET TRANSACTION ISOLATION LEVEL SNAPSHOT
BEGIN TRAN
.....
.....

So we can use the Snapshot Isolation with other Isolation level and were it needed we can use it by putting a single line SQL statement SET TRANSACTION ISOLATION LEVEL SNAPSHOT. In other wards we can say that Snapshot Isolation level can be set by session specific.

On other hand READ COMMITED SNAPSHOT only takes Database level settings like this.

 ALTER DATABASE IsolationTests 
SET READ_COMMITTED_SNAPSHOT ON
No other statement is required and when it execute in database level it going to change the default Isolation level from every query. This may give us incorrect results depend on how we write our existing query.

Hope you like it.

Database Isolation (database systems) Microsoft SQL Server sql Snapshot (computer storage)

Published at DZone with permission of Joydeep Das, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • UUID: Coordination-Free Unique Keys
  • 10 Most Popular Frameworks for Building RESTful APIs
  • Test Execution Tutorial: A Comprehensive Guide With Examples and Best Practices
  • FIFO vs. LIFO: Which Queueing Strategy Is Better for Availability and Latency?

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: