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 ;
66using Microsoft . Data . ProviderBase ;
77
88namespace 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