diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlCommand.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlCommand.cs index bd41fbace4..fa15f3e368 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlCommand.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlCommand.cs @@ -671,67 +671,71 @@ override public void Prepare() // between entry into Execute* API and the thread obtaining the stateObject. _pendingCancel = false; - SqlStatistics statistics = null; - statistics = SqlStatistics.StartTimer(Statistics); + try + { + statistics = SqlStatistics.StartTimer(Statistics); - // only prepare if batch with parameters - if ( - this.IsPrepared && !this.IsDirty - || (this.CommandType == CommandType.StoredProcedure) - || ( - (System.Data.CommandType.Text == this.CommandType) - && (0 == GetParameterCount(_parameters)) - ) + // only prepare if batch with parameters + if ( + this.IsPrepared && !this.IsDirty + || (this.CommandType == CommandType.StoredProcedure) + || ( + (System.Data.CommandType.Text == this.CommandType) + && (0 == GetParameterCount(_parameters)) + ) - ) - { - if (null != Statistics) + ) { - Statistics.SafeIncrement(ref Statistics._prepares); + if (null != Statistics) + { + Statistics.SafeIncrement(ref Statistics._prepares); + } + _hiddenPrepare = false; } - _hiddenPrepare = false; - } - else - { - // Validate the command outside of the try\catch to avoid putting the _stateObj on error - ValidateCommand(isAsync: false); - - bool processFinallyBlock = true; - try + else { - // NOTE: The state object isn't actually needed for this, but it is still here for back-compat (since it does a bunch of checks) - GetStateObject(); + // Validate the command outside of the try\catch to avoid putting the _stateObj on error + ValidateCommand(isAsync: false); - // Loop through parameters ensuring that we do not have unspecified types, sizes, scales, or precisions - if (null != _parameters) + bool processFinallyBlock = true; + try { - int count = _parameters.Count; - for (int i = 0; i < count; ++i) + // NOTE: The state object isn't actually needed for this, but it is still here for back-compat (since it does a bunch of checks) + GetStateObject(); + + // Loop through parameters ensuring that we do not have unspecified types, sizes, scales, or precisions + if (null != _parameters) { - _parameters[i].Prepare(this); + int count = _parameters.Count; + for (int i = 0; i < count; ++i) + { + _parameters[i].Prepare(this); + } } - } - InternalPrepare(); - } - catch (Exception e) - { - processFinallyBlock = ADP.IsCatchableExceptionType(e); - throw; - } - finally - { - if (processFinallyBlock) + InternalPrepare(); + } + catch (Exception e) + { + processFinallyBlock = ADP.IsCatchableExceptionType(e); + throw; + } + finally { - _hiddenPrepare = false; // The command is now officially prepared + if (processFinallyBlock) + { + _hiddenPrepare = false; // The command is now officially prepared - ReliablePutStateObject(); + ReliablePutStateObject(); + } } } } - - SqlStatistics.StopTimer(statistics); + finally + { + SqlStatistics.StopTimer(statistics); + } } private void InternalPrepare() diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParser.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParser.cs index 86cc1fd328..b871e6f420 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParser.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParser.cs @@ -1018,6 +1018,12 @@ internal void Disconnect() _pMarsPhysicalConObj = null; } } + + _resetConnectionEvent?.Dispose(); + _resetConnectionEvent = null; + + _defaultEncoding = null; + _defaultCollation = null; } // Fires a single InfoMessageEvent