Labels
Area\Connection PoolingUse this label to tag issues that apply to problems with connection pool.Repro Available ✔️Issues that are reproducible with repro provided.
Milestone
Description
Describe the bug
On SQL Server, executing two queries inside a common TransactionScope, both are executed using the isolation level defined in the TransactionScope, as expected.
The same does not happen on SQL Azure: the second query is executed with the default Azure isolation level that is "Read Committed Snapshot".
To reproduce
Here the code to reproduce the issue.
class Program
{
const string AzureConnectionString = "Server=tcp:xxxx.database.windows.net,1433;Initial Catalog=xxxx; Persist Security Info=False;User ID=xxx; Password=xxx;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout = 30;";
const string SqlServerConnectionString = "Server=localhost,1433;Initial Catalog=LCMS;Integrated Security=True";
static void Main(string[] args)
{
Console.WriteLine("SQL SERVER");
string connectionString = SqlServerConnectionString;
using (var scope = new TransactionScope(TransactionScopeOption.RequiresNew, new TransactionOptions() { IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted }))
{
TestExec(connectionString); // Expected print: "read uncommitted"
TestExec(connectionString); // Expected print: "read uncommitted"
}
Console.WriteLine("SQL AZURE");
connectionString = AzureConnectionString;
using (var scope = new TransactionScope(TransactionScopeOption.RequiresNew, new TransactionOptions() { IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted }))
{
TestExec(connectionString); // Expected print: "read uncommitted"
TestExec(connectionString); // Expected print: "read uncommitted", Actual: "read committed snapshot"
}
}
static void TestExec(string connectionString)
{
using (var conn = new SqlConnection(connectionString))
{
conn.Open();
var cmd = new SqlCommand()
{
CommandText = "dbcc useroptions",
Connection = conn
};
var reader = cmd.ExecuteReader();
while (reader.Read())
{
if (reader.GetString(0) == "isolation level")
Console.WriteLine(reader.GetString(1));
}
}
}
}Expected behavior
A a new SqlConnection opened inside a TransactionScope must have the same isolation level defined in the TransactionScope.
Further technical details
Additional context
The issue seems related to the connection pooling and the sp_reset_connection, because does not happen using Pooling=No in the connection string.
Reacted by asos-stefanzilik and Thomas Bolon
Metadata
Metadata
Assignees
Labels
Area\Connection PoolingUse this label to tag issues that apply to problems with connection pool.Repro Available ✔️Issues that are reproducible with repro provided.
Type
Projects
- StatusShow more project fieldsDone
Activity
karinazhou commentedon Aug 6, 2019
Hi @csalvaneschi,
Thank you for reporting this. We will look into it.
Is this reproducible for both net core and net framework? And what version of Microsoft.Data.SqlClient are you using? Or are you using System.Data.SqlClient?
ErikEJ commentedon Aug 6, 2019
@karinazhou @David-Engel You should consider an issue template!
ErikEJ commentedon Aug 6, 2019
Sorry, I see now you have one, but the user just ignored it - maybe format better?
chrjs commentedon Aug 6, 2019
@karinazhou found on .NETFramework v4.7.2, System.Data.SqlClient.
karinazhou commentedon Aug 7, 2019
@ErikEJ Thank you for the suggestions.
@chrjs I am able to reproduce this locally. When targeting the Azure DB, the isolation level is not returned as expected for the second query in the same TransactionScope. We will investigate it more.
felipepessoto commentedon Oct 14, 2019
Hi, do you have any update on this?
It seems the same breaking change we had in SQL Server 2014 and that was reverted: https://support.microsoft.com/en-us/help/3025845/fix-the-transaction-isolation-level-is-reset-incorrectly-when-the-sql
felipepessoto commentedon Nov 15, 2019
The sp_reset_connection in SQL Azure is resetting the isolation level, causing this behavior.
It is a breaking change (altought, makes sense to me), but many applications including the .NET and TransactionScope class depends on it.
@ErikEJ @karinazhou
asos-martinsmith commentedon Jun 18, 2023
What is the progress on the fix for this and why is it marked as "low priority"?
We have an application using Snapshot isolation.
It does a read and if a condition is met then does a write.
We are dependent on the documented behaviour
If a snapshot transaction attempts to commit modifications to data that has changed since the transaction began, the transaction will roll back and an error will be raised.
to give us an optimistic concurrency exception - this is broken for us because the isolation level gets reset before the write that would raise this exception so we don't get the expected exception,
EDIT:
Though a workaround is to add an explicit
SET TRANSACTION ISOLATION LEVEL SNAPSHOT
in the relevant SQL
tbolon commentedon Aug 18, 2023
I am surprised this problem is still relevant.
See this code, which simply use a new TransactionScope() without any option:
And the output:
That means thay basically, the transaction scope transaction level is ignored when running more that one connection per TransactionScope (which is the recommended way to code : do not share a connection, instead open and close it when not needed, and expect the pooling will manage them.
I am surprise that all documentation still state that TransactionScope sets a Serializable scope by default and it's never documented that the isolation level is not supported on Azure Sql Db (or buggy because it's only ok for the first SqlConnection opened).
It does not seems that this bug is specific to Microsoft.Data.SqlClient (System.Data.SqlClient has the same behavior), and it should be redirected to the dotnet/runtime repo if TransactionScope or the documentation must be amended.
See also this:
2 remaining items
Add design note comparing issues dotnet#96 and dotnet#146
Add design note comparing issues dotnet#96 and dotnet#146
Move isolation level reset to connection checkout
Tests: restrict TransactionScope reuse guard to on-prem SQL Server
priyankatiwari08 commentedon Sep 17, 2026
/triage
Re-issue session isolation level on TransactionScope re-enlistment (#…
Reset pooled session isolation level before reuse (fixes #96) (#4330)