From b765c1bab675f413c7a86cc316f0c5ae1da277d9 Mon Sep 17 00:00:00 2001 From: Krzysztof Nozderko Date: Wed, 10 Apr 2024 03:12:59 -0700 Subject: [PATCH] changes from review --- README.md | 2 +- .../IntegrationTests/ConnectionPoolCommonIT.cs | 2 +- Snowflake.Data/Client/SnowflakeDbConnection.cs | 7 ++++--- Snowflake.Data/Core/Session/SessionPool.cs | 7 +++++++ 4 files changed, 13 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 15fac13e4..7e748c8a5 100644 --- a/README.md +++ b/README.md @@ -670,7 +670,7 @@ CancellationTokenSource cancellationTokenSource = new CancellationTokenSource() Evict the Connection -------------------- -For the open connection, call the `PreventPooling()` to make the connection not return to the pool. +For the open connection, call the `PreventPooling()` to mark the connection to be removed on close instead being still pooled. The busy sessions counter will be decreased when the connection is closed. Logging diff --git a/Snowflake.Data.Tests/IntegrationTests/ConnectionPoolCommonIT.cs b/Snowflake.Data.Tests/IntegrationTests/ConnectionPoolCommonIT.cs index ab55a9e35..796779883 100644 --- a/Snowflake.Data.Tests/IntegrationTests/ConnectionPoolCommonIT.cs +++ b/Snowflake.Data.Tests/IntegrationTests/ConnectionPoolCommonIT.cs @@ -119,7 +119,7 @@ public void TestFailWhenPreventingFromReturningToPoolNotOpenedConnection() var thrown = Assert.Throws(() => connection.PreventPooling()); // assert - Assert.That(thrown.Message, Does.Contain("Connection is not ready to prevent its session from returning to the pool")); + Assert.That(thrown.Message, Does.Contain("Session not yet created for this connection. Unable to prevent the session from pooling")); } } } diff --git a/Snowflake.Data/Client/SnowflakeDbConnection.cs b/Snowflake.Data/Client/SnowflakeDbConnection.cs index 1727f25e6..6d0fdfc17 100755 --- a/Snowflake.Data/Client/SnowflakeDbConnection.cs +++ b/Snowflake.Data/Client/SnowflakeDbConnection.cs @@ -113,11 +113,12 @@ public void PreventPooling() throw new Exception("Session not yet created for this connection. Unable to prevent the session from pooling"); } SfSession.SetPooling(false); + logger.Debug($"Session {SfSession.sessionId} marked not to be pooled any more"); } internal bool HasActiveExplicitTransaction() => ExplicitTransaction != null && ExplicitTransaction.IsActive; - private bool ReleaseOrReturnSessionToPool() + private bool TryToReturnSessionToPool() { var pooling = SnowflakeDbConnectionPool.GetPooling() && SfSession.GetPooling(); var transactionRollbackStatus = pooling ? TerminateTransactionForDirtyConnectionReturningToPool() : TransactionRollbackStatus.Undefined; @@ -178,7 +179,7 @@ public override void Close() logger.Debug("Close Connection."); if (IsNonClosedWithSession()) { - var returnedToPool = ReleaseOrReturnSessionToPool(); + var returnedToPool = TryToReturnSessionToPool(); if (!returnedToPool) { SfSession.close(); @@ -210,7 +211,7 @@ public virtual Task CloseAsync(CancellationToken cancellationToken) { if (IsNonClosedWithSession()) { - var returnedToPool = ReleaseOrReturnSessionToPool(); + var returnedToPool = TryToReturnSessionToPool(); if (returnedToPool) { _connectionState = ConnectionState.Closed; diff --git a/Snowflake.Data/Core/Session/SessionPool.cs b/Snowflake.Data/Core/Session/SessionPool.cs index 51170d876..6f1fdd18b 100644 --- a/Snowflake.Data/Core/Session/SessionPool.cs +++ b/Snowflake.Data/Core/Session/SessionPool.cs @@ -391,10 +391,17 @@ private Task NewSessionAsync(String connectionString, SecureString pa internal void ReleaseBusySession(SFSession session) { s_logger.Debug("SessionPool::ReleaseBusySession"); + int currentPoolSize; lock (_sessionPoolLock) { _busySessionsCounter.Decrease(); + currentPoolSize = GetCurrentPoolSize(); } + var currentSizeMessageOldPool = $"After releasing a busy session from the pool, the pool size is: {currentPoolSize}"; + var poolSizeMessage = IsMultiplePoolsVersion() + ? $"{currentSizeMessageOldPool} - pool identified by: {ConnectionString}" + : currentSizeMessageOldPool; + s_logger.Debug(poolSizeMessage); } internal bool AddSession(SFSession session, bool ensureMinPoolSize)