Click here to monitor SSC

John Magnabosco

SQL Server Development and Data Security

Less Than Desirable Reads

Published Wednesday, October 14, 2009 5:44 AM

Once again, firing up Mr. Peabody's WABAC Machine unearthed a valuable blog entry that I posted before my Simple-Talk blogging days. The post below was originally published on March 2008 and maintains its relevance. Enjoy!

I was watching a very interesting movie quite a few months ago. In this movie, the primary character hopped into his sports car in the middle of the day, backed out of his driveway and proceeded to travel down a deserted street in the middle of New York City. I have never visited the "Big Apple" but I would suspect that this occurrence would be quite an anomaly.

It would be reasonable to assume that having only a singular transaction occur within a SQL Server database would be an equivalent anomaly to the one presented in the movie previously mentioned. To this, the delicate balance between data concurrency and data consistency begins.

Transaction isolation level in stored procedures is the traffic cop on the street of transactions. They prevent those nasty collisions that backup the flow of data. While a detailed description of all transaction isolation levels is another blog entry, I wanted to explore the Evil-Read Trio that can occur if careful consideration of these transaction isolation levels does not occur.

THE DIRTY READ: These are not the ones that are found in the foot locker of your neighborhood teenager. These are transactions that read uncommitted data modifications that are made by another transaction. This can wreak havoc if aggregated calculations occur while transactions are being added or modified and especially of these modifications are rolled back. Using the READ COMMITTED isolation level will prevent the occurrences of dirty reads.

THE NON-REPEATABLE READ: Complex stored procedures may execute a SELECT statement more than once within the same transaction. If another transaction modifies the data prior to the completion of the first transaction different results may occur when the second occurrence of the SELECT statement is run. The utilization of REPEATABLE READ isolation level will prevent the occurrences of non-repeatable reads as well as dirty reads.

THE PHANTOM READ: Much like a non-repeatable read, this type of read depends upon a multiple execution of a SELECT statement within the same transaction and another transaction modifying the data in which the SELECT statement is querying. In the case of the phantom read the modification of the data is an INSERT or DELETE which causes the row in question to appear and/or disappear within the reading transaction. While Ghostbusters may not have a good solution for this, the use of SERIALIZABLE READ isolation level will prevent phantom reads as well as non-repeatable and dirty reads.

The question may arise to how it can be determined that these types of reads are occurring. The ol' handy-dandy SQL Server Profiler can be employed to expose these reads. Include the SQL:StmtStarting , SQL:StmtCompleted, SQL:BatchStarting and SQL:BatchCompleted event classes in
the trace to catch the occurrence.

by Johnm

Comments

No Comments
You need to sign in to comment on this blog

About Johnm

John Magnabosco manages the Data Services Group at one of the fastest growing companies in the United States. He is also a Co-Founder of the Indianapolis Professional Association for SQL Server (IndyPASS), Co-Founder of IndyTechFest, the author of the book titled "Protecting SQL Server Data" and contributing author of "SQL Server MVP Deep Dives Volume 2".
<October 2009>
SuMoTuWeThFrSa
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567
How to Kill a Company in One Step or Save it in Three
 The majority of companies that suffer a major data loss subsequently go out of business. Wesley David... Read more...

Migrating from OCS 2007 R2 to Lync: Part 4
 Having migrated the rest of our users and legacy resources across and started getting ready to... Read more...

Automated Script-generation with Powershell and SMO
 In the first of a series of articles on automating the process of building, modifying and copying SQL... Read more...

Seth Godin: Big in the IT Business
 Seth Godin has transformed our understanding of marketing in IT. He invented the concept of 'permission... Read more...

Using SQL Test Database Unit Testing with TeamCity Continuous Integration
 With database applications, the process of test and integration can be frustratingly slow because so... Read more...