diff --git a/Snowflake.Data/Client/SnowflakeDbConnectionPool.cs b/Snowflake.Data/Client/SnowflakeDbConnectionPool.cs index 0355ddd92..40062af46 100644 --- a/Snowflake.Data/Client/SnowflakeDbConnectionPool.cs +++ b/Snowflake.Data/Client/SnowflakeDbConnectionPool.cs @@ -1,5 +1,5 @@ /* - * Copyright (c) 2012-2023 Snowflake Computing Inc. All rights reserved. + * Copyright (c) 2012-2024 Snowflake Computing Inc. All rights reserved. */ using System; @@ -29,25 +29,31 @@ private static IConnectionManager ConnectionManager return s_connectionManager; } } - + internal static SFSession GetSession(string connectionString, SecureString password) { s_logger.Debug($"SnowflakeDbConnectionPool::GetSession"); return ConnectionManager.GetSession(connectionString, password); } - + internal static Task 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)); + } + internal static SessionPool GetPool(string connectionString) { s_logger.Debug($"SnowflakeDbConnectionPool::GetPool"); return ConnectionManager.GetPool(connectionString); } - + internal static bool AddSession(SFSession session) { s_logger.Debug("SnowflakeDbConnectionPool::AddSession"); @@ -83,7 +89,7 @@ public static void SetTimeout(long connectionTimeout) s_logger.Debug("SnowflakeDbConnectionPool::SetTimeout"); ConnectionManager.SetTimeout(connectionTimeout); } - + public static long GetTimeout() { s_logger.Debug("SnowflakeDbConnectionPool::GetTimeout"); @@ -110,7 +116,7 @@ public static bool GetPooling() internal static void SetOldConnectionPoolVersion() // TODO: set to public once development of entire ConnectionPoolManager epic is complete { - SetConnectionPoolVersion(ConnectionPoolType.SingleConnectionCache); + SetConnectionPoolVersion(ConnectionPoolType.SingleConnectionCache); } internal static void SetConnectionPoolVersion(ConnectionPoolType requestedPoolType) diff --git a/Snowflake.Data/Client/SnowflakeDbSessionPool.cs b/Snowflake.Data/Client/SnowflakeDbSessionPool.cs new file mode 100644 index 000000000..c680ef7f4 --- /dev/null +++ b/Snowflake.Data/Client/SnowflakeDbSessionPool.cs @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2024 Snowflake Computing Inc. All rights reserved. + */ +using System; +using Snowflake.Data.Core.Session; + +namespace Snowflake.Data.Client +{ + public class SnowflakeDbSessionPool : IDisposable + { + private SessionPool _sessionPool; + + internal SnowflakeDbSessionPool(SessionPool sessionPool) + => _sessionPool = sessionPool ?? throw new NullReferenceException("SessionPool not provided!"); + public void Dispose() => _sessionPool = null; + + public void SetMaxPoolSize(int size) => _sessionPool.SetMaxPoolSize(size); + public int GetMaxPoolSize() => _sessionPool.GetMaxPoolSize(); + + public void SetTimeout(long seconds) => _sessionPool.SetTimeout(seconds); + public long GetTimeout() => _sessionPool.GetTimeout(); + + public int GetCurrentPoolSize() => _sessionPool.GetCurrentPoolSize(); + + public bool SetPooling(bool isEnable) => _sessionPool.SetPooling(isEnable); + public bool GetPooling() => _sessionPool.GetPooling(); + } +} diff --git a/Snowflake.Data/Core/Session/ConnectionCacheManager.cs b/Snowflake.Data/Core/Session/ConnectionCacheManager.cs index b7c885234..9a8514f10 100644 --- a/Snowflake.Data/Core/Session/ConnectionCacheManager.cs +++ b/Snowflake.Data/Core/Session/ConnectionCacheManager.cs @@ -1,5 +1,5 @@ /* - * Copyright (c) 2012-2023 Snowflake Computing Inc. All rights reserved. + * Copyright (c) 2012-2024 Snowflake Computing Inc. All rights reserved. */ using System.Security; @@ -25,5 +25,6 @@ public Task GetSessionAsync(string connectionString, SecureString pas public bool SetPooling(bool poolingEnabled) => _sessionPool.SetPooling(poolingEnabled); public bool GetPooling() => _sessionPool.GetPooling(); public SessionPool GetPool(string _) => _sessionPool; + public SessionPool GetPool(string _, SecureString __) => _sessionPool; } } diff --git a/Snowflake.Data/Core/Session/ConnectionPoolManager.cs b/Snowflake.Data/Core/Session/ConnectionPoolManager.cs index fae78a014..96e499bf3 100644 --- a/Snowflake.Data/Core/Session/ConnectionPoolManager.cs +++ b/Snowflake.Data/Core/Session/ConnectionPoolManager.cs @@ -1,5 +1,5 @@ /* - * Copyright (c) 2012-2023 Snowflake Computing Inc. All rights reserved. + * Copyright (c) 2012-2024 Snowflake Computing Inc. All rights reserved. */ using System; @@ -27,7 +27,7 @@ internal ConnectionPoolManager() _pools = new Dictionary(); } } - + public SFSession GetSession(string connectionString, SecureString password) { s_logger.Debug($"ConnectionPoolManager::GetSession"); @@ -57,11 +57,11 @@ public void ClearAllPools() s_logger.Debug("ConnectionPoolManager::ClearAllPools"); foreach (var sessionPool in _pools.Values) { - sessionPool.ClearSessions(); + sessionPool.ClearSessions(); } _pools.Clear(); } - + public void SetMaxPoolSize(int maxPoolSize) { throw s_operationNotAvailable; @@ -104,7 +104,7 @@ public bool SetPooling(bool poolingEnabled) throw s_operationNotAvailable; } - public bool GetPooling() + public bool GetPooling() { s_logger.Debug("ConnectionPoolManager::GetPooling"); var values = _pools.Values.Select(it => it.GetPooling()).Distinct().ToList(); @@ -113,11 +113,11 @@ public bool GetPooling() : throw new SnowflakeDbException(SFError.INCONSISTENT_RESULT_ERROR, "Multiple pools have different Pooling values"); } - internal SessionPool GetPool(string connectionString, SecureString password) + public SessionPool GetPool(string connectionString, SecureString password) { s_logger.Debug($"ConnectionPoolManager::GetPool"); var poolKey = GetPoolKey(connectionString); - + if (_pools.TryGetValue(poolKey, out var item)) return item; lock (s_poolsLock) @@ -134,6 +134,11 @@ internal SessionPool GetPool(string connectionString, SecureString password) public SessionPool GetPool(string connectionString) { s_logger.Debug($"ConnectionPoolManager::GetPool"); + if (!connectionString.ToLower().Contains("password=")) + { + s_logger.Error($"To obtain a pool a password must to be given with a connection string or SecureString parameter"); + throw new SnowflakeDbException(SFError.MISSING_CONNECTION_PROPERTY, "Could not provide the pool without the password"); + } return GetPool(connectionString, null); } diff --git a/Snowflake.Data/Core/Session/IConnectionManager.cs b/Snowflake.Data/Core/Session/IConnectionManager.cs index 247cbe2e6..01cfa3e8c 100644 --- a/Snowflake.Data/Core/Session/IConnectionManager.cs +++ b/Snowflake.Data/Core/Session/IConnectionManager.cs @@ -1,5 +1,5 @@ /* - * Copyright (c) 2012-2023 Snowflake Computing Inc. All rights reserved. + * Copyright (c) 2012-2024 Snowflake Computing Inc. All rights reserved. */ using System.Security; @@ -23,5 +23,6 @@ internal interface IConnectionManager bool SetPooling(bool poolingEnabled); bool GetPooling(); SessionPool GetPool(string connectionString); + SessionPool GetPool(string connectionString, SecureString password); } }