diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParserStateObject.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParserStateObject.cs index 931bb53f7e..32b3e8a812 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParserStateObject.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParserStateObject.cs @@ -2514,23 +2514,36 @@ internal bool IsConnectionAlive(bool throwOnException) else { uint error; - SniContext = SniContext.Snix_Connect; - error = CheckConnection(); - if ((error != TdsEnums.SNI_SUCCESS) && (error != TdsEnums.SNI_WAIT_TIMEOUT)) + // Read an empty packet from the network so that we can tell if the network has been interupted + PacketHandle readPacket = EmptyReadPacket; + try { - // Connection is dead - isAlive = false; - if (throwOnException) + SniContext = SniContext.Snix_Connect; + + error = CheckConnection(); + if ((error != TdsEnums.SNI_SUCCESS) && (error != TdsEnums.SNI_WAIT_TIMEOUT)) { - // Get the error from SNI so that we can throw the correct exception - AddError(_parser.ProcessSNIError(this)); - ThrowExceptionAndWarning(); + // Connection is dead + isAlive = false; + if (throwOnException) + { + // Get the error from SNI so that we can throw the correct exception + AddError(_parser.ProcessSNIError(this)); + ThrowExceptionAndWarning(); + } + } + else + { + _lastSuccessfulIOTimer._value = DateTime.UtcNow.Ticks; } } - else + finally { - _lastSuccessfulIOTimer._value = DateTime.UtcNow.Ticks; + if (!IsPacketEmpty(readPacket)) + { + ReleasePacket(readPacket); + } } } }