From 8f576679f1f18e3f64760001d993baf245f0f5d4 Mon Sep 17 00:00:00 2001 From: Tianyi Chen Date: Thu, 4 Apr 2019 12:46:30 -0700 Subject: [PATCH] Fix bugs for Dispose to make it more robust. --- Snowflake.Data/Client/SnowflakeDbConnection.cs | 16 +++++++++++++++- Snowflake.Data/Client/SnowflakeDbTransaction.cs | 8 ++++++-- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/Snowflake.Data/Client/SnowflakeDbConnection.cs b/Snowflake.Data/Client/SnowflakeDbConnection.cs index 183085437..02dc99d32 100755 --- a/Snowflake.Data/Client/SnowflakeDbConnection.cs +++ b/Snowflake.Data/Client/SnowflakeDbConnection.cs @@ -45,6 +45,11 @@ public SecureString Password get; set; } + public bool IsOpen() + { + return _connectionState == ConnectionState.Open; + } + public override string Database => _connectionState == ConnectionState.Open ? SfSession.database : string.Empty; public override int ConnectionTimeout => this._connectionTimeout; @@ -100,7 +105,16 @@ public override void Open() { logger.Debug("Open Connection."); SetSession(); - SfSession.Open(); + try + { + SfSession.Open(); + } + catch (Exception e) + { + // Otherwise when Dispose() is called, the close request would timeout. + _connectionState = ConnectionState.Closed; + throw e; + } OnSessionEstablished(); } diff --git a/Snowflake.Data/Client/SnowflakeDbTransaction.cs b/Snowflake.Data/Client/SnowflakeDbTransaction.cs index ae499f68f..fe6ed2a2b 100755 --- a/Snowflake.Data/Client/SnowflakeDbTransaction.cs +++ b/Snowflake.Data/Client/SnowflakeDbTransaction.cs @@ -79,8 +79,12 @@ protected override void Dispose(bool disposing) if (disposed) return; - // Snowflake can handle a rollback for a rollback without any transaction - this.Rollback(); + // Rollback the uncommitted transaction when the connection is open + if (connection != null && connection.IsOpen()) + { + // When there is no uncommitted transaction, Snowflake would just ignore the rollback request; + this.Rollback(); + } disposed = true; base.Dispose(disposing);