Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

Commit

Permalink
Fix for 2nd Connection hangs in Mirroring Environment (#24181)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gene Lee authored Sep 21, 2017
1 parent b73daf3 commit dd48b05
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -1416,7 +1416,7 @@ TimeoutTimer timeout
// Determine unit interval
if (timeout.IsInfinite)
{
timeoutUnitInterval = checked((long)ADP.FailoverTimeoutStep * ADP.TimerFromSeconds(ADP.DefaultConnectionTimeout));
timeoutUnitInterval = checked((long)(ADP.FailoverTimeoutStep * ADP.TimerFromSeconds(ADP.DefaultConnectionTimeout)));
}
else
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// 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.Collections.Generic;
using System.Diagnostics;
using System.Threading;

namespace System.Data.SqlClient.ManualTesting.Tests
{
public static class ConnectionOnMirroringTest
{
private static ManualResetEvent workerCompletedEvent = new ManualResetEvent(false);

[CheckConnStrSetupFact]
public static void TestMultipleConnectionToMirroredServer()
{
string mirroringStateDesc;
string failoverPartnerName;
bool isMirroring = GetMirroringInfo(DataTestUtility.TcpConnStr, out mirroringStateDesc, out failoverPartnerName);
bool isSynchronized = "SYNCHRONIZED".Equals(mirroringStateDesc, StringComparison.InvariantCultureIgnoreCase);
if (isMirroring && isSynchronized && !string.IsNullOrEmpty(failoverPartnerName))
{
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(DataTestUtility.TcpConnStr);
builder.ConnectTimeout = 0;

TestWorker worker = new TestWorker(builder.ConnectionString);
Thread childThread = new Thread(() => worker.TestMultipleConnection());
childThread.Start();

if (workerCompletedEvent.WaitOne(10000))
{
childThread.Join();
}
else
{
// currently Thread.Abort() throws PlatformNotSupportedException in CoreFx.
childThread.Interrupt();
throw new Exception("SqlConnection could not open and close successfully in timely manner. Possibly connection hangs.");
}
}
}

private static bool GetMirroringInfo(string connectionString, out string mirroringStateDesc, out string failoverPartnerName)
{
mirroringStateDesc = null;
failoverPartnerName = null;

SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(connectionString);
string dbname = builder.InitialCatalog;

builder.ConnectTimeout = 5;
connectionString = builder.ConnectionString;

DataTable dt = DataTestUtility.RunQuery(connectionString, $"select mirroring_state_desc from sys.database_mirroring where database_id = DB_ID('{dbname}')");
mirroringStateDesc = dt.Rows[0][0].ToString();

bool isMirroring = !string.IsNullOrEmpty(mirroringStateDesc);
if (isMirroring)
{
dt = DataTestUtility.RunQuery(connectionString, $"select mirroring_partner_name from sys.database_mirroring where database_id = DB_ID('{dbname}')");
failoverPartnerName = dt.Rows[0][0].ToString();
}

return isMirroring;
}

private class TestWorker
{
private string _connectionString;

public TestWorker(string connectionString)
{
_connectionString = connectionString;
}

public void TestMultipleConnection()
{
List<SqlConnection> list = new List<SqlConnection>();

for (int i = 0; i < 10; ++i)
{
SqlConnection conn = new SqlConnection(_connectionString);
list.Add(conn);
conn.Open();
}

foreach (SqlConnection conn in list)
{
conn.Dispose();
}

workerCompletedEvent.Set();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
<Compile Include="SQL\LocalDBTest\LocalDBTest.cs" />
<Compile Include="SQL\MARSSessionPoolingTest\MARSSessionPoolingTest.cs" />
<Compile Include="SQL\MARSTest\MARSTest.cs" />
<Compile Include="SQL\MirroringTest\ConnectionOnMirroringTest.cs" />
<Compile Include="SQL\ParallelTransactionsTest\ParallelTransactionsTest.cs" />
<Compile Include="SQL\SqlSchemaInfoTest\SqlSchemaInfoTest.cs" />
<Compile Include="SQL\SqlBulkCopyTest\AdjustPrecScaleForBulkCopy.cs" />
Expand Down

0 comments on commit dd48b05

Please sign in to comment.