-
Notifications
You must be signed in to change notification settings - Fork 140
/
SnowflakeDbConnectionPool.cs
168 lines (146 loc) · 6.13 KB
/
SnowflakeDbConnectionPool.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
/*
* Copyright (c) 2012-2024 Snowflake Computing Inc. All rights reserved.
*/
using System;
using System.Security;
using System.Threading;
using System.Threading.Tasks;
using Snowflake.Data.Core;
using Snowflake.Data.Core.Session;
using Snowflake.Data.Core.Tools;
using Snowflake.Data.Log;
namespace Snowflake.Data.Client
{
public class SnowflakeDbConnectionPool
{
private static readonly SFLogger s_logger = SFLoggerFactory.GetLogger<SnowflakeDbConnectionPool>();
private static readonly Object s_connectionManagerInstanceLock = new Object();
private static IConnectionManager s_connectionManager;
internal const ConnectionPoolType DefaultConnectionPoolType = ConnectionPoolType.MultipleConnectionPool;
private static IConnectionManager ConnectionManager
{
get
{
if (s_connectionManager != null)
return s_connectionManager;
SetConnectionPoolVersion(DefaultConnectionPoolType, false);
return s_connectionManager;
}
}
internal static SFSession GetSession(string connectionString, SecureString password)
{
s_logger.Debug($"SnowflakeDbConnectionPool::GetSession");
return ConnectionManager.GetSession(connectionString, password);
}
internal static Task<SFSession> GetSessionAsync(string connectionString, SecureString password, CancellationToken cancellationToken)
{
s_logger.Debug($"SnowflakeDbConnectionPool::GetSessionAsync");
return ConnectionManager.GetSessionAsync(connectionString, password, cancellationToken);
}
public static SnowflakeDbSessionPool GetPool(string connectionString, SecureString password)
{
s_logger.Debug($"SnowflakeDbConnectionPool::GetPool");
return new SnowflakeDbSessionPool(ConnectionManager.GetPool(connectionString, password));
}
public static SnowflakeDbSessionPool GetPool(string connectionString)
{
s_logger.Debug($"SnowflakeDbConnectionPool::GetPool");
return new SnowflakeDbSessionPool(ConnectionManager.GetPool(connectionString));
}
internal static SessionPool GetPoolInternal(string connectionString)
{
s_logger.Debug($"SnowflakeDbConnectionPool::GetPoolInternal");
return ConnectionManager.GetPool(connectionString);
}
internal static bool AddSession(SFSession session)
{
s_logger.Debug("SnowflakeDbConnectionPool::AddSession");
return ConnectionManager.AddSession(session);
}
internal static void ReleaseBusySession(SFSession session)
{
s_logger.Debug("SnowflakeDbConnectionPool::ReleaseBusySession");
ConnectionManager.ReleaseBusySession(session);
}
public static void ClearAllPools()
{
s_logger.Debug("SnowflakeDbConnectionPool::ClearAllPools");
ConnectionManager.ClearAllPools();
}
public static void SetMaxPoolSize(int maxPoolSize)
{
s_logger.Debug("SnowflakeDbConnectionPool::SetMaxPoolSize");
ConnectionManager.SetMaxPoolSize(maxPoolSize);
}
public static int GetMaxPoolSize()
{
s_logger.Debug("SnowflakeDbConnectionPool::GetMaxPoolSize");
return ConnectionManager.GetMaxPoolSize();
}
public static void SetTimeout(long connectionTimeout)
{
s_logger.Debug("SnowflakeDbConnectionPool::SetTimeout");
ConnectionManager.SetTimeout(connectionTimeout);
}
public static long GetTimeout()
{
s_logger.Debug("SnowflakeDbConnectionPool::GetTimeout");
return ConnectionManager.GetTimeout();
}
public static int GetCurrentPoolSize()
{
s_logger.Debug("SnowflakeDbConnectionPool::GetCurrentPoolSize");
return ConnectionManager.GetCurrentPoolSize();
}
public static bool SetPooling(bool isEnable)
{
s_logger.Debug("SnowflakeDbConnectionPool::SetPooling");
return ConnectionManager.SetPooling(isEnable);
}
public static bool GetPooling()
{
s_logger.Debug("SnowflakeDbConnectionPool::GetPooling");
return ConnectionManager.GetPooling();
}
public static void SetOldConnectionPoolVersion()
{
ForceConnectionPoolVersion(ConnectionPoolType.SingleConnectionCache);
}
private static void SetConnectionPoolVersion(ConnectionPoolType requestedPoolType, bool force)
{
lock (s_connectionManagerInstanceLock)
{
if (s_connectionManager != null && !force)
return;
Diagnostics.LogDiagnostics();
s_connectionManager?.ClearAllPools();
if (requestedPoolType == ConnectionPoolType.MultipleConnectionPool)
{
s_connectionManager = new ConnectionPoolManager();
s_logger.Info("SnowflakeDbConnectionPool - multiple connection pools enabled");
}
if (requestedPoolType == ConnectionPoolType.SingleConnectionCache)
{
s_connectionManager = new ConnectionCacheManager();
s_logger.Warn("SnowflakeDbConnectionPool - connection cache enabled");
}
}
}
internal static void ForceConnectionPoolVersion(ConnectionPoolType requestedPoolType)
{
SetConnectionPoolVersion(requestedPoolType, true);
}
internal static ConnectionPoolType GetConnectionPoolVersion()
{
if (ConnectionManager != null)
{
switch (ConnectionManager)
{
case ConnectionCacheManager _: return ConnectionPoolType.SingleConnectionCache;
case ConnectionPoolManager _: return ConnectionPoolType.MultipleConnectionPool;
}
}
return DefaultConnectionPoolType;
}
}
}