-
Notifications
You must be signed in to change notification settings - Fork 311
Preserve distributed transactions on pooled connection reset #3019
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
9fb2835
Preserve distributed transactions on reset.
mdaigle 6db966b
Reset transacted connections before adding them to the general pool.
mdaigle 00ded0c
Add dropped connection test.
mdaigle a036426
Enable for azure, branch assertions on server type.
mdaigle a3a176d
Fix namespace
mdaigle 2b84482
Rename windows specific test class.
mdaigle 83d9612
Merge branch 'main' of github.com:dotnet/SqlClient into preserve-tran…
mdaigle 73bf7ba
Address review comments. Fix test condition.
mdaigle 8dbf4c5
Make tests windows only. Adjust assertions for local vs azure db.
mdaigle 157abe6
Touch up comments. Add framework specific behavior when connecting wi…
mdaigle 8b5c409
Exclude x86 architecture.
mdaigle 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
181 changes: 181 additions & 0 deletions
181
...ata.SqlClient/tests/ManualTests/SQL/TransactionTest/DistributedTransactionTest.Windows.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,181 @@ | ||
// 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.Runtime.InteropServices; | ||
using System.Threading.Tasks; | ||
using System.Transactions; | ||
using Microsoft.Data.SqlClient.TestUtilities; | ||
using Xunit; | ||
|
||
namespace Microsoft.Data.SqlClient.ManualTesting.Tests | ||
{ | ||
|
||
[PlatformSpecific(TestPlatforms.Windows)] | ||
public class DistributedTransactionTestWindows | ||
{ | ||
|
||
#if NET | ||
private static bool s_DelegatedTransactionCondition => DataTestUtility.AreConnStringsSetup() && DataTestUtility.IsNotAzureServer() && DataTestUtility.IsNotX86Architecture; | ||
|
||
[ConditionalFact(nameof(s_DelegatedTransactionCondition), Timeout = 10000)] | ||
public async Task Delegated_transaction_deadlock_in_SinglePhaseCommit() | ||
{ | ||
TransactionManager.ImplicitDistributedTransactions = true; | ||
using var transaction = new CommittableTransaction(); | ||
|
||
// Uncommenting the following makes the deadlock go away as a workaround. If the transaction is promoted before | ||
// the first SqlClient enlistment, it never goes into the delegated state. | ||
// _ = TransactionInterop.GetTransmitterPropagationToken(transaction); | ||
await using var conn = new SqlConnection(DataTestUtility.TCPConnectionString); | ||
await conn.OpenAsync(); | ||
conn.EnlistTransaction(transaction); | ||
|
||
// Enlisting the transaction in second connection causes the transaction to be promoted. | ||
// After this, the transaction state will be "delegated" (delegated to SQL Server), and the commit below will | ||
// trigger a call to SqlDelegatedTransaction.SinglePhaseCommit. | ||
await using var conn2 = new SqlConnection(DataTestUtility.TCPConnectionString); | ||
await conn2.OpenAsync(); | ||
conn2.EnlistTransaction(transaction); | ||
|
||
// Possible deadlock | ||
transaction.Commit(); | ||
} | ||
#endif | ||
|
||
private static bool s_EnlistedTransactionPreservedWhilePooledCondition => DataTestUtility.AreConnStringsSetup() && DataTestUtility.IsNotX86Architecture; | ||
|
||
[ConditionalFact(nameof(s_EnlistedTransactionPreservedWhilePooledCondition), Timeout = 10000)] | ||
public void Test_EnlistedTransactionPreservedWhilePooled() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Verified that these tests currently fail when run on main against an azure db. |
||
{ | ||
#if NET | ||
TransactionManager.ImplicitDistributedTransactions = true; | ||
#endif | ||
RunTestSet(EnlistedTransactionPreservedWhilePooled); | ||
} | ||
|
||
private void EnlistedTransactionPreservedWhilePooled() | ||
{ | ||
Exception commandException = null; | ||
Exception transactionException = null; | ||
|
||
try | ||
{ | ||
using (TransactionScope txScope = new TransactionScope(TransactionScopeOption.Required, TimeSpan.MaxValue)) | ||
{ | ||
// Leave first connection open so that the transaction is promoted | ||
SqlConnection rootConnection = new SqlConnection(ConnectionString); | ||
rootConnection.Open(); | ||
using (SqlCommand command = rootConnection.CreateCommand()) | ||
{ | ||
command.CommandText = $"INSERT INTO {TestTableName} VALUES ({InputCol1}, '{InputCol2}')"; | ||
command.ExecuteNonQuery(); | ||
} | ||
|
||
// Closing and reopening cycles the connection through the pool. | ||
// We want to verify that the transaction state is preserved through this cycle. | ||
SqlConnection enlistedConnection = new SqlConnection(ConnectionString); | ||
enlistedConnection.Open(); | ||
enlistedConnection.Close(); | ||
enlistedConnection.Open(); | ||
|
||
// Forcibly kill the root connection to mimic gateway's behavior when using the proxy connection policy | ||
// https://techcommunity.microsoft.com/blog/azuredbsupport/azure-sql-database-idle-sessions-are-killed-after-about-30-minutes-when-proxy-co/3268601 | ||
// Can also represent a general server-side, process failure | ||
KillProcess(rootConnection.ServerProcessId); | ||
|
||
|
||
using (SqlCommand command = enlistedConnection.CreateCommand()) | ||
{ | ||
command.CommandText = $"INSERT INTO {TestTableName} VALUES ({InputCol1}, '{InputCol2}')"; | ||
try | ||
{ | ||
command.ExecuteNonQuery(); | ||
} | ||
catch (Exception ex) | ||
{ | ||
commandException = ex; | ||
} | ||
} | ||
|
||
txScope.Complete(); | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
transactionException = ex; | ||
} | ||
|
||
if (Utils.IsAzureSqlServer(new SqlConnectionStringBuilder((ConnectionString)).DataSource)) | ||
{ | ||
// Even if an application swallows the command exception, completing the transaction should indicate that it failed. | ||
Assert.IsType<TransactionInDoubtException>(transactionException); | ||
// See https://learn.microsoft.com/en-us/sql/relational-databases/errors-events/database-engine-events-and-errors-3000-to-3999?view=sql-server-ver16 | ||
// Error 3971 corresponds to "The server failed to resume the transaction." | ||
Assert.Equal(3971, ((SqlException)commandException).Number); | ||
} | ||
else | ||
{ | ||
Assert.IsType<TransactionAbortedException>(transactionException); | ||
|
||
#if NETFRAMEWORK | ||
// See https://learn.microsoft.com/en-us/sql/relational-databases/errors-events/database-engine-events-and-errors-8000-to-8999?view=sql-server-ver16 | ||
// The distributed transaction failed | ||
Assert.Equal(8525, ((SqlException)commandException).Number); | ||
#else | ||
Assert.IsType<InvalidOperationException>(commandException); | ||
#endif | ||
} | ||
|
||
// Verify that nothing made it into the database | ||
DataTable result = DataTestUtility.RunQuery(ConnectionString, $"select col2 from {TestTableName} where col1 = {InputCol1}"); | ||
Assert.True(result.Rows.Count == 0); | ||
} | ||
|
||
private void KillProcess(int serverProcessId) | ||
{ | ||
using (TransactionScope txScope = new TransactionScope(TransactionScopeOption.Suppress)) | ||
{ | ||
using (SqlConnection connection = new SqlConnection(ConnectionString)) | ||
{ | ||
connection.Open(); | ||
using (SqlCommand command = connection.CreateCommand()) | ||
{ | ||
command.CommandText = $"KILL {serverProcessId}"; | ||
command.ExecuteNonQuery(); | ||
} | ||
} | ||
txScope.Complete(); | ||
} | ||
} | ||
|
||
private static string TestTableName; | ||
private static string ConnectionString; | ||
private const int InputCol1 = 1; | ||
private const string InputCol2 = "One"; | ||
|
||
private static void RunTestSet(Action TestCase) | ||
{ | ||
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(DataTestUtility.TCPConnectionString); | ||
|
||
builder.Pooling = true; | ||
builder.MaxPoolSize = 5; | ||
builder.Enlist = true; | ||
ConnectionString = builder.ConnectionString; | ||
|
||
TestTableName = DataTestUtility.GenerateObjectName(); | ||
DataTestUtility.RunNonQuery(ConnectionString, $"create table {TestTableName} (col1 int, col2 text)"); | ||
try | ||
{ | ||
TestCase(); | ||
} | ||
finally | ||
{ | ||
DataTestUtility.RunNonQuery(ConnectionString, $"drop table {TestTableName}"); | ||
} | ||
} | ||
} | ||
} | ||
|
46 changes: 0 additions & 46 deletions
46
...rosoft.Data.SqlClient/tests/ManualTests/SQL/TransactionTest/DistributedTransactionTest.cs
This file was deleted.
Oops, something went wrong.
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.
Leaving Is2000 logic in place for now until we remove support for it. The fix will not apply to versions before 2000.