Skip to content

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy
SQL

SQL Server: Why Two Sessions Can See Different Data Inside What Looks Like the Same Transaction

- 27.09.26 - ErcanOPAK

🗄️ The Two Sessions That Disagreed About What the Database Actually Said

Two connections querying the exact same table at nearly the same moment can legitimately see different data – not due to a bug in either query, but because the database’s isolation level governs exactly what a transaction is allowed to see of another transaction’s uncommitted or concurrently-changing work, and the default isolation level (READ COMMITTED) still allows a fair amount of variation between what two overlapping transactions observe, especially across multiple statements within the same transaction.

🔎 The Problem

-- Session A, inside a transaction, reads the same row twice:
BEGIN TRAN;
SELECT Status FROM Orders WHERE OrderId = 501;   -- Returns '"'"'Pending'"'"'
-- ... some other work happens here ...
SELECT Status FROM Orders WHERE OrderId = 501;   -- Returns '"'"'Shipped'"'"'!
COMMIT;

-- Session B, running concurrently, committed an UPDATE to that exact
-- row BETWEEN Session A'"'"'s two reads. Under READ COMMITTED (SQL Server'"'"'s
-- default), this is completely expected and correct behavior - each
-- individual read sees whatever was committed at the moment it ran,
-- with no guarantee that two reads within the same transaction see a
-- consistent snapshot of the data.

✅ Fix: Choose the Isolation Level That Matches What Consistency You Actually Need

  • REPEATABLE READ (or SNAPSHOT isolation, which avoids some of REPEATABLE READ’s locking cost) guarantees that a value read once within a transaction reads the same on a second read within that same transaction – the right choice specifically when a transaction’s logic depends on a value not changing out from under it mid-transaction.
  • SNAPSHOT isolation specifically (enabled at the database level, then requested per-transaction) gives each transaction a consistent point-in-time view of the whole database for its entire duration, without the same blocking behavior REPEATABLE READ’s locks introduce – often the better fit for read-heavy reporting-style transactions that need consistency without wanting to block writers.
  • For the specific, common case of ‘read a value, then act based on it, and nobody else should be able to change it in between,’ an explicit UPDLOCK/HOLDLOCK hint (or just wrapping the read-then-write in a properly isolated transaction to begin with) is worth reaching for directly, rather than assuming the default isolation level provides that guarantee on its own.

⚠️ Why READ COMMITTED’s Behavior Surprises People

  • ‘Read committed’ sounds like it should mean ‘a consistent, committed view of the database’ – and it does guarantee that anything READ was genuinely committed at the time it was read, but it makes no promise at all that two separate reads within the same transaction will see the same committed data, since a competing transaction is free to commit a change in between them.
  • This is a very reasonable thing to overlook because most individual queries genuinely don’t care about this distinction – the bug only becomes visible in a transaction whose logic specifically depends on a value staying the same across more than one read within its own boundaries, which is a narrower and easier-to-miss scenario than it initially sounds.

READ COMMITTED promises that what you read was true the instant you read it – it never once promised it would still be true the next time you asked.

— Database Engineer

Related posts:

SQL: Use Indexes to Boost Query Performance

SQL: Understand JOIN Types — INNER, LEFT, RIGHT, FULL

How to use OUTPUT for Insert, Update and Delete in SQL

Post Views: 3

Post navigation

C#: Fix a Deadlock Caused by Mixing lock and await in the Same Method
The AI Prompt That Finds Feature Flags Nobody Remembers to Remove

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

October 2026
M T W T F S S
 1234
567891011
12131415161718
19202122232425
262728293031  
« Sep    

Most Viewed Posts

  • Get the User Name and Domain Name from an Email Address in SQL (973)
  • How to make theater mode the default for Youtube (950)
  • How to add default value for Entity Framework migrations for DateTime and Bool (939)
  • Get the First and Last Word from a String or Sentence in SQL (847)
  • How to select distinct rows in a datatable in C# (836)
  • How to enable, disable and check if Service Broker is enabled on a database in SQL Server (624)
  • Add Constraint to SQL Table to ensure email contains @ (590)
  • Average of all values in a column that are not zero in SQL (553)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (526)
  • Find numbers with more than two decimal places in SQL (468)

Recent Posts

  • CSS: Fix a prefers-color-scheme Media Query That Gets Silently Overridden by a Browser Extension’s Forced Dark Mode
  • Git: Fix a Merge Commit That Silently Drops a File Because Both Branches Deleted It Differently
  • HTML5: Fix a Native Lazy-Loading Image That Never Loads Because It Sits Inside a Hidden Tab Until the User Clicks It
  • The AI Prompt That Traces a Null Reference Exception Back to the Exact Line That First Produced the Null
  • The AI Prompt That Turns a Gym Membership Contract’s Fine Print Into a Plain-English List of Cancellation Steps
  • Photoshop: Fix a Color Profile Mismatch That Makes Printed Output Look Nothing Like What You Saw On Screen
  • WordPress: Fix Search Results That Return Pages From a Theme You Deactivated Months Ago
  • Visual Studio: Fix a Test Project That Builds Fine Alone but Fails to Discover Any Tests After a NuGet Restore
  • ASP.NET Core: Fix a File Upload That Times Out on Slow Connections Only Because Kestrel’s Minimum Data Rate Feature Kicked In
  • JavaScript: Fix an Array Destructuring Default Value That Silently Never Applies Because null Was Passed Instead of Undefined

Most Viewed Posts

  • Get the User Name and Domain Name from an Email Address in SQL (973)
  • How to make theater mode the default for Youtube (950)
  • How to add default value for Entity Framework migrations for DateTime and Bool (939)
  • Get the First and Last Word from a String or Sentence in SQL (847)
  • How to select distinct rows in a datatable in C# (836)

Recent Posts

  • CSS: Fix a prefers-color-scheme Media Query That Gets Silently Overridden by a Browser Extension’s Forced Dark Mode
  • Git: Fix a Merge Commit That Silently Drops a File Because Both Branches Deleted It Differently
  • HTML5: Fix a Native Lazy-Loading Image That Never Loads Because It Sits Inside a Hidden Tab Until the User Clicks It
  • The AI Prompt That Traces a Null Reference Exception Back to the Exact Line That First Produced the Null
  • The AI Prompt That Turns a Gym Membership Contract’s Fine Print Into a Plain-English List of Cancellation Steps

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com