From 112d468e750d590a1655abaec8261d884d5f08ef Mon Sep 17 00:00:00 2001 From: v-kaywon Date: Thu, 5 Dec 2019 11:04:25 -0800 Subject: [PATCH] fix sqlstatistics and clientconnectionid --- .../src/System/Data/SqlClient/SqlConnection.cs | 5 ++++- .../src/System/Data/SqlClient/SqlStatistics.cs | 4 ++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/libraries/System.Data.SqlClient/src/System/Data/SqlClient/SqlConnection.cs b/src/libraries/System.Data.SqlClient/src/System/Data/SqlClient/SqlConnection.cs index f67ef15996e0f6..9c5d7458f21b7b 100644 --- a/src/libraries/System.Data.SqlClient/src/System/Data/SqlClient/SqlConnection.cs +++ b/src/libraries/System.Data.SqlClient/src/System/Data/SqlClient/SqlConnection.cs @@ -360,7 +360,9 @@ public Guid ClientConnectionId else { Task reconnectTask = _currentReconnectionTask; - if (reconnectTask != null && !reconnectTask.IsCompleted) + // Connection closed but previously open should return the correct ClientConnectionId + DbConnectionClosedPreviouslyOpened innerConnectionClosed = (InnerConnection as DbConnectionClosedPreviouslyOpened); + if ((reconnectTask != null && !reconnectTask.IsCompleted) || null != innerConnectionClosed) { return _originalConnectionId; } @@ -633,6 +635,7 @@ private void CloseInnerConnection() // The SqlInternalConnectionTds is set to OpenBusy during close, once this happens the cast below will fail and // the command will no longer be cancelable. It might be desirable to be able to cancel the close operation, but this is // outside of the scope of Whidbey RTM. See (SqlCommand::Cancel) for other lock. + _originalConnectionId = ClientConnectionId; InnerConnection.CloseConnection(this, ConnectionFactory); } diff --git a/src/libraries/System.Data.SqlClient/src/System/Data/SqlClient/SqlStatistics.cs b/src/libraries/System.Data.SqlClient/src/System/Data/SqlClient/SqlStatistics.cs index 017596642daeda..59a437f68d84d2 100644 --- a/src/libraries/System.Data.SqlClient/src/System/Data/SqlClient/SqlStatistics.cs +++ b/src/libraries/System.Data.SqlClient/src/System/Data/SqlClient/SqlStatistics.cs @@ -212,9 +212,9 @@ internal long SafeIncrement(ref long value) internal void UpdateStatistics() { // update connection time - if (_closeTimestamp >= _openTimestamp) + if (_closeTimestamp >= _openTimestamp && long.MaxValue > _closeTimestamp - _openTimestamp) { - SafeAdd(ref _connectionTime, _closeTimestamp - _openTimestamp); + _connectionTime = _closeTimestamp - _openTimestamp; } else {