Skip to content

Commit eb423fd

Browse files
committed
Add abstract class to define pool interface.
1 parent 6151075 commit eb423fd

File tree

4 files changed

+139
-34
lines changed

4 files changed

+139
-34
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@
7777
<Compile Include="$(CommonSourceRoot)\Microsoft\Data\ProviderBase\DbConnectionFactory.cs">
7878
<Link>Microsoft\Data\ProviderBase\DbConnectionFactory.cs</Link>
7979
</Compile>
80+
<Compile Include="$(CommonSourceRoot)Microsoft\Data\ProviderBase\IDbConnectionPool.cs">
81+
<Link>Microsoft\Data\ProviderBase\IDbConnectionPool.cs</Link>
82+
</Compile>
8083
<Compile Include="$(CommonSourceRoot)Microsoft\Data\ProviderBase\DbConnectionPool.cs">
8184
<Link>Microsoft\Data\ProviderBase\DbConnectionPool.cs</Link>
8285
</Compile>

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,9 @@
265265
<Compile Include="$(CommonSourceRoot)Microsoft\Data\ProviderBase\DbConnectionInternal.cs">
266266
<Link>Microsoft\Data\ProviderBase\DbConnectionInternal.cs</Link>
267267
</Compile>
268+
<Compile Include="$(CommonSourceRoot)Microsoft\Data\ProviderBase\IDbConnectionPool.cs">
269+
<Link>Microsoft\Data\ProviderBase\IDbConnectionPool.cs</Link>
270+
</Compile>
268271
<Compile Include="$(CommonSourceRoot)Microsoft\Data\ProviderBase\DbConnectionPool.cs">
269272
<Link>Microsoft\Data\ProviderBase\DbConnectionPool.cs</Link>
270273
</Compile>

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

Lines changed: 23 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
namespace Microsoft.Data.ProviderBase
2222
{
23-
internal sealed class DbConnectionPool
23+
internal sealed class DbConnectionPool : IDbConnectionPool
2424
{
2525
private enum State
2626
{
@@ -418,9 +418,6 @@ internal WaitHandle[] GetHandles(bool withCreate)
418418
private readonly List<DbConnectionInternal> _objectList;
419419
private int _totalObjects;
420420

421-
private static int _objectTypeCount; // EventSource counter
422-
internal readonly int _objectID = System.Threading.Interlocked.Increment(ref _objectTypeCount);
423-
424421
// only created by DbConnectionPoolGroup.GetConnectionPool
425422
internal DbConnectionPool(
426423
DbConnectionFactory connectionFactory,
@@ -474,17 +471,17 @@ private int CreationTimeout
474471
get { return PoolGroupOptions.CreationTimeout; }
475472
}
476473

477-
internal int Count
474+
internal override int Count
478475
{
479476
get { return _totalObjects; }
480477
}
481478

482-
internal DbConnectionFactory ConnectionFactory
479+
internal override DbConnectionFactory ConnectionFactory
483480
{
484481
get { return _connectionFactory; }
485482
}
486483

487-
internal bool ErrorOccurred
484+
internal override bool ErrorOccurred
488485
{
489486
get { return _errorOccurred; }
490487
}
@@ -494,7 +491,7 @@ private bool HasTransactionAffinity
494491
get { return PoolGroupOptions.HasTransactionAffinity; }
495492
}
496493

497-
internal TimeSpan LoadBalanceTimeout
494+
internal override TimeSpan LoadBalanceTimeout
498495
{
499496
get { return PoolGroupOptions.LoadBalanceTimeout; }
500497
}
@@ -522,12 +519,12 @@ private bool NeedToReplenish
522519
}
523520
}
524521

525-
internal DbConnectionPoolIdentity Identity
522+
internal override DbConnectionPoolIdentity Identity
526523
{
527524
get { return _identity; }
528525
}
529526

530-
internal bool IsRunning
527+
internal override bool IsRunning
531528
{
532529
get { return State.Running == _state; }
533530
}
@@ -542,48 +539,40 @@ private int MinPoolSize
542539
get { return PoolGroupOptions.MinPoolSize; }
543540
}
544541

545-
internal int ObjectID
546-
{
547-
get
548-
{
549-
return _objectID;
550-
}
551-
}
552-
553542
#if NETFRAMEWORK
554-
internal DbConnectionPoolCounters PerformanceCounters
543+
internal override DbConnectionPoolCounters PerformanceCounters
555544
{
556545
get { return _connectionFactory.PerformanceCounters; }
557546
}
558547
#endif
559548

560-
internal DbConnectionPoolGroup PoolGroup
549+
internal override DbConnectionPoolGroup PoolGroup
561550
{
562551
get { return _connectionPoolGroup; }
563552
}
564553

565-
internal DbConnectionPoolGroupOptions PoolGroupOptions
554+
internal override DbConnectionPoolGroupOptions PoolGroupOptions
566555
{
567556
get { return _connectionPoolGroupOptions; }
568557
}
569558

570-
internal DbConnectionPoolProviderInfo ProviderInfo
559+
internal override DbConnectionPoolProviderInfo ProviderInfo
571560
{
572561
get { return _connectionPoolProviderInfo; }
573562
}
574563

575564
/// <summary>
576565
/// Return the pooled authentication contexts.
577566
/// </summary>
578-
internal ConcurrentDictionary<DbConnectionPoolAuthenticationContextKey, DbConnectionPoolAuthenticationContext> AuthenticationContexts
567+
internal override ConcurrentDictionary<DbConnectionPoolAuthenticationContextKey, DbConnectionPoolAuthenticationContext> AuthenticationContexts
579568
{
580569
get
581570
{
582571
return _pooledDbAuthenticationContexts;
583572
}
584573
}
585574

586-
internal bool UseLoadBalancing
575+
internal override bool UseLoadBalancing
587576
{
588577
get { return PoolGroupOptions.UseLoadBalancing; }
589578
}
@@ -704,7 +693,7 @@ private void CleanupCallback(object state)
704693
QueuePoolCreateRequest();
705694
}
706695

707-
internal void Clear()
696+
internal override void Clear()
708697
{
709698
SqlClientEventSource.Log.TryPoolerTraceEvent("<prov.DbConnectionPool.Clear|RES|CPOOL> {0}, Clearing.", ObjectID);
710699
DbConnectionInternal obj;
@@ -1039,7 +1028,7 @@ private void DeactivateObject(DbConnectionInternal obj)
10391028
Debug.Assert(rootTxn == true || returnToGeneralPool == true || destroyObject == true);
10401029
}
10411030

1042-
internal void DestroyObject(DbConnectionInternal obj)
1031+
internal override void DestroyObject(DbConnectionInternal obj)
10431032
{
10441033
// A connection with a delegated transaction cannot be disposed of
10451034
// until the delegated transaction has actually completed. Instead,
@@ -1234,7 +1223,7 @@ private void WaitForPendingOpen()
12341223
} while (_pendingOpens.TryPeek(out next));
12351224
}
12361225

1237-
internal bool TryGetConnection(DbConnection owningObject, TaskCompletionSource<DbConnectionInternal> retry, DbConnectionOptions userOptions, out DbConnectionInternal connection)
1226+
internal override bool TryGetConnection(DbConnection owningObject, TaskCompletionSource<DbConnectionInternal> retry, DbConnectionOptions userOptions, out DbConnectionInternal connection)
12381227
{
12391228
uint waitForMultipleObjectsTimeout = 0;
12401229
bool allowCreate = false;
@@ -1516,7 +1505,7 @@ private void PrepareConnection(DbConnection owningObject, DbConnectionInternal o
15161505
/// <param name="userOptions">Options used to create the new connection</param>
15171506
/// <param name="oldConnection">Inner connection that will be replaced</param>
15181507
/// <returns>A new inner connection that is attached to the <paramref name="owningObject"/></returns>
1519-
internal DbConnectionInternal ReplaceConnection(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
1508+
internal override DbConnectionInternal ReplaceConnection(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
15201509
{
15211510
#if NETFRAMEWORK
15221511
PerformanceCounters.SoftConnectsPerSecond.Increment();
@@ -1752,7 +1741,7 @@ private void PoolCreateRequest(object state)
17521741
}
17531742
}
17541743

1755-
internal void PutNewObject(DbConnectionInternal obj)
1744+
internal override void PutNewObject(DbConnectionInternal obj)
17561745
{
17571746
Debug.Assert(obj != null, "why are we adding a null object to the pool?");
17581747

@@ -1768,7 +1757,7 @@ internal void PutNewObject(DbConnectionInternal obj)
17681757

17691758
}
17701759

1771-
internal void PutObject(DbConnectionInternal obj, object owningObject)
1760+
internal override void PutObject(DbConnectionInternal obj, object owningObject)
17721761
{
17731762
Debug.Assert(obj != null, "null obj?");
17741763

@@ -1800,7 +1789,7 @@ internal void PutObject(DbConnectionInternal obj, object owningObject)
18001789
DeactivateObject(obj);
18011790
}
18021791

1803-
internal void PutObjectFromTransactedPool(DbConnectionInternal obj)
1792+
internal override void PutObjectFromTransactedPool(DbConnectionInternal obj)
18041793
{
18051794
Debug.Assert(obj != null, "null pooledObject?");
18061795
Debug.Assert(obj.EnlistedTransaction == null, "pooledObject is still enlisted?");
@@ -1907,7 +1896,7 @@ private bool ReclaimEmancipatedObjects()
19071896
return emancipatedObjectFound;
19081897
}
19091898

1910-
internal void Startup()
1899+
internal override void Startup()
19111900
{
19121901
SqlClientEventSource.Log.TryPoolerTraceEvent("<prov.DbConnectionPool.Startup|RES|INFO|CPOOL> {0}, CleanupWait={1}", ObjectID, _cleanupWait);
19131902
_cleanupTimer = CreateCleanupTimer();
@@ -1918,7 +1907,7 @@ internal void Startup()
19181907
}
19191908
}
19201909

1921-
internal void Shutdown()
1910+
internal override void Shutdown()
19221911
{
19231912
SqlClientEventSource.Log.TryPoolerTraceEvent("<prov.DbConnectionPool.Shutdown|RES|INFO|CPOOL> {0}", ObjectID);
19241913
_state = State.ShuttingDown;
@@ -1936,7 +1925,7 @@ internal void Shutdown()
19361925
// that is implemented inside DbConnectionPool. This method's counterpart (PutTransactedObject) should
19371926
// only be called from DbConnectionPool.DeactivateObject and thus the plumbing to provide access to
19381927
// other objects is unnecessary (hence the asymmetry of Ended but no Begin)
1939-
internal void TransactionEnded(Transaction transaction, DbConnectionInternal transactedObject)
1928+
internal override void TransactionEnded(Transaction transaction, DbConnectionInternal transactedObject)
19401929
{
19411930
Debug.Assert(transaction != null, "null transaction?");
19421931
Debug.Assert(transactedObject != null, "null transactedObject?");
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System;
6+
using System.Collections.Concurrent;
7+
using System.Data.Common;
8+
using System.Threading.Tasks;
9+
using System.Transactions;
10+
using Microsoft.Data.Common;
11+
12+
namespace Microsoft.Data.ProviderBase
13+
{
14+
internal abstract class IDbConnectionPool
15+
{
16+
private static int _objectTypeCount; // EventSource counter
17+
internal readonly int _objectID = System.Threading.Interlocked.Increment(ref _objectTypeCount);
18+
19+
internal int ObjectID
20+
{
21+
get
22+
{
23+
return _objectID;
24+
}
25+
}
26+
27+
internal abstract int Count
28+
{
29+
get;
30+
}
31+
32+
internal abstract DbConnectionFactory ConnectionFactory
33+
{
34+
get;
35+
}
36+
37+
internal abstract bool ErrorOccurred
38+
{
39+
get;
40+
}
41+
42+
internal abstract TimeSpan LoadBalanceTimeout
43+
{
44+
get;
45+
}
46+
47+
internal abstract DbConnectionPoolIdentity Identity
48+
{
49+
get;
50+
}
51+
52+
internal abstract bool IsRunning
53+
{
54+
get;
55+
}
56+
57+
#if NETFRAMEWORK
58+
internal abstract DbConnectionPoolCounters PerformanceCounters
59+
{
60+
get;
61+
}
62+
#endif
63+
internal abstract DbConnectionPoolGroup PoolGroup
64+
{
65+
get;
66+
}
67+
68+
internal abstract DbConnectionPoolGroupOptions PoolGroupOptions
69+
{
70+
get;
71+
}
72+
73+
internal abstract DbConnectionPoolProviderInfo ProviderInfo
74+
{
75+
get;
76+
}
77+
78+
internal abstract ConcurrentDictionary<DbConnectionPoolAuthenticationContextKey, DbConnectionPoolAuthenticationContext> AuthenticationContexts
79+
{
80+
get;
81+
}
82+
83+
internal abstract bool UseLoadBalancing
84+
{
85+
get;
86+
}
87+
88+
internal abstract void Clear();
89+
90+
internal abstract void DestroyObject(DbConnectionInternal obj);
91+
92+
internal abstract bool TryGetConnection(DbConnection owningObject, TaskCompletionSource<DbConnectionInternal> retry, DbConnectionOptions userOptions, out DbConnectionInternal connection);
93+
94+
internal abstract DbConnectionInternal ReplaceConnection(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection);
95+
96+
internal abstract void PutNewObject(DbConnectionInternal obj);
97+
98+
internal abstract void PutObject(DbConnectionInternal obj, object owningObject);
99+
100+
internal abstract void PutObjectFromTransactedPool(DbConnectionInternal obj);
101+
102+
internal abstract void Startup();
103+
104+
internal abstract void Shutdown();
105+
106+
internal abstract void TransactionEnded(Transaction transaction, DbConnectionInternal transactedObject);
107+
108+
109+
}
110+
}

0 commit comments

Comments
 (0)