private static void TestTransactionSingleCase(string caseName, string connectionString, TransactionTestType testType) { WeakReference weak = null; using (SqlConnection con = new SqlConnection(connectionString)) { con.Open(); SqlTransaction gch = null; if ((testType != TransactionTestType.TransactionGC) && (testType != TransactionTestType.TransactionGCConnectionClose)) { gch = con.BeginTransaction(); } switch (testType) { case TransactionTestType.TransactionRollback: gch.Rollback(); break; case TransactionTestType.TransactionDispose: gch.Dispose(); break; case TransactionTestType.TransactionGC: weak = OpenNullifyTransaction(con); GC.Collect(); GC.WaitForPendingFinalizers(); Assert.False(weak.IsAlive, "Transaction is still alive!"); break; case TransactionTestType.ConnectionClose: GC.SuppressFinalize(gch); con.Close(); con.Open(); break; case TransactionTestType.TransactionGCConnectionClose: weak = OpenNullifyTransaction(con); GC.Collect(); GC.WaitForPendingFinalizers(); Assert.False(weak.IsAlive, "Transaction is still alive!"); con.Close(); con.Open(); break; } using (SqlCommand cmd = con.CreateCommand()) { cmd.CommandText = "select @@trancount"; int tranCount = (int)cmd.ExecuteScalar(); Assert.Equal(tranCount, 0); } } }
private static void TestTransactionSingleCase(string caseName, string connectionString, TransactionTestType testType) { WeakReference weak = null; using (SqlConnection con = new SqlConnection(connectionString)) { con.Open(); SqlTransaction gch = null; if ((testType != TransactionTestType.TransactionGC) && (testType != TransactionTestType.TransactionGCConnectionClose)) gch = con.BeginTransaction(); switch (testType) { case TransactionTestType.TransactionRollback: gch.Rollback(); break; case TransactionTestType.TransactionDispose: gch.Dispose(); break; case TransactionTestType.TransactionGC: weak = OpenNullifyTransaction(con); GC.Collect(); GC.WaitForPendingFinalizers(); Assert.False(weak.IsAlive, "Transaction is still alive!"); break; case TransactionTestType.ConnectionClose: GC.SuppressFinalize(gch); con.Close(); con.Open(); break; case TransactionTestType.TransactionGCConnectionClose: weak = OpenNullifyTransaction(con); GC.Collect(); GC.WaitForPendingFinalizers(); Assert.False(weak.IsAlive, "Transaction is still alive!"); con.Close(); con.Open(); break; } using (SqlCommand cmd = con.CreateCommand()) { cmd.CommandText = "select @@trancount"; int tranCount = (int)cmd.ExecuteScalar(); Assert.Equal(tranCount, 0); } } }