Skip to content

Commit 111e42b

Browse files
committed
Move GetConnectionPoolGroup
1 parent 25d580d commit 111e42b

File tree

2 files changed

+99
-89
lines changed

2 files changed

+99
-89
lines changed

src/Microsoft.Data.SqlClient/src/Microsoft/Data/ProviderBase/DbConnectionFactory.cs

Lines changed: 0 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -47,95 +47,6 @@ protected static Task<DbConnectionInternal> GetCompletedTask()
4747
return s_completedTask ?? (s_completedTask = Task.FromResult<DbConnectionInternal>(null));
4848
}
4949

50-
internal DbConnectionPoolGroup GetConnectionPoolGroup(DbConnectionPoolKey key, DbConnectionPoolGroupOptions poolOptions, ref DbConnectionOptions userConnectionOptions)
51-
{
52-
if (string.IsNullOrEmpty(key.ConnectionString))
53-
{
54-
return (DbConnectionPoolGroup)null;
55-
}
56-
57-
DbConnectionPoolGroup connectionPoolGroup;
58-
Dictionary<DbConnectionPoolKey, DbConnectionPoolGroup> connectionPoolGroups = _connectionPoolGroups;
59-
if (!connectionPoolGroups.TryGetValue(key, out connectionPoolGroup) || (connectionPoolGroup.IsDisabled && connectionPoolGroup.PoolGroupOptions != null))
60-
{
61-
// If we can't find an entry for the connection string in
62-
// our collection of pool entries, then we need to create a
63-
// new pool entry and add it to our collection.
64-
65-
DbConnectionOptions connectionOptions = CreateConnectionOptions(key.ConnectionString, userConnectionOptions);
66-
if (connectionOptions == null)
67-
{
68-
throw ADP.InternalConnectionError(ADP.ConnectionError.ConnectionOptionsMissing);
69-
}
70-
71-
if (userConnectionOptions == null)
72-
{
73-
// we only allow one expansion on the connection string
74-
75-
userConnectionOptions = connectionOptions;
76-
string expandedConnectionString = connectionOptions.Expand();
77-
78-
// if the expanded string is same instance (default implementation), then use the already created options
79-
if ((object)expandedConnectionString != (object)key.ConnectionString)
80-
{
81-
// CONSIDER: caching the original string to reduce future parsing
82-
DbConnectionPoolKey newKey = (DbConnectionPoolKey)((ICloneable)key).Clone();
83-
newKey.ConnectionString = expandedConnectionString;
84-
return GetConnectionPoolGroup(newKey, null, ref userConnectionOptions);
85-
}
86-
}
87-
88-
if (poolOptions == null)
89-
{
90-
if (connectionPoolGroup != null)
91-
{
92-
// reusing existing pool option in case user originally used SetConnectionPoolOptions
93-
poolOptions = connectionPoolGroup.PoolGroupOptions;
94-
}
95-
else
96-
{
97-
// Note: may return null for non-pooled connections
98-
poolOptions = CreateConnectionPoolGroupOptions(connectionOptions);
99-
}
100-
}
101-
102-
lock (this)
103-
{
104-
connectionPoolGroups = _connectionPoolGroups;
105-
if (!connectionPoolGroups.TryGetValue(key, out connectionPoolGroup))
106-
{
107-
DbConnectionPoolGroup newConnectionPoolGroup = new DbConnectionPoolGroup(connectionOptions, key, poolOptions);
108-
newConnectionPoolGroup.ProviderInfo = CreateConnectionPoolGroupProviderInfo(connectionOptions);
109-
110-
// build new dictionary with space for new connection string
111-
Dictionary<DbConnectionPoolKey, DbConnectionPoolGroup> newConnectionPoolGroups = new Dictionary<DbConnectionPoolKey, DbConnectionPoolGroup>(1 + connectionPoolGroups.Count);
112-
foreach (KeyValuePair<DbConnectionPoolKey, DbConnectionPoolGroup> entry in connectionPoolGroups)
113-
{
114-
newConnectionPoolGroups.Add(entry.Key, entry.Value);
115-
}
116-
117-
// lock prevents race condition with PruneConnectionPoolGroups
118-
newConnectionPoolGroups.Add(key, newConnectionPoolGroup);
119-
120-
SqlClientEventSource.Metrics.EnterActiveConnectionPoolGroup();
121-
connectionPoolGroup = newConnectionPoolGroup;
122-
_connectionPoolGroups = newConnectionPoolGroups;
123-
}
124-
else
125-
{
126-
Debug.Assert(!connectionPoolGroup.IsDisabled, "Disabled pool entry discovered");
127-
}
128-
}
129-
Debug.Assert(connectionPoolGroup != null, "how did we not create a pool entry?");
130-
Debug.Assert(userConnectionOptions != null, "how did we not have user connection options?");
131-
}
132-
else if (userConnectionOptions == null)
133-
{
134-
userConnectionOptions = connectionPoolGroup.ConnectionOptions;
135-
}
136-
return connectionPoolGroup;
137-
}
138-
13950
internal DbMetaDataFactory GetMetaDataFactory(DbConnectionPoolGroup connectionPoolGroup, DbConnectionInternal internalConnection)
14051
{
14152
Debug.Assert(connectionPoolGroup != null, "connectionPoolGroup may not be null.");

src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlConnectionFactory.cs

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,103 @@ internal SqlInternalConnectionTds CreatePooledConnection(
161161
return newConnection;
162162
}
163163

164+
internal DbConnectionPoolGroup GetConnectionPoolGroup(
165+
DbConnectionPoolKey key,
166+
DbConnectionPoolGroupOptions poolOptions,
167+
ref DbConnectionOptions userConnectionOptions)
168+
{
169+
if (string.IsNullOrEmpty(key.ConnectionString))
170+
{
171+
return null;
172+
}
173+
174+
if (!_connectionPoolGroups.TryGetValue(key, out DbConnectionPoolGroup connectionPoolGroup) ||
175+
(connectionPoolGroup.IsDisabled && connectionPoolGroup.PoolGroupOptions != null))
176+
{
177+
// If we can't find an entry for the connection string in
178+
// our collection of pool entries, then we need to create a
179+
// new pool entry and add it to our collection.
180+
181+
DbConnectionOptions connectionOptions = CreateConnectionOptions(
182+
key.ConnectionString,
183+
userConnectionOptions);
184+
if (connectionOptions is null)
185+
{
186+
throw ADP.InternalConnectionError(ADP.ConnectionError.ConnectionOptionsMissing);
187+
}
188+
189+
if (userConnectionOptions is null)
190+
{
191+
// We only allow one expansion on the connection string
192+
userConnectionOptions = connectionOptions;
193+
string expandedConnectionString = connectionOptions.Expand();
194+
195+
// if the expanded string is same instance (default implementation), then use the already created options
196+
if ((object)expandedConnectionString != (object)key.ConnectionString)
197+
{
198+
// CONSIDER: caching the original string to reduce future parsing
199+
DbConnectionPoolKey newKey = (DbConnectionPoolKey)key.Clone();
200+
newKey.ConnectionString = expandedConnectionString;
201+
return GetConnectionPoolGroup(newKey, null, ref userConnectionOptions);
202+
}
203+
}
204+
205+
if (poolOptions is null)
206+
{
207+
if (connectionPoolGroup is not null)
208+
{
209+
// reusing existing pool option in case user originally used SetConnectionPoolOptions
210+
poolOptions = connectionPoolGroup.PoolGroupOptions;
211+
}
212+
else
213+
{
214+
// Note: may return null for non-pooled connections
215+
poolOptions = CreateConnectionPoolGroupOptions(connectionOptions);
216+
}
217+
}
218+
219+
lock (this)
220+
{
221+
if (!_connectionPoolGroups.TryGetValue(key, out connectionPoolGroup))
222+
{
223+
DbConnectionPoolGroup newConnectionPoolGroup =
224+
new DbConnectionPoolGroup(connectionOptions, key, poolOptions)
225+
{
226+
ProviderInfo = CreateConnectionPoolGroupProviderInfo(connectionOptions)
227+
};
228+
229+
// build new dictionary with space for new connection string
230+
Dictionary<DbConnectionPoolKey, DbConnectionPoolGroup> newConnectionPoolGroups =
231+
new Dictionary<DbConnectionPoolKey, DbConnectionPoolGroup>(1 + _connectionPoolGroups.Count);
232+
foreach (KeyValuePair<DbConnectionPoolKey, DbConnectionPoolGroup> entry in _connectionPoolGroups)
233+
{
234+
newConnectionPoolGroups.Add(entry.Key, entry.Value);
235+
}
236+
237+
// lock prevents race condition with PruneConnectionPoolGroups
238+
newConnectionPoolGroups.Add(key, newConnectionPoolGroup);
239+
240+
SqlClientEventSource.Metrics.EnterActiveConnectionPoolGroup();
241+
connectionPoolGroup = newConnectionPoolGroup;
242+
_connectionPoolGroups = newConnectionPoolGroups;
243+
}
244+
else
245+
{
246+
Debug.Assert(!connectionPoolGroup.IsDisabled, "Disabled pool entry discovered");
247+
}
248+
}
249+
250+
Debug.Assert(connectionPoolGroup != null, "how did we not create a pool entry?");
251+
Debug.Assert(userConnectionOptions != null, "how did we not have user connection options?");
252+
}
253+
else if (userConnectionOptions is null)
254+
{
255+
userConnectionOptions = connectionPoolGroup.ConnectionOptions;
256+
}
257+
258+
return connectionPoolGroup;
259+
}
260+
164261
internal void QueuePoolForRelease(IDbConnectionPool pool, bool clearing)
165262
{
166263
// Queue the pool up for release -- we'll clear it out and dispose of it as the last
@@ -705,6 +802,8 @@ private IDbConnectionPool GetConnectionPool(
705802
return connectionPool;
706803
}
707804

805+
806+
708807
private void TryGetConnectionCompletedContinuation(Task<DbConnectionInternal> task, object state)
709808
{
710809
// Decompose the state into the parameters we want

0 commit comments

Comments
 (0)