forked from dotnet/SqlClient
/
SqlConnectionFactory.cs
313 lines (268 loc) · 13 KB
/
SqlConnectionFactory.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
// 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;
using System.Data.Common;
using System.Diagnostics;
using System.IO;
using Microsoft.Data.Common;
using Microsoft.Data.ProviderBase;
namespace Microsoft.Data.SqlClient
{
sealed internal class SqlConnectionFactory : DbConnectionFactory
{
private const string _metaDataXml = "MetaDataXml";
private SqlConnectionFactory() : base() { }
public static readonly SqlConnectionFactory SingletonInstance = new SqlConnectionFactory();
override public DbProviderFactory ProviderFactory
{
get
{
return SqlClientFactory.Instance;
}
}
override protected DbConnectionInternal CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection)
{
return CreateConnection(options, poolKey, poolGroupProviderInfo, pool, owningConnection, userOptions: null);
}
override protected DbConnectionInternal CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)
{
SqlConnectionString opt = (SqlConnectionString)options;
SqlConnectionPoolKey key = (SqlConnectionPoolKey)poolKey;
SqlInternalConnection result = null;
SessionData recoverySessionData = null;
SqlConnection sqlOwningConnection = (SqlConnection)owningConnection;
bool applyTransientFaultHandling = sqlOwningConnection != null ? sqlOwningConnection._applyTransientFaultHandling : false;
SqlConnectionString userOpt = null;
if (userOptions != null)
{
userOpt = (SqlConnectionString)userOptions;
}
else if (sqlOwningConnection != null)
{
userOpt = (SqlConnectionString)(sqlOwningConnection.UserConnectionOptions);
}
if (sqlOwningConnection != null)
{
recoverySessionData = sqlOwningConnection._recoverySessionData;
}
bool redirectedUserInstance = false;
DbConnectionPoolIdentity identity = null;
// Pass DbConnectionPoolIdentity to SqlInternalConnectionTds if using integrated security.
// Used by notifications.
if (opt.IntegratedSecurity)
{
if (pool != null)
{
identity = pool.Identity;
}
else
{
identity = DbConnectionPoolIdentity.GetCurrent();
}
}
// FOLLOWING IF BLOCK IS ENTIRELY FOR SSE USER INSTANCES
// If "user instance=true" is in the connection string, we're using SSE user instances
if (opt.UserInstance)
{
// opt.DataSource is used to create the SSE connection
redirectedUserInstance = true;
string instanceName;
if ((null == pool) ||
(null != pool && pool.Count <= 0))
{ // Non-pooled or pooled and no connections in the pool.
SqlInternalConnectionTds sseConnection = null;
try
{
// We throw an exception in case of a failure
// NOTE: Cloning connection option opt to set 'UserInstance=True' and 'Enlist=False'
// This first connection is established to SqlExpress to get the instance name
// of the UserInstance.
SqlConnectionString sseopt = new SqlConnectionString(opt, opt.DataSource, userInstance: true, setEnlistValue: false);
sseConnection = new SqlInternalConnectionTds(identity, sseopt, key.Credential, null, "", null, false, applyTransientFaultHandling: applyTransientFaultHandling);
// NOTE: Retrieve <UserInstanceName> here. This user instance name will be used below to connect to the Sql Express User Instance.
instanceName = sseConnection.InstanceName;
if (!instanceName.StartsWith("\\\\.\\", StringComparison.Ordinal))
{
throw SQL.NonLocalSSEInstance();
}
if (null != pool)
{ // Pooled connection - cache result
SqlConnectionPoolProviderInfo providerInfo = (SqlConnectionPoolProviderInfo)pool.ProviderInfo;
// No lock since we are already in creation mutex
providerInfo.InstanceName = instanceName;
}
}
finally
{
if (null != sseConnection)
{
sseConnection.Dispose();
}
}
}
else
{ // Cached info from pool.
SqlConnectionPoolProviderInfo providerInfo = (SqlConnectionPoolProviderInfo)pool.ProviderInfo;
// No lock since we are already in creation mutex
instanceName = providerInfo.InstanceName;
}
// NOTE: Here connection option opt is cloned to set 'instanceName=<UserInstanceName>' that was
// retrieved from the previous SSE connection. For this UserInstance connection 'Enlist=True'.
// options immutable - stored in global hash - don't modify
opt = new SqlConnectionString(opt, instanceName, userInstance: false, setEnlistValue: null);
poolGroupProviderInfo = null; // null so we do not pass to constructor below...
}
result = new SqlInternalConnectionTds(identity, opt, key.Credential, poolGroupProviderInfo, "", null, redirectedUserInstance, userOpt, recoverySessionData, applyTransientFaultHandling: applyTransientFaultHandling, key.AccessToken, pool);
return result;
}
protected override DbConnectionOptions CreateConnectionOptions(string connectionString, DbConnectionOptions previous)
{
Debug.Assert(!string.IsNullOrEmpty(connectionString), "empty connectionString");
SqlConnectionString result = new SqlConnectionString(connectionString);
return result;
}
override internal DbConnectionPoolProviderInfo CreateConnectionPoolProviderInfo(DbConnectionOptions connectionOptions)
{
DbConnectionPoolProviderInfo providerInfo = null;
if (((SqlConnectionString)connectionOptions).UserInstance)
{
providerInfo = new SqlConnectionPoolProviderInfo();
}
return providerInfo;
}
override protected DbConnectionPoolGroupOptions CreateConnectionPoolGroupOptions(DbConnectionOptions connectionOptions)
{
SqlConnectionString opt = (SqlConnectionString)connectionOptions;
DbConnectionPoolGroupOptions poolingOptions = null;
if (opt.Pooling)
{ // never pool context connections.
int connectionTimeout = opt.ConnectTimeout;
if ((0 < connectionTimeout) && (connectionTimeout < int.MaxValue / 1000))
{
connectionTimeout *= 1000;
}
else if (connectionTimeout >= int.MaxValue / 1000)
{
connectionTimeout = int.MaxValue;
}
if (opt.Authentication == SqlAuthenticationMethod.ActiveDirectoryInteractive || opt.Authentication == SqlAuthenticationMethod.ActiveDirectoryDeviceCodeFlow)
{
// interactive/device code flow mode will always have pool's CreateTimeout = 10 x ConnectTimeout.
if (connectionTimeout >= Int32.MaxValue / 10)
{
connectionTimeout = Int32.MaxValue;
}
else
{
connectionTimeout *= 10;
}
SqlClientEventSource.Log.TraceEvent("<sc.SqlConnectionFactory.CreateConnectionPoolGroupOptions>Set connection pool CreateTimeout={0} when {1} is in use.", connectionTimeout, opt.Authentication);
}
poolingOptions = new DbConnectionPoolGroupOptions(
opt.IntegratedSecurity,
opt.MinPoolSize,
opt.MaxPoolSize,
connectionTimeout,
opt.LoadBalanceTimeout,
opt.Enlist);
}
return poolingOptions;
}
override internal DbConnectionPoolGroupProviderInfo CreateConnectionPoolGroupProviderInfo(DbConnectionOptions connectionOptions)
{
return new SqlConnectionPoolGroupProviderInfo((SqlConnectionString)connectionOptions);
}
internal static SqlConnectionString FindSqlConnectionOptions(SqlConnectionPoolKey key)
{
SqlConnectionString connectionOptions = (SqlConnectionString)SingletonInstance.FindConnectionOptions(key);
if (null == connectionOptions)
{
connectionOptions = new SqlConnectionString(key.ConnectionString);
}
if (connectionOptions.IsEmpty)
{
throw ADP.NoConnectionString();
}
return connectionOptions;
}
override internal DbConnectionPoolGroup GetConnectionPoolGroup(DbConnection connection)
{
SqlConnection c = (connection as SqlConnection);
if (null != c)
{
return c.PoolGroup;
}
return null;
}
override internal DbConnectionInternal GetInnerConnection(DbConnection connection)
{
SqlConnection c = (connection as SqlConnection);
if (null != c)
{
return c.InnerConnection;
}
return null;
}
override protected int GetObjectId(DbConnection connection)
{
SqlConnection c = (connection as SqlConnection);
if (null != c)
{
return c.ObjectID;
}
return 0;
}
override internal void PermissionDemand(DbConnection outerConnection)
{
SqlConnection c = (outerConnection as SqlConnection);
if (null != c)
{
c.PermissionDemand();
}
}
override internal void SetConnectionPoolGroup(DbConnection outerConnection, DbConnectionPoolGroup poolGroup)
{
SqlConnection c = (outerConnection as SqlConnection);
if (null != c)
{
c.PoolGroup = poolGroup;
}
}
override internal void SetInnerConnectionEvent(DbConnection owningObject, DbConnectionInternal to)
{
SqlConnection c = (owningObject as SqlConnection);
if (null != c)
{
c.SetInnerConnectionEvent(to);
}
}
override internal bool SetInnerConnectionFrom(DbConnection owningObject, DbConnectionInternal to, DbConnectionInternal from)
{
SqlConnection c = (owningObject as SqlConnection);
if (null != c)
{
return c.SetInnerConnectionFrom(to, from);
}
return false;
}
override internal void SetInnerConnectionTo(DbConnection owningObject, DbConnectionInternal to)
{
SqlConnection c = (owningObject as SqlConnection);
if (null != c)
{
c.SetInnerConnectionTo(to);
}
}
protected override DbMetaDataFactory CreateMetaDataFactory(DbConnectionInternal internalConnection, out bool cacheMetaDataFactory)
{
Debug.Assert(internalConnection != null, "internalConnection may not be null.");
Stream xmlStream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("Microsoft.Data.SqlClient.SqlMetaData.xml");
cacheMetaDataFactory = true;
Debug.Assert(xmlStream != null, nameof(xmlStream) + " may not be null.");
return new SqlMetaDataFactory(xmlStream,
internalConnection.ServerVersion,
internalConnection.ServerVersion);
}
}
}