Skip to content

Commit e25b768

Browse files
authored
New pool scaffolding (#3352)
1 parent b8948f2 commit e25b768

16 files changed

+350
-195
lines changed

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,6 @@
9595
<Compile Include="$(CommonSourceRoot)\Microsoft\Data\ProviderBase\DbConnectionFactory.cs">
9696
<Link>Microsoft\Data\ProviderBase\DbConnectionFactory.cs</Link>
9797
</Compile>
98-
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\ConnectionPool\DbConnectionPool.cs">
99-
<Link>Microsoft\Data\SqlClient\ConnectionPool\DbConnectionPool.cs</Link>
100-
</Compile>
10198
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\ConnectionPool\DbConnectionPoolAuthenticationContext.cs">
10299
<Link>Microsoft\Data\SqlClient\ConnectionPool\DbConnectionPoolAuthenticationContext.cs</Link>
103100
</Compile>
@@ -125,8 +122,8 @@
125122
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\ConnectionPool\DbConnectionPoolState.cs">
126123
<Link>Microsoft\Data\SqlClient\ConnectionPool\DbConnectionPoolState.cs</Link>
127124
</Compile>
128-
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\ConnectionPool\WaitHandleDbConnectionPool.cs">
129-
<Link>Microsoft\Data\SqlClient\ConnectionPool\WaitHandleDbConnectionPool.cs</Link>
125+
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\ConnectionPool\IDbConnectionPool.cs">
126+
<Link>Microsoft\Data\SqlClient\ConnectionPool\IDbConnectionPool.cs</Link>
130127
</Compile>
131128
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\ConnectionPool\SqlConnectionPoolGroupProviderInfo.cs">
132129
<Link>Microsoft\Data\SqlClient\ConnectionPool\SqlConnectionPoolGroupProviderInfo.cs</Link>
@@ -137,6 +134,9 @@
137134
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\ConnectionPool\SqlConnectionPoolProviderInfo.cs">
138135
<Link>Microsoft\Data\SqlClient\ConnectionPool\SqlConnectionPoolProviderInfo.cs</Link>
139136
</Compile>
137+
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\ConnectionPool\WaitHandleDbConnectionPool.cs">
138+
<Link>Microsoft\Data\SqlClient\ConnectionPool\WaitHandleDbConnectionPool.cs</Link>
139+
</Compile>
140140
<Compile Include="$(CommonSourceRoot)Microsoft\Data\ProviderBase\DbMetaDataFactory.cs">
141141
<Link>Microsoft\Data\ProviderBase\DbMetaDataFactory.cs</Link>
142142
</Compile>

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ override public DbProviderFactory ProviderFactory
3232
}
3333
}
3434

35-
override protected DbConnectionInternal CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection)
35+
override protected DbConnectionInternal CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, object poolGroupProviderInfo, IDbConnectionPool pool, DbConnection owningConnection)
3636
{
3737
return CreateConnection(options, poolKey, poolGroupProviderInfo, pool, owningConnection, userOptions: null);
3838
}
3939

40-
override protected DbConnectionInternal CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)
40+
override protected DbConnectionInternal CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, object poolGroupProviderInfo, IDbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)
4141
{
4242
SqlConnectionString opt = (SqlConnectionString)options;
4343
SqlConnectionPoolKey key = (SqlConnectionPoolKey)poolKey;

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
@@ -211,7 +211,7 @@ internal bool IsDNSCachingBeforeRedirectSupported
211211
internal byte _tceVersionSupported;
212212

213213
// The pool that this connection is associated with, if at all it is.
214-
private DbConnectionPool _dbConnectionPool;
214+
private IDbConnectionPool _dbConnectionPool;
215215

216216
// This is used to preserve the authentication context object if we decide to cache it for subsequent connections in the same pool.
217217
// This will finally end up in _dbConnectionPool.AuthenticationContexts, but only after 1 successful login to SQL Server using this context.
@@ -453,7 +453,7 @@ internal SqlInternalConnectionTds(
453453
SessionData reconnectSessionData = null,
454454
bool applyTransientFaultHandling = false,
455455
string accessToken = null,
456-
DbConnectionPool pool = null,
456+
IDbConnectionPool pool = null,
457457
Func<SqlAuthenticationParameters, CancellationToken,
458458
Task<SqlAuthenticationToken>> accessTokenCallback = null) : base(connectionOptions)
459459
{

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -279,9 +279,6 @@
279279
<Compile Include="$(CommonSourceRoot)Microsoft\Data\ProviderBase\DbConnectionInternal.cs">
280280
<Link>Microsoft\Data\ProviderBase\DbConnectionInternal.cs</Link>
281281
</Compile>
282-
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\ConnectionPool\DbConnectionPool.cs">
283-
<Link>Microsoft\Data\SqlClient\ConnectionPool\DbConnectionPool.cs</Link>
284-
</Compile>
285282
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\ConnectionPool\DbConnectionPoolAuthenticationContext.cs">
286283
<Link>Microsoft\Data\SqlClient\ConnectionPool\DbConnectionPoolAuthenticationContext.cs</Link>
287284
</Compile>
@@ -312,8 +309,8 @@
312309
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\ConnectionPool\DbConnectionPoolState.cs">
313310
<Link>Microsoft\Data\SqlClient\ConnectionPool\DbConnectionPoolState.cs</Link>
314311
</Compile>
315-
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\ConnectionPool\WaitHandleDbConnectionPool.cs">
316-
<Link>Microsoft\Data\SqlClient\ConnectionPool\WaitHandleDbConnectionPool.cs</Link>
312+
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\ConnectionPool\IDbConnectionPool.cs">
313+
<Link>Microsoft\Data\SqlClient\ConnectionPool\IDbConnectionPool.cs</Link>
317314
</Compile>
318315
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\ConnectionPool\SqlConnectionPoolGroupProviderInfo.cs">
319316
<Link>Microsoft\Data\SqlClient\ConnectionPool\SqlConnectionPoolGroupProviderInfo.cs</Link>
@@ -324,6 +321,9 @@
324321
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\ConnectionPool\SqlConnectionPoolProviderInfo.cs">
325322
<Link>Microsoft\Data\SqlClient\ConnectionPool\SqlConnectionPoolProviderInfo.cs</Link>
326323
</Compile>
324+
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\ConnectionPool\WaitHandleDbConnectionPool.cs">
325+
<Link>Microsoft\Data\SqlClient\ConnectionPool\WaitHandleDbConnectionPool.cs</Link>
326+
</Compile>
327327
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\Diagnostics\SqlClientMetrics.cs">
328328
<Link>Microsoft\Data\SqlClient\Diagnostics\SqlClientMetrics.cs</Link>
329329
</Compile>

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ override public DbProviderFactory ProviderFactory
3333
}
3434
}
3535

36-
override protected DbConnectionInternal CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection)
36+
override protected DbConnectionInternal CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, object poolGroupProviderInfo, IDbConnectionPool pool, DbConnection owningConnection)
3737
{
3838
return CreateConnection(options, poolKey, poolGroupProviderInfo, pool, owningConnection, userOptions: null);
3939
}
4040

41-
override protected DbConnectionInternal CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)
41+
override protected DbConnectionInternal CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, object poolGroupProviderInfo, IDbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)
4242
{
4343
SqlConnectionString opt = (SqlConnectionString)options;
4444
SqlConnectionPoolKey key = (SqlConnectionPoolKey)poolKey;

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
@@ -211,7 +211,7 @@ internal bool IsDNSCachingBeforeRedirectSupported
211211
internal byte _tceVersionSupported;
212212

213213
// The pool that this connection is associated with, if at all it is.
214-
private DbConnectionPool _dbConnectionPool;
214+
private IDbConnectionPool _dbConnectionPool;
215215

216216
// This is used to preserve the authentication context object if we decide to cache it for subsequent connections in the same pool.
217217
// This will finally end up in _dbConnectionPool.AuthenticationContexts, but only after 1 successful login to SQL Server using this context.
@@ -464,7 +464,7 @@ internal SqlInternalConnectionTds(
464464
SessionData reconnectSessionData = null,
465465
bool applyTransientFaultHandling = false,
466466
string accessToken = null,
467-
DbConnectionPool pool = null,
467+
IDbConnectionPool pool = null,
468468
Func<SqlAuthenticationParameters, CancellationToken,
469469
Task<SqlAuthenticationToken>> accessTokenCallback = null) : base(connectionOptions)
470470
{

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
</PropertyGroup>
77

88
<ItemGroup>
9-
<Reference Include="System.Configuration" Condition="'$(TargetFramework)' == 'net462'"/>
9+
<Reference Include="System.Configuration" Condition="'$(TargetFramework)' == 'net462'" />
1010
<Reference Include="System.Transactions" Condition="'$(TargetFramework)' == 'net462'" />
1111
</ItemGroup>
1212
<ItemGroup>

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ namespace Microsoft.Data.ProviderBase
1818
internal abstract class DbConnectionFactory
1919
{
2020
private Dictionary<DbConnectionPoolKey, DbConnectionPoolGroup> _connectionPoolGroups;
21-
private readonly List<DbConnectionPool> _poolsToRelease;
21+
private readonly List<IDbConnectionPool> _poolsToRelease;
2222
private readonly List<DbConnectionPoolGroup> _poolGroupsToRelease;
2323
private readonly Timer _pruningTimer;
2424

@@ -37,7 +37,7 @@ internal abstract class DbConnectionFactory
3737
protected DbConnectionFactory()
3838
{
3939
_connectionPoolGroups = new Dictionary<DbConnectionPoolKey, DbConnectionPoolGroup>();
40-
_poolsToRelease = new List<DbConnectionPool>();
40+
_poolsToRelease = new List<IDbConnectionPool>();
4141
_poolGroupsToRelease = new List<DbConnectionPoolGroup>();
4242
_pruningTimer = CreatePruningTimer();
4343
}
@@ -122,7 +122,7 @@ internal DbConnectionInternal CreateNonPooledConnection(DbConnection owningConne
122122
return newConnection;
123123
}
124124

125-
internal DbConnectionInternal CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions)
125+
internal DbConnectionInternal CreatePooledConnection(IDbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions)
126126
{
127127
Debug.Assert(pool != null, "null pool?");
128128
DbConnectionPoolGroupProviderInfo poolGroupProviderInfo = pool.PoolGroup.ProviderInfo;
@@ -176,7 +176,7 @@ internal bool TryGetConnection(DbConnection owningConnection, TaskCompletionSour
176176
Debug.Assert(owningConnection != null, "null owningConnection?");
177177

178178
DbConnectionPoolGroup poolGroup;
179-
DbConnectionPool connectionPool;
179+
IDbConnectionPool connectionPool;
180180
connection = null;
181181

182182
// Work around race condition with clearing the pool between GetConnectionPool obtaining pool
@@ -371,7 +371,7 @@ private void TryGetConnectionCompletedContinuation(Task<DbConnectionInternal> ta
371371
}
372372
}
373373

374-
private DbConnectionPool GetConnectionPool(DbConnection owningObject, DbConnectionPoolGroup connectionPoolGroup)
374+
private IDbConnectionPool GetConnectionPool(DbConnection owningObject, DbConnectionPoolGroup connectionPoolGroup)
375375
{
376376
// if poolgroup is disabled, it will be replaced with a new entry
377377

@@ -402,7 +402,7 @@ private DbConnectionPool GetConnectionPool(DbConnection owningObject, DbConnecti
402402
Debug.Assert(connectionPoolGroup != null, "null connectionPoolGroup?");
403403
SetConnectionPoolGroup(owningObject, connectionPoolGroup);
404404
}
405-
DbConnectionPool connectionPool = connectionPoolGroup.GetConnectionPool(this);
405+
IDbConnectionPool connectionPool = connectionPoolGroup.GetConnectionPool(this);
406406
return connectionPool;
407407
}
408408

@@ -530,8 +530,8 @@ private void PruneConnectionPoolGroups(object state)
530530
{
531531
if (0 != _poolsToRelease.Count)
532532
{
533-
DbConnectionPool[] poolsToRelease = _poolsToRelease.ToArray();
534-
foreach (DbConnectionPool pool in poolsToRelease)
533+
IDbConnectionPool[] poolsToRelease = _poolsToRelease.ToArray();
534+
foreach (IDbConnectionPool pool in poolsToRelease)
535535
{
536536
if (pool != null)
537537
{
@@ -540,7 +540,7 @@ private void PruneConnectionPoolGroups(object state)
540540
if (0 == pool.Count)
541541
{
542542
_poolsToRelease.Remove(pool);
543-
SqlClientEventSource.Log.TryAdvancedTraceEvent("<prov.DbConnectionFactory.PruneConnectionPoolGroups|RES|INFO|CPOOL> {0}, ReleasePool={1}", ObjectID, pool.ObjectId);
543+
SqlClientEventSource.Log.TryAdvancedTraceEvent("<prov.DbConnectionFactory.PruneConnectionPoolGroups|RES|INFO|CPOOL> {0}, ReleasePool={1}", ObjectID, pool.Id);
544544

545545
SqlClientEventSource.Metrics.ExitInactiveConnectionPool();
546546
}
@@ -607,7 +607,7 @@ private void PruneConnectionPoolGroups(object state)
607607
}
608608
}
609609

610-
internal void QueuePoolForRelease(DbConnectionPool pool, bool clearing)
610+
internal void QueuePoolForRelease(IDbConnectionPool pool, bool clearing)
611611
{
612612
// Queue the pool up for release -- we'll clear it out and dispose
613613
// of it as the last part of the pruning timer callback so we don't
@@ -645,12 +645,12 @@ internal void QueuePoolGroupForRelease(DbConnectionPoolGroup poolGroup)
645645
SqlClientEventSource.Metrics.ExitActiveConnectionPoolGroup();
646646
}
647647

648-
virtual protected DbConnectionInternal CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)
648+
virtual protected DbConnectionInternal CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, object poolGroupProviderInfo, IDbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)
649649
{
650650
return CreateConnection(options, poolKey, poolGroupProviderInfo, pool, owningConnection);
651651
}
652652

653-
abstract protected DbConnectionInternal CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection);
653+
abstract protected DbConnectionInternal CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, object poolGroupProviderInfo, IDbConnectionPool pool, DbConnection owningConnection);
654654

655655
abstract protected DbConnectionOptions CreateConnectionOptions(string connectionString, DbConnectionOptions previous);
656656

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ internal bool IsEmancipated
108108
{
109109
// NOTE: There are race conditions between PrePush, PostPop and this
110110
// property getter -- only use this while this object is locked;
111-
// (DbConnectionPool.Clear and ReclaimEmancipatedObjects
111+
// (IDbConnectionPool.Clear and ReclaimEmancipatedObjects
112112
// do this for us)
113113

114114
// The functionality is as follows:
@@ -157,7 +157,7 @@ internal bool IsInPool
157157
/// <summary>
158158
/// The pooler that the connection came from (Pooled connections only)
159159
/// </summary>
160-
internal DbConnectionPool Pool { get; private set; }
160+
internal IDbConnectionPool Pool { get; private set; }
161161

162162
public abstract string ServerVersion { get; }
163163

@@ -393,7 +393,7 @@ internal void CleanupConnectionOnTransactionCompletion(Transaction transaction)
393393
{
394394
DetachTransaction(transaction, false);
395395

396-
DbConnectionPool pool = Pool;
396+
IDbConnectionPool pool = Pool;
397397
pool?.TransactionEnded(transaction, this);
398398
}
399399

@@ -454,7 +454,7 @@ internal virtual void CloseConnection(DbConnection owningObject, DbConnectionFac
454454
{
455455
PrepareForCloseConnection();
456456

457-
DbConnectionPool connectionPool = Pool;
457+
IDbConnectionPool connectionPool = Pool;
458458

459459
// Detach from enlisted transactions that are no longer active on close
460460
DetachCurrentTransactionIfEnded();
@@ -464,10 +464,10 @@ internal virtual void CloseConnection(DbConnection owningObject, DbConnectionFac
464464
// into the pool.
465465
if (connectionPool is not null)
466466
{
467-
// PutObject calls Deactivate for us...
468-
connectionPool.PutObject(this, owningObject);
467+
// ReturnInternalConnection calls Deactivate for us...
468+
connectionPool.ReturnInternalConnection(this, owningObject);
469469

470-
// NOTE: Before we leave the PutObject call, another thread may have
470+
// NOTE: Before we leave the ReturnInternalConnection call, another thread may have
471471
// already popped the connection from the pool, so don't expect to be
472472
// able to verify it.
473473
}
@@ -558,7 +558,7 @@ internal virtual void DelegatedTransactionEnded()
558558

559559
Deactivate(); // call it one more time just in case
560560

561-
DbConnectionPool pool = Pool;
561+
IDbConnectionPool pool = Pool;
562562

563563
if (pool == null)
564564
{
@@ -698,7 +698,7 @@ internal void MakeNonPooledObject(DbConnection owningObject)
698698
/// Used by DbConnectionFactory to indicate that this object IS part of a connection pool.
699699
/// </summary>
700700
/// <param name="connectionPool"></param>
701-
internal void MakePooledConnection(DbConnectionPool connectionPool)
701+
internal void MakePooledConnection(IDbConnectionPool connectionPool)
702702
{
703703
_createTime = DateTime.UtcNow;
704704
Pool = connectionPool;
@@ -717,7 +717,7 @@ internal virtual void OpenConnection(DbConnection outerConnection, DbConnectionF
717717

718718
internal void PostPop(DbConnection newOwner)
719719
{
720-
// Called by DbConnectionPool right after it pulls this from its pool, we take this
720+
// Called by IDbConnectionPool right after it pulls this from its pool, we take this
721721
// opportunity to ensure ownership and pool counts are legit.
722722
Debug.Assert(!IsEmancipated, "pooled object not in pool");
723723

@@ -757,7 +757,7 @@ internal virtual void PrepareForReplaceConnection()
757757

758758
internal void PrePush(object expectedOwner)
759759
{
760-
// Called by DbConnectionPool when we're about to be put into it's pool, we take this
760+
// Called by IDbConnectionPool when we're about to be put into it's pool, we take this
761761
// opportunity to ensure ownership and pool counts are legit.
762762

763763
// IMPORTANT NOTE: You must have taken a lock on the object before you call this method

0 commit comments

Comments
 (0)