-
Notifications
You must be signed in to change notification settings - Fork 329
Reset session isolation level on pool return (fixes #96) #4330
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
priyankatiwari08
wants to merge
3
commits into
dotnet:main
Choose a base branch
from
priyankatiwari08:feature/sqltransaction-isolation-leak
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+249
−0
Draft
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
144 changes: 144 additions & 0 deletions
144
src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/TransactionTest/IsolationLevelLeakTest.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| // See the LICENSE file in the project root for more information. | ||
|
|
||
| using System; | ||
| using System.Data; | ||
| using System.Transactions; | ||
| using Microsoft.Data.SqlClient.Tests.Common; | ||
| using Xunit; | ||
|
Comment on lines
+5
to
+9
|
||
| using IsolationLevel = System.Data.IsolationLevel; | ||
|
|
||
| namespace Microsoft.Data.SqlClient.ManualTesting.Tests | ||
| { | ||
| // SqlTransaction / TransactionScope used to leave the pooled connection | ||
| // with the elevated session isolation level. The next user of the pooled | ||
| // connection would silently inherit it. The fix resets the session | ||
| // isolation level to READ COMMITTED when the connection is returned to | ||
| // the pool. | ||
| public static class IsolationLevelLeakTest | ||
| { | ||
| private const string GetIsoSql = @" | ||
| SELECT CASE transaction_isolation_level | ||
| WHEN 0 THEN 'Unspecified' | ||
| WHEN 1 THEN 'ReadUncommitted' | ||
| WHEN 2 THEN 'ReadCommitted' | ||
| WHEN 3 THEN 'RepeatableRead' | ||
| WHEN 4 THEN 'Serializable' | ||
| WHEN 5 THEN 'Snapshot' | ||
| END | ||
| FROM sys.dm_exec_sessions WHERE session_id = @@SPID;"; | ||
|
|
||
| private static string BuildPooledConnString(string appName) => | ||
| new SqlConnectionStringBuilder(DataTestUtility.TCPConnectionString) | ||
| { | ||
| Pooling = true, | ||
| MaxPoolSize = 1, | ||
| MultipleActiveResultSets = false, | ||
| Enlist = true, | ||
| ApplicationName = appName | ||
| }.ConnectionString; | ||
|
|
||
| private static int GetSpid(SqlConnection c) | ||
| { | ||
| using SqlCommand cmd = new SqlCommand("SELECT @@SPID;", c); | ||
| return Convert.ToInt32(cmd.ExecuteScalar()); | ||
| } | ||
|
|
||
| private static string GetIso(SqlConnection c) | ||
| { | ||
| using SqlCommand cmd = new SqlCommand(GetIsoSql, c); | ||
| return (string)cmd.ExecuteScalar(); | ||
| } | ||
|
|
||
| [ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup))] | ||
| public static void SqlTransaction_SerializableDoesNotLeakAcrossPool() | ||
| { | ||
| string cs = BuildPooledConnString("IsoLeakTest-SqlTx"); | ||
| int spid1; | ||
| using (SqlConnection c = new SqlConnection(cs)) | ||
| { | ||
| c.Open(); | ||
| spid1 = GetSpid(c); | ||
| using SqlTransaction tx = c.BeginTransaction(IsolationLevel.Serializable); | ||
| Assert.Equal("Serializable", GetIsoOnTx(c, tx)); | ||
| tx.Rollback(); | ||
| } | ||
|
|
||
| using (SqlConnection c = new SqlConnection(cs)) | ||
| { | ||
| c.Open(); | ||
| Assert.Equal(spid1, GetSpid(c)); // pool reuse | ||
| Assert.Equal("ReadCommitted", GetIso(c)); | ||
| } | ||
| } | ||
|
|
||
| [ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup))] | ||
| public static void TransactionScope_SerializableDoesNotLeakAcrossPool() | ||
| { | ||
| string cs = BuildPooledConnString("IsoLeakTest-TxScope"); | ||
| int spid1; | ||
| using (var scope = new TransactionScope( | ||
| TransactionScopeOption.RequiresNew, | ||
| new TransactionOptions { IsolationLevel = System.Transactions.IsolationLevel.Serializable }, | ||
| TransactionScopeAsyncFlowOption.Enabled)) | ||
| using (SqlConnection c = new SqlConnection(cs)) | ||
| { | ||
| c.Open(); | ||
| spid1 = GetSpid(c); | ||
| Assert.Equal("Serializable", GetIso(c)); | ||
| scope.Complete(); | ||
| } | ||
|
|
||
| using (SqlConnection c = new SqlConnection(cs)) | ||
| { | ||
| c.Open(); | ||
| Assert.Equal(spid1, GetSpid(c)); | ||
| Assert.Equal("ReadCommitted", GetIso(c)); | ||
| } | ||
| } | ||
|
|
||
| // Negative test: legacy switch ON brings the old leak back. | ||
| // Uses LocalAppContextSwitchesHelper to force the cached switch value | ||
| // in LocalAppContextSwitches (production code reads the cache, not | ||
| // AppContext, after first use). The helper restores the previous | ||
| // value on dispose. | ||
| [ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup))] | ||
| public static void LegacySwitch_PreservesOldLeakBehavior() | ||
| { | ||
| using LocalAppContextSwitchesHelper switchesHelper = new(); | ||
| switchesHelper.UseLegacyIsolationLevelBehavior = true; | ||
|
|
||
| // Distinct app name so this test gets its own pool group. | ||
| string cs = BuildPooledConnString("IsoLeakTest-Legacy"); | ||
| try | ||
| { | ||
| int spid1; | ||
| using (SqlConnection c = new SqlConnection(cs)) | ||
| { | ||
| c.Open(); | ||
| spid1 = GetSpid(c); | ||
| using SqlTransaction tx = c.BeginTransaction(IsolationLevel.Serializable); | ||
| tx.Rollback(); | ||
| } | ||
|
|
||
| using (SqlConnection c = new SqlConnection(cs)) | ||
| { | ||
| c.Open(); | ||
| Assert.Equal(spid1, GetSpid(c)); | ||
| Assert.Equal("Serializable", GetIso(c)); // legacy: leaks | ||
| } | ||
| } | ||
| finally | ||
| { | ||
| SqlConnection.ClearAllPools(); | ||
| } | ||
|
Comment on lines
+138
to
+141
|
||
| } | ||
|
|
||
| private static string GetIsoOnTx(SqlConnection c, SqlTransaction tx) | ||
| { | ||
| using SqlCommand cmd = new SqlCommand(GetIsoSql, c, tx); | ||
| return (string)cmd.ExecuteScalar(); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's worth noting that Azure Synapse Analytics does things slightly differently - it only supports the
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTEDstatement, and the default isolation level can vary based upon a database option.I think this might also present an issue for #4335.