3
3
// See the LICENSE file in the project root for more information.
4
4
5
5
using System ;
6
+ using System . Collections . Generic ;
6
7
using System . Data . Common ;
7
8
using System . Diagnostics ;
8
9
using System . IO ;
9
10
using System . Reflection ;
11
+ using System . Threading ;
12
+ using System . Threading . Tasks ;
10
13
using Microsoft . Data . Common ;
11
14
using Microsoft . Data . Common . ConnectionString ;
12
15
using Microsoft . Data . ProviderBase ;
@@ -20,22 +23,49 @@ namespace Microsoft.Data.SqlClient
20
23
{
21
24
internal sealed class SqlConnectionFactory : DbConnectionFactory
22
25
{
23
- internal static readonly SqlConnectionFactory SingletonInstance = new SqlConnectionFactory ( ) ;
26
+ #region Member Variables
24
27
25
- private SqlConnectionFactory ( ) : base ( )
28
+ private static readonly TimeSpan PruningDueTime = TimeSpan . FromMinutes ( 4 ) ;
29
+ private static readonly TimeSpan PruningPeriod = TimeSpan . FromSeconds ( 30 ) ;
30
+
31
+ // s_pendingOpenNonPooled is an array of tasks used to throttle creation of non-pooled
32
+ // connections to a maximum of Environment.ProcessorCount at a time.
33
+ private static Task < DbConnectionInternal > s_completedTask ;
34
+ private static int s_objectTypeCount ;
35
+ private static Task < DbConnectionInternal > [ ] s_pendingOpenNonPooled =
36
+ new Task < DbConnectionInternal > [ Environment . ProcessorCount ] ;
37
+ private static uint s_pendingOpenNonPooledNext = 0 ;
38
+
39
+ private readonly List < DbConnectionPoolGroup > _poolGroupsToRelease ;
40
+ private readonly List < IDbConnectionPool > _poolsToRelease ;
41
+ private readonly Timer _pruningTimer ;
42
+ private Dictionary < DbConnectionPoolKey , DbConnectionPoolGroup > _connectionPoolGroups ;
43
+
44
+ #endregion
45
+
46
+ #region Constructors
47
+
48
+ private SqlConnectionFactory ( )
26
49
{
50
+ _connectionPoolGroups = new Dictionary < DbConnectionPoolKey , DbConnectionPoolGroup > ( ) ;
51
+ _poolsToRelease = new List < IDbConnectionPool > ( ) ;
52
+ _poolGroupsToRelease = new List < DbConnectionPoolGroup > ( ) ;
53
+ _pruningTimer = ADP . UnsafeCreateTimer (
54
+ new TimerCallback ( PruneConnectionPoolGroups ) ,
55
+ state : null ,
56
+ PruningDueTime ,
57
+ PruningPeriod ) ;
58
+
27
59
#if NET
28
60
SubscribeToAssemblyLoadContextUnload ( ) ;
29
61
#endif
30
62
}
63
+
64
+ #endregion
31
65
32
- public override DbProviderFactory ProviderFactory
33
- {
34
- get
35
- {
36
- return SqlClientFactory . Instance ;
37
- }
38
- }
66
+ public static readonly SqlConnectionFactory SingletonInstance = new SqlConnectionFactory ( ) ;
67
+
68
+ public DbProviderFactory ProviderFactory => SqlClientFactory . Instance ;
39
69
40
70
protected override DbConnectionInternal CreateConnection (
41
71
DbConnectionOptions options ,
0 commit comments