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.
5 using System.Transactions;
8 namespace System.Data.SqlClient.ManualTesting.Tests
10 public class TransactionEnlistmentTest
12 [ConditionalFact(typeof(DataTestUtility),nameof(DataTestUtility.AreConnStringsSetup))]
13 public static void TestAutoEnlistment_TxScopeComplete()
15 RunTestSet(TestCase_AutoEnlistment_TxScopeComplete);
18 [ConditionalFact(typeof(DataTestUtility),nameof(DataTestUtility.AreConnStringsSetup))]
19 public static void TestAutoEnlistment_TxScopeNonComplete()
21 RunTestSet(TestCase_AutoEnlistment_TxScopeNonComplete);
24 [ConditionalFact(typeof(DataTestUtility),nameof(DataTestUtility.AreConnStringsSetup))]
25 public static void TestManualEnlistment_Enlist()
27 RunTestSet(TestCase_ManualEnlistment_Enlist);
30 [ConditionalFact(typeof(DataTestUtility),nameof(DataTestUtility.AreConnStringsSetup))]
31 public static void TestManualEnlistment_NonEnlist()
33 RunTestSet(TestCase_ManualEnlistment_NonEnlist);
36 [ConditionalFact(typeof(DataTestUtility),nameof(DataTestUtility.AreConnStringsSetup))]
37 public static void TestManualEnlistment_Enlist_TxScopeComplete()
39 RunTestSet(TestCase_ManualEnlistment_Enlist_TxScopeComplete);
45 private static void TestCase_AutoEnlistment_TxScopeComplete()
47 SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(ConnectionString);
48 builder.Enlist = true;
49 ConnectionString = builder.ConnectionString;
51 using (TransactionScope txScope = new TransactionScope(TransactionScopeOption.Required, TimeSpan.MaxValue))
53 using (SqlConnection connection = new SqlConnection(ConnectionString))
56 using (SqlCommand command = connection.CreateCommand())
58 command.CommandText = $"INSERT INTO {TestTableName} VALUES ({InputCol1}, '{InputCol2}')";
59 command.ExecuteNonQuery();
65 DataTable result = DataTestUtility.RunQuery(ConnectionString, $"select col2 from {TestTableName} where col1 = {InputCol1}");
66 Assert.True(result.Rows.Count == 1);
67 Assert.True(string.Equals(result.Rows[0][0], InputCol2));
70 private static void TestCase_AutoEnlistment_TxScopeNonComplete()
72 SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(ConnectionString);
73 builder.Enlist = true;
74 ConnectionString = builder.ConnectionString;
76 using (TransactionScope txScope = new TransactionScope(TransactionScopeOption.Required, TimeSpan.MaxValue))
78 using (SqlConnection connection = new SqlConnection(ConnectionString))
81 using (SqlCommand command = connection.CreateCommand())
83 command.CommandText = $"INSERT INTO {TestTableName} VALUES ({InputCol1}, '{InputCol2}')";
84 command.ExecuteNonQuery();
89 DataTable result = DataTestUtility.RunQuery(ConnectionString, $"select col2 from {TestTableName} where col1 = {InputCol1}");
90 Assert.True(result.Rows.Count == 0);
93 private static void TestCase_ManualEnlistment_Enlist()
95 SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(ConnectionString);
96 builder.Enlist = false;
97 ConnectionString = builder.ConnectionString;
99 using (SqlConnection connection = new SqlConnection(ConnectionString))
102 using (TransactionScope txScope = new TransactionScope())
104 connection.EnlistTransaction(Transactions.Transaction.Current);
105 using (SqlCommand command = connection.CreateCommand())
107 command.CommandText = $"INSERT INTO {TestTableName} VALUES ({InputCol1}, '{InputCol2}')";
108 command.ExecuteNonQuery();
113 DataTable result = DataTestUtility.RunQuery(ConnectionString, $"select col2 from {TestTableName} where col1 = {InputCol1}");
114 Assert.True(result.Rows.Count == 0);
117 private static void TestCase_ManualEnlistment_NonEnlist()
119 SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(ConnectionString);
120 builder.Enlist = false;
121 ConnectionString = builder.ConnectionString;
123 using (SqlConnection connection = new SqlConnection(ConnectionString))
126 using (TransactionScope txScope = new TransactionScope())
128 using (SqlCommand command = connection.CreateCommand())
130 command.CommandText = $"INSERT INTO {TestTableName} VALUES ({InputCol1}, '{InputCol2}')";
131 command.ExecuteNonQuery();
136 DataTable result = DataTestUtility.RunQuery(ConnectionString, $"select col2 from {TestTableName} where col1 = {InputCol1}");
137 Assert.True(result.Rows.Count == 1);
138 Assert.True(string.Equals(result.Rows[0][0], InputCol2));
141 private static void TestCase_ManualEnlistment_Enlist_TxScopeComplete()
143 SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(ConnectionString);
144 builder.Enlist = false;
145 ConnectionString = builder.ConnectionString;
147 using (SqlConnection connection = new SqlConnection(ConnectionString))
150 using (TransactionScope txScope = new TransactionScope())
152 connection.EnlistTransaction(Transactions.Transaction.Current);
153 using (SqlCommand command = connection.CreateCommand())
155 command.CommandText = $"INSERT INTO {TestTableName} VALUES ({InputCol1}, '{InputCol2}')";
156 command.ExecuteNonQuery();
162 DataTable result = DataTestUtility.RunQuery(ConnectionString, $"select col2 from {TestTableName} where col1 = {InputCol1}");
163 Assert.True(result.Rows.Count == 1);
164 Assert.True(string.Equals(result.Rows[0][0], InputCol2));
170 private static string TestTableName;
171 private static string ConnectionString;
172 private const int InputCol1 = 1;
173 private const string InputCol2 = "One";
175 private static void RunTestSet(Action TestCase)
177 SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(DataTestUtility.TcpConnStr);
178 builder.ConnectTimeout = 5;
180 builder.Pooling = true;
181 ConnectionString = builder.ConnectionString;
183 RunTestFormat(TestCase);
185 builder.Pooling = false;
186 ConnectionString = builder.ConnectionString;
188 RunTestFormat(TestCase);
191 private static void RunTestFormat(Action testCase)
193 TestTableName = DataTestUtility.GenerateTableName();
194 DataTestUtility.RunNonQuery(ConnectionString, $"create table {TestTableName} (col1 int, col2 text)");
201 DataTestUtility.RunNonQuery(ConnectionString, $"drop table {TestTableName}");