Skip to content

Commit b75dfb5

Browse files
authored
Move to Shared for SqlConnectionPoolGroupProviderInfo.cs (#1323)
1 parent d8b59b6 commit b75dfb5

File tree

6 files changed

+57
-152
lines changed

6 files changed

+57
-152
lines changed

src/Microsoft.Data.SqlClient/netcore/src/Microsoft.Data.SqlClient.csproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,9 @@
247247
<Compile Include="..\..\src\Microsoft\Data\SqlClient\SqlCommandSet.cs">
248248
<Link>Microsoft\Data\SqlClient\SqlCommandSet.cs</Link>
249249
</Compile>
250+
<Compile Include="..\..\src\Microsoft\Data\SqlClient\SqlConnectionPoolGroupProviderInfo.cs">
251+
<Link>Microsoft\Data\SqlClient\SqlConnectionPoolGroupProviderInfo.cs</Link>
252+
</Compile>
250253
<Compile Include="..\..\src\Microsoft\Data\SqlClient\SqlConnectionPoolProviderInfo.cs">
251254
<Link>Microsoft\Data\SqlClient\SqlConnectionPoolProviderInfo.cs</Link>
252255
</Compile>
@@ -516,7 +519,6 @@
516519
<Compile Include="Microsoft\Data\SqlClient\SqlConnection.cs" />
517520
<Compile Include="Microsoft\Data\SqlClient\SqlConnectionFactory.cs" />
518521
<Compile Include="Microsoft\Data\SqlClient\SqlConnectionHelper.cs" />
519-
<Compile Include="Microsoft\Data\SqlClient\SqlConnectionPoolGroupProviderInfo.cs" />
520522
<Compile Include="Microsoft\Data\SqlClient\SqlConnectionPoolKey.cs" />
521523
<Compile Include="Microsoft\Data\SqlClient\SqlConnectionString.cs" />
522524
<Compile Include="Microsoft\Data\SqlClient\SqlConnectionStringBuilder.cs" />

src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlConnectionPoolGroupProviderInfo.cs

Lines changed: 0 additions & 91 deletions
This file was deleted.

src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlInternalConnectionTds.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1635,7 +1635,7 @@ private void LoginNoFailover(ServerInfo serverInfo,
16351635
// We must wait for CompleteLogin to finish for to have the
16361636
// env change from the server to know its designated failover
16371637
// partner; save this information in _currentFailoverPartner.
1638-
PoolGroupProviderInfo.FailoverCheck(this, false, connectionOptions, ServerProvidedFailOverPartner);
1638+
PoolGroupProviderInfo.FailoverCheck(false, connectionOptions, ServerProvidedFailOverPartner);
16391639
}
16401640
CurrentDataSource = originalServerInfo.UserServerName;
16411641
}
@@ -1841,7 +1841,7 @@ TimeoutTimer timeout
18411841
// We must wait for CompleteLogin to finish for to have the
18421842
// env change from the server to know its designated failover
18431843
// partner; save this information in _currentFailoverPartner.
1844-
PoolGroupProviderInfo.FailoverCheck(this, useFailoverHost, connectionOptions, ServerProvidedFailOverPartner);
1844+
PoolGroupProviderInfo.FailoverCheck(useFailoverHost, connectionOptions, ServerProvidedFailOverPartner);
18451845
}
18461846
CurrentDataSource = (useFailoverHost ? failoverHost : primaryServerInfo.UserServerName);
18471847
}

src/Microsoft.Data.SqlClient/netfx/src/Microsoft.Data.SqlClient.csproj

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,9 @@
318318
<Compile Include="..\..\src\Microsoft\Data\SqlClient\SqlCommandSet.cs">
319319
<Link>Microsoft\Data\SqlClient\SqlCommandSet.cs</Link>
320320
</Compile>
321+
<Compile Include="..\..\src\Microsoft\Data\SqlClient\SqlConnectionPoolGroupProviderInfo.cs">
322+
<Link>Microsoft\Data\SqlClient\SqlConnectionPoolGroupProviderInfo.cs</Link>
323+
</Compile>
321324
<Compile Include="..\..\src\Microsoft\Data\SqlClient\SqlConnectionPoolProviderInfo.cs">
322325
<Link>Microsoft\Data\SqlClient\SqlConnectionPoolProviderInfo.cs</Link>
323326
</Compile>
@@ -495,7 +498,6 @@
495498
<Compile Include="Microsoft\Data\SqlClient\SqlConnection.cs" />
496499
<Compile Include="Microsoft\Data\SqlClient\SqlConnectionFactory.cs" />
497500
<Compile Include="Microsoft\Data\SqlClient\SqlConnectionHelper.cs" />
498-
<Compile Include="Microsoft\Data\SqlClient\SqlConnectionPoolGroupProviderInfo.cs" />
499501
<Compile Include="Microsoft\Data\SqlClient\SqlConnectionPoolKey.cs" />
500502
<Compile Include="Microsoft\Data\SqlClient\SqlConnectionString.cs" />
501503
<Compile Include="Microsoft\Data\SqlClient\SqlConnectionStringBuilder.cs" />
@@ -659,4 +661,4 @@
659661
<Import Project="$(NetFxSource)tools\targets\GenerateThisAssemblyCs.targets" />
660662
<Import Project="$(NetFxSource)tools\targets\GenerateAssemblyRef.targets" />
661663
<Import Project="$(NetFxSource)tools\targets\GenerateAssemblyInfo.targets" />
662-
</Project>
664+
</Project>

src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlInternalConnectionTds.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1966,7 +1966,7 @@ private void LoginNoFailover(ServerInfo serverInfo, string newPassword, SecureSt
19661966
// We must wait for CompleteLogin to finish for to have the
19671967
// env change from the server to know its designated failover
19681968
// partner; save this information in _currentFailoverPartner.
1969-
PoolGroupProviderInfo.FailoverCheck(this, false, connectionOptions, ServerProvidedFailOverPartner);
1969+
PoolGroupProviderInfo.FailoverCheck(false, connectionOptions, ServerProvidedFailOverPartner);
19701970
}
19711971
CurrentDataSource = originalServerInfo.UserServerName;
19721972
}
@@ -2235,7 +2235,7 @@ TimeoutTimer timeout
22352235
// We must wait for CompleteLogin to finish for to have the
22362236
// env change from the server to know its designated failover
22372237
// partner; save this information in _currentFailoverPartner.
2238-
PoolGroupProviderInfo.FailoverCheck(this, useFailoverHost, connectionOptions, ServerProvidedFailOverPartner);
2238+
PoolGroupProviderInfo.FailoverCheck(useFailoverHost, connectionOptions, ServerProvidedFailOverPartner);
22392239
}
22402240
CurrentDataSource = (useFailoverHost ? failoverHost : primaryServerInfo.UserServerName);
22412241
}
Lines changed: 46 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -2,46 +2,36 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5-
using Microsoft.Data.Common;
5+
using System.Security;
66
using Microsoft.Data.ProviderBase;
77

88
namespace Microsoft.Data.SqlClient
99
{
10-
sealed internal class SqlConnectionPoolGroupProviderInfo : DbConnectionPoolGroupProviderInfo
10+
internal sealed class SqlConnectionPoolGroupProviderInfo : DbConnectionPoolGroupProviderInfo
1111
{
1212
private string _alias;
13-
private System.Security.PermissionSet _failoverPermissionSet;
1413
private string _failoverPartner;
1514
private bool _useFailoverPartner;
15+
#if NETFRAMEWORK
16+
private PermissionSet _failoverPermissionSet;
17+
#endif
1618

1719
internal SqlConnectionPoolGroupProviderInfo(SqlConnectionString connectionOptions)
1820
{
1921
// This is for the case where the user specified the failover partner
20-
// in the connection string and we have not yet connected to get the
22+
// in the connection string and we have not yet connected to get the
2123
// env change.
2224
_failoverPartner = connectionOptions.FailoverPartner;
2325

24-
if (ADP.IsEmpty(_failoverPartner))
26+
if (string.IsNullOrEmpty(_failoverPartner))
2527
{
2628
_failoverPartner = null;
2729
}
2830
}
2931

30-
internal string FailoverPartner
31-
{
32-
get
33-
{
34-
return _failoverPartner;
35-
}
36-
}
32+
internal string FailoverPartner => _failoverPartner;
3733

38-
internal bool UseFailoverPartner
39-
{
40-
get
41-
{
42-
return _useFailoverPartner;
43-
}
44-
}
34+
internal bool UseFailoverPartner => _useFailoverPartner;
4535

4636
internal void AliasCheck(string server)
4737
{
@@ -55,15 +45,48 @@ internal void AliasCheck(string server)
5545
}
5646
else if (_alias != server)
5747
{
58-
SqlClientEventSource.Log.TryTraceEvent("<sc.SqlConnectionPoolGroupProviderInfo|INFO> alias change detected. Clearing PoolGroup");
48+
SqlClientEventSource.Log.TryTraceEvent("SqlConnectionPoolGroupProviderInfo.AliasCheck | Info | Alias change detected. Clearing PoolGroup.");
5949
base.PoolGroup.Clear();
6050
_alias = server;
6151
}
6252
}
6353
}
6454
}
6555

66-
private System.Security.PermissionSet CreateFailoverPermission(SqlConnectionString userConnectionOptions, string actualFailoverPartner)
56+
internal void FailoverCheck(bool actualUseFailoverPartner, SqlConnectionString userConnectionOptions, string actualFailoverPartner)
57+
{
58+
if (UseFailoverPartner != actualUseFailoverPartner)
59+
{
60+
SqlClientEventSource.Log.TryTraceEvent("SqlConnectionPoolGroupProviderInfo.FailoverCheck | Info | Failover detected. Failover partner '{0}'. Clearing PoolGroup", actualFailoverPartner);
61+
base.PoolGroup.Clear();
62+
_useFailoverPartner = actualUseFailoverPartner;
63+
}
64+
// Only construct a new permission set when we're connecting to the
65+
// primary data source, not the failover partner.
66+
if (!_useFailoverPartner && _failoverPartner != actualFailoverPartner)
67+
{
68+
// NOTE: we optimistically generate the permission set to keep
69+
// lock short, but we only do this when we get a new
70+
// failover partner.
71+
72+
#if NETFRAMEWORK
73+
PermissionSet failoverPermissionSet = CreateFailoverPermission(userConnectionOptions, actualFailoverPartner);
74+
#endif
75+
lock (this)
76+
{
77+
if (_failoverPartner != actualFailoverPartner)
78+
{
79+
_failoverPartner = actualFailoverPartner;
80+
#if NETFRAMEWORK
81+
_failoverPermissionSet = failoverPermissionSet;
82+
#endif
83+
}
84+
}
85+
}
86+
}
87+
88+
#if NETFRAMEWORK
89+
private PermissionSet CreateFailoverPermission(SqlConnectionString userConnectionOptions, string actualFailoverPartner)
6790
{
6891
string keywordToReplace;
6992

@@ -94,51 +117,20 @@ private System.Security.PermissionSet CreateFailoverPermission(SqlConnectionStri
94117
return (new SqlConnectionString(failoverConnectionString)).CreatePermissionSet();
95118
}
96119

97-
internal void FailoverCheck(SqlInternalConnection connection, bool actualUseFailoverPartner, SqlConnectionString userConnectionOptions, string actualFailoverPartner)
98-
{
99-
if (UseFailoverPartner != actualUseFailoverPartner)
100-
{
101-
// TODO: will connections in progress somehow be active for two different datasources?
102-
SqlClientEventSource.Log.TryTraceEvent("<sc.SqlConnectionPoolGroupProviderInfo|INFO> Failover detected. failover partner='{0}'. Clearing PoolGroup", actualFailoverPartner);
103-
104-
base.PoolGroup.Clear();
105-
_useFailoverPartner = actualUseFailoverPartner;
106-
}
107-
// Only construct a new permission set when we're connecting to the
108-
// primary data source, not the failover partner.
109-
if (!_useFailoverPartner && _failoverPartner != actualFailoverPartner)
110-
{
111-
// NOTE: we optimisitically generate the permission set to keep
112-
// lock short, but we only do this when we get a new
113-
// failover partner.
114-
// TODO: it seems to me that being optimistic here may not be such a good idea; what if there are 100s of concurrent failovers?
115-
116-
System.Security.PermissionSet failoverPermissionSet = CreateFailoverPermission(userConnectionOptions, actualFailoverPartner);
117-
118-
lock (this)
119-
{
120-
if (_failoverPartner != actualFailoverPartner)
121-
{
122-
_failoverPartner = actualFailoverPartner;
123-
_failoverPermissionSet = failoverPermissionSet;
124-
}
125-
}
126-
}
127-
}
128-
129120
internal void FailoverPermissionDemand()
130121
{
131122
if (_useFailoverPartner)
132123
{
133124
// Note that we only demand when there is a permission set, which only
134125
// happens once we've identified a failover situation in FailoverCheck
135-
System.Security.PermissionSet failoverPermissionSet = _failoverPermissionSet;
126+
PermissionSet failoverPermissionSet = _failoverPermissionSet;
136127
if (null != failoverPermissionSet)
137128
{
138129
// demand on pooled failover connections
139130
failoverPermissionSet.Demand();
140131
}
141132
}
142133
}
134+
#endif
143135
}
144136
}

0 commit comments

Comments
 (0)