From a65c00fbd0f79ad4824a49a6b8cbdda728e0f083 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Hofman?= Date: Thu, 5 Oct 2023 15:50:30 +0200 Subject: [PATCH 01/20] SNOW-902611 move control over creation of session to session pool --- .../Client/SnowflakeDbConnection.cs | 63 ++++-------- .../Client/SnowflakeDbConnectionPool.cs | 16 ++- Snowflake.Data/Core/Session/SessionPool.cs | 98 ++++++++++++++++++- 3 files changed, 126 insertions(+), 51 deletions(-) diff --git a/Snowflake.Data/Client/SnowflakeDbConnection.cs b/Snowflake.Data/Client/SnowflakeDbConnection.cs index 615bcc879..8690aa807 100755 --- a/Snowflake.Data/Client/SnowflakeDbConnection.cs +++ b/Snowflake.Data/Client/SnowflakeDbConnection.cs @@ -246,39 +246,21 @@ public override void Open() logger.Debug($"Open with a connection already opened: {_connectionState}"); return; } - SfSession = SnowflakeDbConnectionPool.GetSession(this.ConnectionString); - if (SfSession != null) + try { + OnSessionConnecting(); + SfSession = SnowflakeDbConnectionPool.GetSession(ConnectionString, Password); + if (SfSession == null) + throw new SnowflakeDbException(SFError.INTERNAL_ERROR, "Could not open session"); logger.Debug($"Connection open with pooled session: {SfSession.sessionId}"); + OnSessionEstablished(); } - else + catch (Exception e) { - SetSession(); - try - { - SfSession.Open(); - } - catch (Exception e) - { - // Otherwise when Dispose() is called, the close request would timeout. - _connectionState = ConnectionState.Closed; - logger.Error("Unable to connect", e); - if (!(e.GetType() == typeof(SnowflakeDbException))) - { - throw - new SnowflakeDbException( - e, - SnowflakeDbException.CONNECTION_FAILURE_SSTATE, - SFError.INTERNAL_ERROR, - "Unable to connect. " + e.Message); - } - else - { - throw; - } - } + _connectionState = ConnectionState.Closed; + logger.Error(e.Message); + throw; } - OnSessionEstablished(); } public override Task OpenAsync(CancellationToken cancellationToken) @@ -289,19 +271,11 @@ public override Task OpenAsync(CancellationToken cancellationToken) logger.Debug($"Open with a connection already opened: {_connectionState}"); return Task.CompletedTask; } - SfSession = SnowflakeDbConnectionPool.GetSession(this.ConnectionString); - if (SfSession != null) - { - logger.Debug($"Connection open with pooled session: {SfSession.sessionId}"); - OnSessionEstablished(); - return Task.CompletedTask; - } - registerConnectionCancellationCallback(cancellationToken); - SetSession(); - - return SfSession.OpenAsync(cancellationToken).ContinueWith( - previousTask => + OnSessionConnecting(); + return SnowflakeDbConnectionPool + .GetSessionAsync(ConnectionString, Password, cancellationToken) + .ContinueWith(previousTask => { if (previousTask.IsFaulted) { @@ -322,8 +296,9 @@ public override Task OpenAsync(CancellationToken cancellationToken) } else { - logger.Debug("All good"); // Only continue if the session was opened successfully + SfSession = previousTask.Result; + logger.Debug($"Connection open with pooled session: {SfSession.sessionId}"); OnSessionEstablished(); } }, @@ -349,15 +324,15 @@ public void SetArrayBindStageCreated() /// Create a new SFsession with the connection string settings. /// /// If the connection string can't be processed - private void SetSession() + private void OnSessionConnecting() { - SfSession = new SFSession(ConnectionString, Password); - _connectionTimeout = (int)SfSession.connectionTimeout.TotalSeconds; + // SfSession = new SFSession(ConnectionString, Password); _connectionState = ConnectionState.Connecting; } private void OnSessionEstablished() { + _connectionTimeout = (int)SfSession.connectionTimeout.TotalSeconds; _connectionState = ConnectionState.Open; } diff --git a/Snowflake.Data/Client/SnowflakeDbConnectionPool.cs b/Snowflake.Data/Client/SnowflakeDbConnectionPool.cs index 8e0093262..81cb3528a 100644 --- a/Snowflake.Data/Client/SnowflakeDbConnectionPool.cs +++ b/Snowflake.Data/Client/SnowflakeDbConnectionPool.cs @@ -1,4 +1,7 @@ -using Snowflake.Data.Core; +using System.Security; +using System.Threading; +using System.Threading.Tasks; +using Snowflake.Data.Core; using Snowflake.Data.Core.Session; using Snowflake.Data.Log; @@ -8,12 +11,17 @@ public class SnowflakeDbConnectionPool { private static readonly SFLogger s_logger = SFLoggerFactory.GetLogger(); - internal static SFSession GetSession(string connStr) + internal static SFSession GetSession(string connStr, SecureString password) { s_logger.Debug("SnowflakeDbConnectionPool::GetSession"); - return SessionPoolSingleton.Instance.GetSession(connStr); + return SessionPoolSingleton.Instance.GetSession(connStr, password); } - + + internal static Task GetSessionAsync(string connStr, SecureString password, CancellationToken cancellationToken) + { + return SessionPoolSingleton.Instance.GetSessionAsync(connStr, password, cancellationToken); + } + internal static bool AddSession(SFSession session) { s_logger.Debug("SnowflakeDbConnectionPool::AddSession"); diff --git a/Snowflake.Data/Core/Session/SessionPool.cs b/Snowflake.Data/Core/Session/SessionPool.cs index 092135e47..dd8cff7f2 100644 --- a/Snowflake.Data/Core/Session/SessionPool.cs +++ b/Snowflake.Data/Core/Session/SessionPool.cs @@ -1,6 +1,10 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Security; +using System.Threading; +using System.Threading.Tasks; +using Snowflake.Data.Client; using Snowflake.Data.Log; namespace Snowflake.Data.Core.Session @@ -70,11 +74,11 @@ private void CleanExpiredSessions() } } - internal SFSession GetSession(string connStr) + internal SFSession GetSession(string connStr, SecureString password) { s_logger.Debug("SessionPool::GetSession"); if (!_pooling) - return null; + return NewSession(connStr, password); lock (s_sessionPoolLock) { for (int i = 0; i < _sessionPool.Count; i++) @@ -97,8 +101,96 @@ internal SFSession GetSession(string connStr) } } } - return null; + return NewSession(connStr, password); } + + internal Task GetSessionAsync(string connStr, SecureString password, CancellationToken cancellationToken) + { + s_logger.Debug("SessionPool::GetSession"); + if (!_pooling) + return Task.FromResult(NewSession(connStr, password)); + lock (s_sessionPoolLock) + { + for (int i = 0; i < _sessionPool.Count; i++) + { + if (_sessionPool[i].connStr.Equals(connStr)) + { + SFSession session = _sessionPool[i]; + _sessionPool.RemoveAt(i); + long timeNow = DateTimeOffset.UtcNow.ToUnixTimeSeconds(); + if (session.IsExpired(_timeout, timeNow)) + { + session.close(); + i--; + } + else + { + s_logger.Debug($"reuse pooled session with sid {session.sessionId}"); + return Task.FromResult(session); + } + } + } + } + return NewSessionAsync(connStr, password, cancellationToken); + } + + internal SFSession NewSession(String connectionString, SecureString password) + { + try + { + var session = new SFSession(connectionString, password); + session.Open(); + return session; + } + catch (Exception e) + { + // Otherwise when Dispose() is called, the close request would timeout. + if (!(e is SnowflakeDbException)) + throw; + throw new SnowflakeDbException( + e, + SnowflakeDbException.CONNECTION_FAILURE_SSTATE, + SFError.INTERNAL_ERROR, + "Unable to connect. " + e.Message); + } + } + + internal Task NewSessionAsync(String connectionString, SecureString password, CancellationToken cancellationToken) + { + try + { + var session = new SFSession(connectionString, password); + return session + .OpenAsync(cancellationToken) + .ContinueWith(previousTask => + { + if (previousTask.IsCanceled) + cancellationToken.ThrowIfCancellationRequested(); + + if (previousTask.IsFaulted && previousTask.Exception != null) + throw previousTask.Exception; + + if (previousTask.IsFaulted) + throw new SnowflakeDbException( + SnowflakeDbException.CONNECTION_FAILURE_SSTATE, + SFError.INTERNAL_ERROR, + "Async open on connection failure"); + + return session; + }, cancellationToken); + } + catch (Exception e) + { + if (!(e is SnowflakeDbException)) + throw; + throw new SnowflakeDbException( + e, + SnowflakeDbException.CONNECTION_FAILURE_SSTATE, + SFError.INTERNAL_ERROR, + "Unable to connect. " + e.Message); + } + } + internal bool AddSession(SFSession session) { s_logger.Debug("SessionPool::AddSession"); From 3642b966463e4f021b64d13b079e18f32cbe1fd0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Hofman?= Date: Fri, 6 Oct 2023 10:06:09 +0200 Subject: [PATCH 02/20] SNOW-902611 bugfix on failed session open --- Snowflake.Data/Core/Session/SessionPool.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Snowflake.Data/Core/Session/SessionPool.cs b/Snowflake.Data/Core/Session/SessionPool.cs index dd8cff7f2..e4c7cd689 100644 --- a/Snowflake.Data/Core/Session/SessionPool.cs +++ b/Snowflake.Data/Core/Session/SessionPool.cs @@ -145,7 +145,7 @@ internal SFSession NewSession(String connectionString, SecureString password) catch (Exception e) { // Otherwise when Dispose() is called, the close request would timeout. - if (!(e is SnowflakeDbException)) + if (e is SnowflakeDbException) throw; throw new SnowflakeDbException( e, @@ -181,7 +181,7 @@ internal Task NewSessionAsync(String connectionString, SecureString p } catch (Exception e) { - if (!(e is SnowflakeDbException)) + if (e is SnowflakeDbException) throw; throw new SnowflakeDbException( e, From 52d40380d82a520ee9c7a663b968c7be3d3b8443 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Hofman?= Date: Fri, 6 Oct 2023 12:03:51 +0200 Subject: [PATCH 03/20] SNOW-902611 bugfixes for open failures --- Snowflake.Data/Client/SnowflakeDbConnection.cs | 13 +++++++++++-- Snowflake.Data/Core/Session/SessionPool.cs | 7 ++----- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/Snowflake.Data/Client/SnowflakeDbConnection.cs b/Snowflake.Data/Client/SnowflakeDbConnection.cs index 8690aa807..24c753ca0 100755 --- a/Snowflake.Data/Client/SnowflakeDbConnection.cs +++ b/Snowflake.Data/Client/SnowflakeDbConnection.cs @@ -257,9 +257,18 @@ public override void Open() } catch (Exception e) { + // Otherwise when Dispose() is called, the close request would timeout. _connectionState = ConnectionState.Closed; - logger.Error(e.Message); - throw; + logger.Error("Unable to connect: ", e); + if (e is SnowflakeDbException) + { + throw; + } + throw new SnowflakeDbException( + e, + SnowflakeDbException.CONNECTION_FAILURE_SSTATE, + SFError.INTERNAL_ERROR, + "Unable to connect. " + e.Message); } } diff --git a/Snowflake.Data/Core/Session/SessionPool.cs b/Snowflake.Data/Core/Session/SessionPool.cs index e4c7cd689..0be796727 100644 --- a/Snowflake.Data/Core/Session/SessionPool.cs +++ b/Snowflake.Data/Core/Session/SessionPool.cs @@ -164,9 +164,6 @@ internal Task NewSessionAsync(String connectionString, SecureString p .OpenAsync(cancellationToken) .ContinueWith(previousTask => { - if (previousTask.IsCanceled) - cancellationToken.ThrowIfCancellationRequested(); - if (previousTask.IsFaulted && previousTask.Exception != null) throw previousTask.Exception; @@ -174,10 +171,10 @@ internal Task NewSessionAsync(String connectionString, SecureString p throw new SnowflakeDbException( SnowflakeDbException.CONNECTION_FAILURE_SSTATE, SFError.INTERNAL_ERROR, - "Async open on connection failure"); + "Failure while opening session async"); return session; - }, cancellationToken); + }, TaskContinuationOptions.NotOnCanceled); } catch (Exception e) { From f53ae8bdd99e4895c73efbd3e1aa5edc58970f62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Hofman?= Date: Fri, 6 Oct 2023 12:40:57 +0200 Subject: [PATCH 04/20] SNOW-902611 bugfixe for async open failure --- Snowflake.Data/Core/Session/SessionPool.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Snowflake.Data/Core/Session/SessionPool.cs b/Snowflake.Data/Core/Session/SessionPool.cs index 0be796727..647ea41b7 100644 --- a/Snowflake.Data/Core/Session/SessionPool.cs +++ b/Snowflake.Data/Core/Session/SessionPool.cs @@ -108,7 +108,7 @@ internal Task GetSessionAsync(string connStr, SecureString password, { s_logger.Debug("SessionPool::GetSession"); if (!_pooling) - return Task.FromResult(NewSession(connStr, password)); + return NewSessionAsync(connStr, password, cancellationToken); lock (s_sessionPoolLock) { for (int i = 0; i < _sessionPool.Count; i++) From e1a14f5831eb92729696382703de33d991d3b7d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Hofman?= Date: Fri, 6 Oct 2023 16:14:41 +0200 Subject: [PATCH 05/20] SNOW-902611 bugfixe for async close failures --- Snowflake.Data.Tests/IntegrationTests/SFConnectionIT.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Snowflake.Data.Tests/IntegrationTests/SFConnectionIT.cs b/Snowflake.Data.Tests/IntegrationTests/SFConnectionIT.cs index 40bc46acc..70d4062c4 100644 --- a/Snowflake.Data.Tests/IntegrationTests/SFConnectionIT.cs +++ b/Snowflake.Data.Tests/IntegrationTests/SFConnectionIT.cs @@ -18,7 +18,7 @@ namespace Snowflake.Data.Tests.IntegrationTests using Snowflake.Data.Tests.Mock; using System.Runtime.InteropServices; - [TestFixture] + [TestFixture, NonParallelizable] class SFConnectionIT : SFBaseTest { private static readonly SFLogger s_logger = SFLoggerFactory.GetLogger(); @@ -28,6 +28,7 @@ public void TestBasicConnection() { using (IDbConnection conn = new SnowflakeDbConnection()) { + bool pooling = SnowflakeDbConnectionPool.GetPooling(); SnowflakeDbConnectionPool.SetPooling(false); conn.ConnectionString = ConnectionString; conn.Open(); @@ -46,6 +47,7 @@ public void TestBasicConnection() conn.Close(); Assert.AreEqual(ConnectionState.Closed, conn.State); + SnowflakeDbConnectionPool.SetPooling(pooling); } } From 482567102f083d4bb859a19d2beca868008a84cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Hofman?= Date: Fri, 6 Oct 2023 17:06:28 +0200 Subject: [PATCH 06/20] enabled pooling to check failures --- Snowflake.Data.Tests/IntegrationTests/SFConnectionIT.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Snowflake.Data.Tests/IntegrationTests/SFConnectionIT.cs b/Snowflake.Data.Tests/IntegrationTests/SFConnectionIT.cs index 70d4062c4..b4f3afb8c 100644 --- a/Snowflake.Data.Tests/IntegrationTests/SFConnectionIT.cs +++ b/Snowflake.Data.Tests/IntegrationTests/SFConnectionIT.cs @@ -1893,7 +1893,7 @@ public void TestCloseAsyncFailure() { using (var conn = new MockSnowflakeDbConnection(new MockCloseSessionException())) { - SnowflakeDbConnectionPool.SetPooling(false); + // SnowflakeDbConnectionPool.SetPooling(false); conn.ConnectionString = ConnectionString; Assert.AreEqual(conn.State, ConnectionState.Closed); Task task = null; From 09c7b3dec3d66aa15c0953f8b2c24c47c65d9dd5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Hofman?= Date: Fri, 6 Oct 2023 17:30:14 +0200 Subject: [PATCH 07/20] disable pooling to check failures --- Snowflake.Data.Tests/IntegrationTests/SFConnectionIT.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Snowflake.Data.Tests/IntegrationTests/SFConnectionIT.cs b/Snowflake.Data.Tests/IntegrationTests/SFConnectionIT.cs index b4f3afb8c..d4529a564 100644 --- a/Snowflake.Data.Tests/IntegrationTests/SFConnectionIT.cs +++ b/Snowflake.Data.Tests/IntegrationTests/SFConnectionIT.cs @@ -1755,6 +1755,7 @@ public void TestAsyncLoginTimeout() [Test, NonParallelizable] public void TestAsyncDefaultLoginTimeout() { + SnowflakeDbConnectionPool.SetPooling(false); using (var conn = new MockSnowflakeDbConnection()) { // unlimited retry count to trigger the timeout @@ -1891,9 +1892,9 @@ public void TestCloseAsync() [Test, NonParallelizable] public void TestCloseAsyncFailure() { + SnowflakeDbConnectionPool.SetPooling(false); using (var conn = new MockSnowflakeDbConnection(new MockCloseSessionException())) { - // SnowflakeDbConnectionPool.SetPooling(false); conn.ConnectionString = ConnectionString; Assert.AreEqual(conn.State, ConnectionState.Closed); Task task = null; From 1616fed077c2299612ba7d91cfd5b77285cd298c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Hofman?= Date: Mon, 9 Oct 2023 12:47:13 +0200 Subject: [PATCH 08/20] SNOW-902611 refactor improvements --- .../Client/SnowflakeDbConnection.cs | 5 -- Snowflake.Data/Core/Session/SessionPool.cs | 47 +++++++------------ 2 files changed, 18 insertions(+), 34 deletions(-) diff --git a/Snowflake.Data/Client/SnowflakeDbConnection.cs b/Snowflake.Data/Client/SnowflakeDbConnection.cs index 24c753ca0..b611589cf 100755 --- a/Snowflake.Data/Client/SnowflakeDbConnection.cs +++ b/Snowflake.Data/Client/SnowflakeDbConnection.cs @@ -329,13 +329,8 @@ public void SetArrayBindStageCreated() _isArrayBindStageCreated = true; } - /// - /// Create a new SFsession with the connection string settings. - /// - /// If the connection string can't be processed private void OnSessionConnecting() { - // SfSession = new SFSession(ConnectionString, Password); _connectionState = ConnectionState.Connecting; } diff --git a/Snowflake.Data/Core/Session/SessionPool.cs b/Snowflake.Data/Core/Session/SessionPool.cs index 647ea41b7..3238853bc 100644 --- a/Snowflake.Data/Core/Session/SessionPool.cs +++ b/Snowflake.Data/Core/Session/SessionPool.cs @@ -58,7 +58,7 @@ public static SessionPoolSingleton Instance private void CleanExpiredSessions() { - s_logger.Debug("SessionPool::cleanExpiredSessions"); + s_logger.Debug("SessionPool::CleanExpiredSessions"); lock (s_sessionPoolLock) { long timeNow = DateTimeOffset.UtcNow.ToUnixTimeSeconds(); @@ -79,36 +79,22 @@ internal SFSession GetSession(string connStr, SecureString password) s_logger.Debug("SessionPool::GetSession"); if (!_pooling) return NewSession(connStr, password); - lock (s_sessionPoolLock) - { - for (int i = 0; i < _sessionPool.Count; i++) - { - if (_sessionPool[i].connStr.Equals(connStr)) - { - SFSession session = _sessionPool[i]; - _sessionPool.RemoveAt(i); - long timeNow = DateTimeOffset.UtcNow.ToUnixTimeSeconds(); - if (session.IsExpired(_timeout, timeNow)) - { - session.close(); - i--; - } - else - { - s_logger.Debug($"reuse pooled session with sid {session.sessionId}"); - return session; - } - } - } - } - return NewSession(connStr, password); + SFSession session = GetIdleSession(connStr); + return session ?? NewSession(connStr, password); } internal Task GetSessionAsync(string connStr, SecureString password, CancellationToken cancellationToken) { - s_logger.Debug("SessionPool::GetSession"); + s_logger.Debug("SessionPool::GetSessionAsync"); if (!_pooling) return NewSessionAsync(connStr, password, cancellationToken); + SFSession session = GetIdleSession(connStr); + return session != null ? Task.FromResult(session) : NewSessionAsync(connStr, password, cancellationToken); + } + + private SFSession GetIdleSession(string connStr) + { + s_logger.Debug("SessionPool::GetIdleSession"); lock (s_sessionPoolLock) { for (int i = 0; i < _sessionPool.Count; i++) @@ -126,16 +112,17 @@ internal Task GetSessionAsync(string connStr, SecureString password, else { s_logger.Debug($"reuse pooled session with sid {session.sessionId}"); - return Task.FromResult(session); + return session; } } } } - return NewSessionAsync(connStr, password, cancellationToken); + return null; } - internal SFSession NewSession(String connectionString, SecureString password) + private SFSession NewSession(String connectionString, SecureString password) { + s_logger.Debug("SessionPool::NewSession"); try { var session = new SFSession(connectionString, password); @@ -155,8 +142,9 @@ internal SFSession NewSession(String connectionString, SecureString password) } } - internal Task NewSessionAsync(String connectionString, SecureString password, CancellationToken cancellationToken) + private Task NewSessionAsync(String connectionString, SecureString password, CancellationToken cancellationToken) { + s_logger.Debug("SessionPool::NewSessionAsync"); try { var session = new SFSession(connectionString, password); @@ -255,6 +243,7 @@ public int GetCurrentPoolSize() public bool SetPooling(bool isEnable) { + s_logger.Info($"SessionPool::SetPooling({isEnable})"); if (_pooling == isEnable) return false; _pooling = isEnable; From 2b09dea19902bffbb5189bbd6e6b016276bf171f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Hofman?= Date: Mon, 9 Oct 2023 13:21:03 +0200 Subject: [PATCH 09/20] SNOW-902611 teardown of pool settings changes in tests --- Snowflake.Data.Tests/App.config | 4 +-- .../IntegrationTests/PoolConfigRestorer.cs | 26 +++++++++++++++ .../IntegrationTests/SFConnectionIT.cs | 16 +++++++-- .../IntegrationTests/SFConnectionPoolT.cs | 33 ++++--------------- 4 files changed, 47 insertions(+), 32 deletions(-) create mode 100644 Snowflake.Data.Tests/IntegrationTests/PoolConfigRestorer.cs diff --git a/Snowflake.Data.Tests/App.config b/Snowflake.Data.Tests/App.config index 62a7928d6..3bfa38262 100755 --- a/Snowflake.Data.Tests/App.config +++ b/Snowflake.Data.Tests/App.config @@ -21,7 +21,7 @@ Copyright (c) 2012-2017 Snowflake Computing Inc. All rights reserved. - + @@ -31,7 +31,7 @@ Copyright (c) 2012-2017 Snowflake Computing Inc. All rights reserved. - + diff --git a/Snowflake.Data.Tests/IntegrationTests/PoolConfigRestorer.cs b/Snowflake.Data.Tests/IntegrationTests/PoolConfigRestorer.cs new file mode 100644 index 000000000..d1a822de1 --- /dev/null +++ b/Snowflake.Data.Tests/IntegrationTests/PoolConfigRestorer.cs @@ -0,0 +1,26 @@ +using Snowflake.Data.Client; + +namespace Snowflake.Data.Tests.IntegrationTests +{ + class PoolConfigRestorer { + private readonly bool _pooling; + private readonly long _timeout; + private readonly int _maxPoolSize; + + public PoolConfigRestorer() + { + _maxPoolSize = SnowflakeDbConnectionPool.GetMaxPoolSize(); + _timeout = SnowflakeDbConnectionPool.GetTimeout(); + _pooling = SnowflakeDbConnectionPool.GetPooling(); + } + + public void Reset() + { + SnowflakeDbConnectionPool.SetMaxPoolSize(_maxPoolSize); + SnowflakeDbConnectionPool.SetTimeout(_timeout); + SnowflakeDbConnectionPool.SetPooling(_pooling); + } + } + + +} \ No newline at end of file diff --git a/Snowflake.Data.Tests/IntegrationTests/SFConnectionIT.cs b/Snowflake.Data.Tests/IntegrationTests/SFConnectionIT.cs index d4529a564..e8b4fe0b8 100644 --- a/Snowflake.Data.Tests/IntegrationTests/SFConnectionIT.cs +++ b/Snowflake.Data.Tests/IntegrationTests/SFConnectionIT.cs @@ -22,13 +22,25 @@ namespace Snowflake.Data.Tests.IntegrationTests class SFConnectionIT : SFBaseTest { private static readonly SFLogger s_logger = SFLoggerFactory.GetLogger(); + private static readonly PoolConfigRestorer s_previousPoolConfig = new PoolConfigRestorer(); + [SetUp] + public void BeforeTest() + { + s_previousPoolConfig.Reset(); + SnowflakeDbConnectionPool.ClearAllPools(); + } + + [TearDown] + public void AfterTest() + { + s_previousPoolConfig.Reset(); + } [Test] public void TestBasicConnection() { using (IDbConnection conn = new SnowflakeDbConnection()) { - bool pooling = SnowflakeDbConnectionPool.GetPooling(); SnowflakeDbConnectionPool.SetPooling(false); conn.ConnectionString = ConnectionString; conn.Open(); @@ -47,7 +59,6 @@ public void TestBasicConnection() conn.Close(); Assert.AreEqual(ConnectionState.Closed, conn.State); - SnowflakeDbConnectionPool.SetPooling(pooling); } } @@ -1755,7 +1766,6 @@ public void TestAsyncLoginTimeout() [Test, NonParallelizable] public void TestAsyncDefaultLoginTimeout() { - SnowflakeDbConnectionPool.SetPooling(false); using (var conn = new MockSnowflakeDbConnection()) { // unlimited retry count to trigger the timeout diff --git a/Snowflake.Data.Tests/IntegrationTests/SFConnectionPoolT.cs b/Snowflake.Data.Tests/IntegrationTests/SFConnectionPoolT.cs index 005db6605..6f858d7cf 100644 --- a/Snowflake.Data.Tests/IntegrationTests/SFConnectionPoolT.cs +++ b/Snowflake.Data.Tests/IntegrationTests/SFConnectionPoolT.cs @@ -15,37 +15,16 @@ namespace Snowflake.Data.Tests.IntegrationTests using Snowflake.Data.Tests.Mock; using System.Data.Common; using Moq; - - class PoolConfig { - private readonly bool _pooling; - private readonly long _timeout; - private readonly int _maxPoolSize; - public PoolConfig() - { - _maxPoolSize = SnowflakeDbConnectionPool.GetMaxPoolSize(); - _timeout = SnowflakeDbConnectionPool.GetTimeout(); - _pooling = SnowflakeDbConnectionPool.GetPooling(); - } - - public void Reset() - { - SnowflakeDbConnectionPool.SetMaxPoolSize(_maxPoolSize); - SnowflakeDbConnectionPool.SetTimeout(_timeout); - SnowflakeDbConnectionPool.SetPooling(_pooling); - } - } - [TestFixture, NonParallelizable] class SFConnectionPoolT : SFBaseTest { - private static SFLogger logger = SFLoggerFactory.GetLogger(); - private static readonly PoolConfig previousPoolConfig = new PoolConfig(); + private static readonly PoolConfigRestorer s_previousPoolConfig = new PoolConfigRestorer(); [SetUp] public void BeforeTest() { - previousPoolConfig.Reset(); + s_previousPoolConfig.Reset(); SnowflakeDbConnectionPool.SetPooling(true); SnowflakeDbConnectionPool.ClearAllPools(); } @@ -53,7 +32,7 @@ public void BeforeTest() [TearDown] public void AfterTest() { - previousPoolConfig.Reset(); + s_previousPoolConfig.Reset(); } [OneTimeTearDown] @@ -435,12 +414,12 @@ public void TestConnectionPoolTurnOff() class SFConnectionPoolITAsync : SFBaseTestAsync { private static SFLogger logger = SFLoggerFactory.GetLogger(); - private static readonly PoolConfig previousPoolConfig = new PoolConfig(); + private static readonly PoolConfigRestorer s_previousPoolConfigRestorer = new PoolConfigRestorer(); [SetUp] public void BeforeTest() { - previousPoolConfig.Reset(); + s_previousPoolConfigRestorer.Reset(); SnowflakeDbConnectionPool.SetPooling(true); SnowflakeDbConnectionPool.ClearAllPools(); } @@ -448,7 +427,7 @@ public void BeforeTest() [TearDown] public void AfterTest() { - previousPoolConfig.Reset(); + s_previousPoolConfigRestorer.Reset(); } [OneTimeTearDown] From 05219685b7262fb2ea93973d3820f60463f2e87d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Hofman?= Date: Mon, 9 Oct 2023 13:45:26 +0200 Subject: [PATCH 10/20] SNOW-902611 removed unnecessary catch --- Snowflake.Data.Tests/App.config | 14 +++---- .../IntegrationTests/PoolConfigRestorer.cs | 4 +- Snowflake.Data/Core/Session/SFSession.cs | 2 +- Snowflake.Data/Core/Session/SessionPool.cs | 41 +++++++------------ 4 files changed, 23 insertions(+), 38 deletions(-) diff --git a/Snowflake.Data.Tests/App.config b/Snowflake.Data.Tests/App.config index 3bfa38262..a7920fad2 100755 --- a/Snowflake.Data.Tests/App.config +++ b/Snowflake.Data.Tests/App.config @@ -6,7 +6,7 @@ Copyright (c) 2012-2017 Snowflake Computing Inc. All rights reserved.
- + @@ -21,7 +21,7 @@ Copyright (c) 2012-2017 Snowflake Computing Inc. All rights reserved. - + @@ -29,9 +29,9 @@ Copyright (c) 2012-2017 Snowflake Computing Inc. All rights reserved. - + - + @@ -39,12 +39,12 @@ Copyright (c) 2012-2017 Snowflake Computing Inc. All rights reserved. - - +