/// <summary> /// Tests that using the same connection string results in the same pool\internal connection and a different string results in a different pool\internal connection /// </summary> /// <param name="connectionString"></param> private static void BasicConnectionPoolingTest(string connectionString) { SqlConnection connection = new SqlConnection(connectionString); connection.Open(); InternalConnectionWrapper internalConnection = new InternalConnectionWrapper(connection); ConnectionPoolWrapper connectionPool = new ConnectionPoolWrapper(connection); connection.Close(); SqlConnection connection2 = new SqlConnection(connectionString); connection2.Open(); Assert.True(internalConnection.IsInternalConnectionOf(connection2), "New connection does not use same internal connection"); Assert.True(connectionPool.ContainsConnection(connection2), "New connection is in a different pool"); connection2.Close(); SqlConnection connection3 = new SqlConnection(connectionString + ";App=SqlConnectionPoolUnitTest;"); connection3.Open(); Assert.False(internalConnection.IsInternalConnectionOf(connection3), "Connection with different connection string uses same internal connection"); Assert.False(connectionPool.ContainsConnection(connection3), "Connection with different connection string uses same connection pool"); connection3.Close(); connectionPool.Cleanup(); SqlConnection connection4 = new SqlConnection(connectionString); connection4.Open(); Assert.True(internalConnection.IsInternalConnectionOf(connection4), "New connection does not use same internal connection"); Assert.True(connectionPool.ContainsConnection(connection4), "New connection is in a different pool"); connection4.Close(); }
/// <summary> /// Tests if killing the connection using the InternalConnectionWrapper is working /// </summary> /// <param name="connectionString"></param> private static void KillConnectionTest(string connectionString) { InternalConnectionWrapper wrapper = null; using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); wrapper = new InternalConnectionWrapper(connection); using (SqlCommand command = new SqlCommand("SELECT 5;", connection)) { CompareScalarResults(5, command.ExecuteScalar()); } wrapper.KillConnection(); } using (SqlConnection connection2 = new SqlConnection(connectionString)) { connection2.Open(); Assert.False(wrapper.IsInternalConnectionOf(connection2), "New connection has internal connection that was just killed"); using (SqlCommand command = new SqlCommand("SELECT 5;", connection2)) { CompareScalarResults(5, command.ExecuteScalar()); } } }
/// <summary> /// Tests if killing the connection using the InternalConnectionWrapper is working /// </summary> /// <param name="connectionString"></param> private static void KillConnectionTest(string connectionString) { #if DEBUG InternalConnectionWrapper wrapper = null; using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); wrapper = new InternalConnectionWrapper(connection); using (SqlCommand command = new SqlCommand("SELECT 5;", connection)) { DataTestUtility.AssertEqualsWithDescription(5, command.ExecuteScalar(), "Incorrect scalar result."); } wrapper.KillConnection(); } using (SqlConnection connection2 = new SqlConnection(connectionString)) { connection2.Open(); Assert.False(wrapper.IsInternalConnectionOf(connection2), "New connection has internal connection that was just killed"); using (SqlCommand command = new SqlCommand("SELECT 5;", connection2)) { DataTestUtility.AssertEqualsWithDescription(5, command.ExecuteScalar(), "Incorrect scalar result."); } } #endif }
/// <summary> /// Checks if an 'emancipated' internal connection is reclaimed when a new connection is opened AND we hit max pool size /// NOTE: 'emancipated' means that the internal connection's SqlConnection has fallen out of scope and has no references, but was not explicitly disposed\closed /// </summary> /// <param name="connectionString"></param> private static void ReclaimEmancipatedOnOpenTest(string connectionString) { string newConnectionString = (new SqlConnectionStringBuilder(connectionString) { MaxPoolSize = 1 }).ConnectionString; SqlConnection.ClearAllPools(); InternalConnectionWrapper internalConnection = CreateEmancipatedConnection(newConnectionString); ConnectionPoolWrapper connectionPool = internalConnection.ConnectionPool; GC.Collect(); GC.WaitForPendingFinalizers(); DataTestUtility.AssertEqualsWithDescription(1, connectionPool.ConnectionCount, "Wrong number of connections in the pool."); DataTestUtility.AssertEqualsWithDescription(0, connectionPool.FreeConnectionCount, "Wrong number of free connections in the pool."); using (SqlConnection connection = new SqlConnection(newConnectionString)) { connection.Open(); Assert.True(internalConnection.IsInternalConnectionOf(connection), "Connection has wrong internal connection"); Assert.True(connectionPool.ContainsConnection(connection), "Connection is in wrong connection pool"); } }
/// <summary> /// Checks if an 'emancipated' internal connection is reclaimed when a new connection is opened AND we hit max pool size /// NOTE: 'emancipated' means that the internal connection's SqlConnection has fallen out of scope and has no references, but was not explicitly disposed\closed /// </summary> /// <param name="connectionString"></param> private static void ReclaimEmancipatedOnOpenTest(string connectionString) { string newConnectionString = connectionString + ";Max Pool Size=1"; SqlConnection.ClearAllPools(); InternalConnectionWrapper internalConnection = CreateEmancipatedConnection(newConnectionString); ConnectionPoolWrapper connectionPool = internalConnection.ConnectionPool; GC.Collect(); GC.WaitForPendingFinalizers(); Assert.True(1 == connectionPool.ConnectionCount, "Wrong number of connections in the pool"); Assert.True(0 == connectionPool.FreeConnectionCount, "Wrong number of free connections in the pool"); using (SqlConnection connection = new SqlConnection(newConnectionString)) { connection.Open(); Assert.True(internalConnection.IsInternalConnectionOf(connection), "Connection has wrong internal connection"); Assert.True(connectionPool.ContainsConnection(connection), "Connection is in wrong connection pool"); } }
/// <summary> /// Tests if killing the connection using the InternalConnectionWrapper is working /// </summary> /// <param name="connectionString"></param> private static void KillConnectionTest(string connectionString) { InternalConnectionWrapper wrapper = null; using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); wrapper = new InternalConnectionWrapper(connection); using (SqlCommand command = new SqlCommand("SELECT 5;", connection)) { DataTestClass.AssertEqualsWithDescription(5, command.ExecuteScalar(), "Incorrect scalar result."); } wrapper.KillConnection(); } using (SqlConnection connection2 = new SqlConnection(connectionString)) { connection2.Open(); Assert.False(wrapper.IsInternalConnectionOf(connection2), "New connection has internal connection that was just killed"); using (SqlCommand command = new SqlCommand("SELECT 5;", connection2)) { DataTestClass.AssertEqualsWithDescription(5, command.ExecuteScalar(), "Incorrect scalar result."); } } }