1 //------------------------------------------------------------------------------
2 // <copyright file="SqlConnectionPoolGroupProviderInfo.cs" company="Microsoft">
3 // Copyright (c) Microsoft Corporation. All rights reserved.
5 // <owner current="true" primary="true">Microsoft</owner>
6 // <owner current="true" primary="false">Microsoft</owner>
7 //------------------------------------------------------------------------------
9 namespace System
.Data
.SqlClient
13 using System
.Data
.Common
;
14 using System
.Data
.ProviderBase
;
16 sealed internal class SqlConnectionPoolGroupProviderInfo
: DbConnectionPoolGroupProviderInfo
{
17 private string _alias
;
18 private System
.Security
.PermissionSet _failoverPermissionSet
;
19 private string _failoverPartner
;
20 private bool _useFailoverPartner
;
22 internal SqlConnectionPoolGroupProviderInfo(SqlConnectionString connectionOptions
) {
23 // This is for the case where the user specified the failover partner
24 // in the connection string and we have not yet connected to get the
26 _failoverPartner
= connectionOptions
.FailoverPartner
;
28 if (ADP
.IsEmpty(_failoverPartner
)) {
29 _failoverPartner
= null;
33 internal string FailoverPartner
{
35 return _failoverPartner
;
39 internal bool UseFailoverPartner
{
41 return _useFailoverPartner
;
45 internal void AliasCheck(string server
) {
46 if (_alias
!= server
) {
51 else if (_alias
!= server
) {
52 Bid
.Trace("<sc.SqlConnectionPoolGroupProviderInfo|INFO> alias change detected. Clearing PoolGroup\n");
53 base.PoolGroup
.Clear();
60 private System
.Security
.PermissionSet
CreateFailoverPermission(SqlConnectionString userConnectionOptions
, string actualFailoverPartner
) {
61 string keywordToReplace
;
63 // RULES FOR CONSTRUCTING THE CONNECTION STRING TO DEMAND ON:
65 // 1) If no Failover Partner was specified in the original string:
67 // Server=actualFailoverPartner
69 // 2) If Failover Partner was specified in the original string:
71 // Server=originalValue; Failover Partner=actualFailoverPartner
73 // NOTE: in all cases, when we get a failover partner name from
74 // the server, we will use that name over what was specified
75 // in the original connection string.
77 if (null == userConnectionOptions
[SqlConnectionString
.KEY
.FailoverPartner
]) {
78 keywordToReplace
= SqlConnectionString
.KEY
.Data_Source
;
81 keywordToReplace
= SqlConnectionString
.KEY
.FailoverPartner
;
84 string failoverConnectionString
= userConnectionOptions
.ExpandKeyword(keywordToReplace
, actualFailoverPartner
);
85 return (new SqlConnectionString(failoverConnectionString
)).CreatePermissionSet();
88 internal void FailoverCheck(SqlInternalConnection connection
, bool actualUseFailoverPartner
, SqlConnectionString userConnectionOptions
, string actualFailoverPartner
) {
89 if (UseFailoverPartner
!= actualUseFailoverPartner
) {
91 Bid
.Trace("<sc.SqlConnectionPoolGroupProviderInfo|INFO> Failover detected. failover partner='%ls'. Clearing PoolGroup\n", actualFailoverPartner
);
92 base.PoolGroup
.Clear();
93 _useFailoverPartner
= actualUseFailoverPartner
;
95 // Only construct a new permission set when we're connecting to the
96 // primary data source, not the failover partner.
97 if (!_useFailoverPartner
&& _failoverPartner
!= actualFailoverPartner
) {
98 // NOTE: we optimisitically generate the permission set to keep
99 // lock short, but we only do this when we get a new
103 System
.Security
.PermissionSet failoverPermissionSet
= CreateFailoverPermission(userConnectionOptions
, actualFailoverPartner
);
106 if (_failoverPartner
!= actualFailoverPartner
) {
107 _failoverPartner
= actualFailoverPartner
;
108 _failoverPermissionSet
= failoverPermissionSet
;
114 internal void FailoverPermissionDemand() {
115 if (_useFailoverPartner
) {
116 // Note that we only demand when there is a permission set, which only
117 // happens once we've identified a failover situation in FailoverCheck
118 System
.Security
.PermissionSet failoverPermissionSet
= _failoverPermissionSet
;
119 if (null != failoverPermissionSet
) {
120 // demand on pooled failover connections
121 failoverPermissionSet
.Demand();