From 22d32dd995c25c03681dafd47825d567ccffcfed Mon Sep 17 00:00:00 2001 From: Piotr Bulawa Date: Thu, 9 Nov 2023 10:19:22 +0100 Subject: [PATCH] SNOW-957736: Fix SnowflakeDbConnection.Dispose behaviour (#807) --- .../Client/SnowflakeDbConnection.cs | 35 ++++++++++++------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/Snowflake.Data/Client/SnowflakeDbConnection.cs b/Snowflake.Data/Client/SnowflakeDbConnection.cs index 36c35011f..b773a0150 100755 --- a/Snowflake.Data/Client/SnowflakeDbConnection.cs +++ b/Snowflake.Data/Client/SnowflakeDbConnection.cs @@ -26,7 +26,7 @@ public class SnowflakeDbConnection : DbConnection internal int _connectionTimeout; - private bool disposed = false; + private bool _disposed = false; private static Mutex _arraybindingMutex = new Mutex(); @@ -361,21 +361,30 @@ protected override DbCommand CreateDbCommand() protected override void Dispose(bool disposing) { - if (disposed) - return; - - try - { - this.Close(); - } - catch (Exception ex) + if (!_disposed) { - // Prevent an exception from being thrown when disposing of this object - logger.Error("Unable to close connection", ex); + if (disposing) + { + try + { + Close(); + } + catch (Exception ex) + { + // Prevent an exception from being thrown when disposing of this object + logger.Error("Unable to close connection", ex); + } + } + else + { + SfSession?.close(); + SfSession = null; + _connectionState = ConnectionState.Closed; + } + + _disposed = true; } - disposed = true; - base.Dispose(disposing); }