|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using System; |
| 6 | +using System.Data; |
| 7 | +using System.Runtime.InteropServices; |
| 8 | +using System.Threading.Tasks; |
| 9 | +using System.Transactions; |
| 10 | +using Microsoft.Data.SqlClient.TestUtilities; |
| 11 | +using Xunit; |
| 12 | + |
| 13 | +namespace Microsoft.Data.SqlClient.ManualTesting.Tests |
| 14 | +{ |
| 15 | + |
| 16 | + [PlatformSpecific(TestPlatforms.Windows)] |
| 17 | + public class DistributedTransactionTestWindows |
| 18 | + { |
| 19 | + |
| 20 | +#if NET |
| 21 | + private static bool s_DelegatedTransactionCondition => DataTestUtility.AreConnStringsSetup() && DataTestUtility.IsNotAzureServer() && DataTestUtility.IsNotX86Architecture; |
| 22 | + |
| 23 | + [ConditionalFact(nameof(s_DelegatedTransactionCondition), Timeout = 10000)] |
| 24 | + public async Task Delegated_transaction_deadlock_in_SinglePhaseCommit() |
| 25 | + { |
| 26 | + TransactionManager.ImplicitDistributedTransactions = true; |
| 27 | + using var transaction = new CommittableTransaction(); |
| 28 | + |
| 29 | + // Uncommenting the following makes the deadlock go away as a workaround. If the transaction is promoted before |
| 30 | + // the first SqlClient enlistment, it never goes into the delegated state. |
| 31 | + // _ = TransactionInterop.GetTransmitterPropagationToken(transaction); |
| 32 | + await using var conn = new SqlConnection(DataTestUtility.TCPConnectionString); |
| 33 | + await conn.OpenAsync(); |
| 34 | + conn.EnlistTransaction(transaction); |
| 35 | + |
| 36 | + // Enlisting the transaction in second connection causes the transaction to be promoted. |
| 37 | + // After this, the transaction state will be "delegated" (delegated to SQL Server), and the commit below will |
| 38 | + // trigger a call to SqlDelegatedTransaction.SinglePhaseCommit. |
| 39 | + await using var conn2 = new SqlConnection(DataTestUtility.TCPConnectionString); |
| 40 | + await conn2.OpenAsync(); |
| 41 | + conn2.EnlistTransaction(transaction); |
| 42 | + |
| 43 | + // Possible deadlock |
| 44 | + transaction.Commit(); |
| 45 | + } |
| 46 | +#endif |
| 47 | + |
| 48 | + private static bool s_EnlistedTransactionPreservedWhilePooledCondition => DataTestUtility.AreConnStringsSetup() && DataTestUtility.IsNotX86Architecture; |
| 49 | + |
| 50 | + [ConditionalFact(nameof(s_EnlistedTransactionPreservedWhilePooledCondition), Timeout = 10000)] |
| 51 | + public void Test_EnlistedTransactionPreservedWhilePooled() |
| 52 | + { |
| 53 | +#if NET |
| 54 | + TransactionManager.ImplicitDistributedTransactions = true; |
| 55 | +#endif |
| 56 | + RunTestSet(EnlistedTransactionPreservedWhilePooled); |
| 57 | + } |
| 58 | + |
| 59 | + private void EnlistedTransactionPreservedWhilePooled() |
| 60 | + { |
| 61 | + Exception commandException = null; |
| 62 | + Exception transactionException = null; |
| 63 | + |
| 64 | + try |
| 65 | + { |
| 66 | + using (TransactionScope txScope = new TransactionScope(TransactionScopeOption.Required, TimeSpan.MaxValue)) |
| 67 | + { |
| 68 | + // Leave first connection open so that the transaction is promoted |
| 69 | + SqlConnection rootConnection = new SqlConnection(ConnectionString); |
| 70 | + rootConnection.Open(); |
| 71 | + using (SqlCommand command = rootConnection.CreateCommand()) |
| 72 | + { |
| 73 | + command.CommandText = $"INSERT INTO {TestTableName} VALUES ({InputCol1}, '{InputCol2}')"; |
| 74 | + command.ExecuteNonQuery(); |
| 75 | + } |
| 76 | + |
| 77 | + // Closing and reopening cycles the connection through the pool. |
| 78 | + // We want to verify that the transaction state is preserved through this cycle. |
| 79 | + SqlConnection enlistedConnection = new SqlConnection(ConnectionString); |
| 80 | + enlistedConnection.Open(); |
| 81 | + enlistedConnection.Close(); |
| 82 | + enlistedConnection.Open(); |
| 83 | + |
| 84 | + // Forcibly kill the root connection to mimic gateway's behavior when using the proxy connection policy |
| 85 | + // https://techcommunity.microsoft.com/blog/azuredbsupport/azure-sql-database-idle-sessions-are-killed-after-about-30-minutes-when-proxy-co/3268601 |
| 86 | + // Can also represent a general server-side, process failure |
| 87 | + KillProcess(rootConnection.ServerProcessId); |
| 88 | + |
| 89 | + |
| 90 | + using (SqlCommand command = enlistedConnection.CreateCommand()) |
| 91 | + { |
| 92 | + command.CommandText = $"INSERT INTO {TestTableName} VALUES ({InputCol1}, '{InputCol2}')"; |
| 93 | + try |
| 94 | + { |
| 95 | + command.ExecuteNonQuery(); |
| 96 | + } |
| 97 | + catch (Exception ex) |
| 98 | + { |
| 99 | + commandException = ex; |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + txScope.Complete(); |
| 104 | + } |
| 105 | + } |
| 106 | + catch (Exception ex) |
| 107 | + { |
| 108 | + transactionException = ex; |
| 109 | + } |
| 110 | + |
| 111 | + if (Utils.IsAzureSqlServer(new SqlConnectionStringBuilder((ConnectionString)).DataSource)) |
| 112 | + { |
| 113 | + // Even if an application swallows the command exception, completing the transaction should indicate that it failed. |
| 114 | + Assert.IsType<TransactionInDoubtException>(transactionException); |
| 115 | + // See https://learn.microsoft.com/en-us/sql/relational-databases/errors-events/database-engine-events-and-errors-3000-to-3999?view=sql-server-ver16 |
| 116 | + // Error 3971 corresponds to "The server failed to resume the transaction." |
| 117 | + Assert.Equal(3971, ((SqlException)commandException).Number); |
| 118 | + } |
| 119 | + else |
| 120 | + { |
| 121 | + Assert.IsType<TransactionAbortedException>(transactionException); |
| 122 | + |
| 123 | +#if NETFRAMEWORK |
| 124 | + // See https://learn.microsoft.com/en-us/sql/relational-databases/errors-events/database-engine-events-and-errors-8000-to-8999?view=sql-server-ver16 |
| 125 | + // The distributed transaction failed |
| 126 | + Assert.Equal(8525, ((SqlException)commandException).Number); |
| 127 | +#else |
| 128 | + Assert.IsType<InvalidOperationException>(commandException); |
| 129 | +#endif |
| 130 | + } |
| 131 | + |
| 132 | + // Verify that nothing made it into the database |
| 133 | + DataTable result = DataTestUtility.RunQuery(ConnectionString, $"select col2 from {TestTableName} where col1 = {InputCol1}"); |
| 134 | + Assert.True(result.Rows.Count == 0); |
| 135 | + } |
| 136 | + |
| 137 | + private void KillProcess(int serverProcessId) |
| 138 | + { |
| 139 | + using (TransactionScope txScope = new TransactionScope(TransactionScopeOption.Suppress)) |
| 140 | + { |
| 141 | + using (SqlConnection connection = new SqlConnection(ConnectionString)) |
| 142 | + { |
| 143 | + connection.Open(); |
| 144 | + using (SqlCommand command = connection.CreateCommand()) |
| 145 | + { |
| 146 | + command.CommandText = $"KILL {serverProcessId}"; |
| 147 | + command.ExecuteNonQuery(); |
| 148 | + } |
| 149 | + } |
| 150 | + txScope.Complete(); |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + private static string TestTableName; |
| 155 | + private static string ConnectionString; |
| 156 | + private const int InputCol1 = 1; |
| 157 | + private const string InputCol2 = "One"; |
| 158 | + |
| 159 | + private static void RunTestSet(Action TestCase) |
| 160 | + { |
| 161 | + SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(DataTestUtility.TCPConnectionString); |
| 162 | + |
| 163 | + builder.Pooling = true; |
| 164 | + builder.MaxPoolSize = 5; |
| 165 | + builder.Enlist = true; |
| 166 | + ConnectionString = builder.ConnectionString; |
| 167 | + |
| 168 | + TestTableName = DataTestUtility.GenerateObjectName(); |
| 169 | + DataTestUtility.RunNonQuery(ConnectionString, $"create table {TestTableName} (col1 int, col2 text)"); |
| 170 | + try |
| 171 | + { |
| 172 | + TestCase(); |
| 173 | + } |
| 174 | + finally |
| 175 | + { |
| 176 | + DataTestUtility.RunNonQuery(ConnectionString, $"drop table {TestTableName}"); |
| 177 | + } |
| 178 | + } |
| 179 | + } |
| 180 | +} |
| 181 | + |
0 commit comments