From 778068ffa29b5dc794c8ef6be78baf5c4fefa155 Mon Sep 17 00:00:00 2001 From: Cheena Malhotra Date: Fri, 15 Nov 2019 11:33:56 -0800 Subject: [PATCH] [release/3.0] Fix driver behavior of sending Attention signal for successful Bulk Copy operation --- .../src/System/Data/SqlClient/SqlBulkCopy.cs | 18 +++++++++--------- .../SQL/SqlBulkCopyTest/CopyAllFromReader.cs | 2 +- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/System.Data.SqlClient/src/System/Data/SqlClient/SqlBulkCopy.cs b/src/System.Data.SqlClient/src/System/Data/SqlClient/SqlBulkCopy.cs index ff278b9ce747..ac253e827003 100644 --- a/src/System.Data.SqlClient/src/System/Data/SqlClient/SqlBulkCopy.cs +++ b/src/System.Data.SqlClient/src/System/Data/SqlClient/SqlBulkCopy.cs @@ -2561,7 +2561,7 @@ private void CopyBatchesAsyncContinuedOnError(bool cleanupParser) if (_stateObj != null) { - CleanUpStateObjectOnError(); + CleanUpStateObject(); } } catch (OutOfMemoryException) @@ -2584,7 +2584,7 @@ private void CopyBatchesAsyncContinuedOnError(bool cleanupParser) } // Cleans the stateobj. Used in a number of places, specially in exceptions. - private void CleanUpStateObjectOnError() + private void CleanUpStateObject(bool isCancelRequested = true) { if (_stateObj != null) { @@ -2594,7 +2594,7 @@ private void CleanUpStateObjectOnError() _stateObj.ResetBuffer(); _stateObj.ResetPacketCounters(); // If _parser is closed, sending attention will raise debug assertion, so we avoid it (but not calling CancelRequest). - if (_parser.State == TdsParserState.OpenNotLoggedIn || _parser.State == TdsParserState.OpenLoggedIn) + if (isCancelRequested && (_parser.State == TdsParserState.OpenNotLoggedIn || _parser.State == TdsParserState.OpenLoggedIn)) { _stateObj.CancelRequest(); } @@ -2655,7 +2655,7 @@ private void WriteToServerInternalRestContinuedAsync(BulkCopySimpleResultSet int _localColumnMappings = null; try { - CleanUpStateObjectOnError(); + CleanUpStateObject(); } finally { @@ -2671,7 +2671,7 @@ private void WriteToServerInternalRestContinuedAsync(BulkCopySimpleResultSet int _localColumnMappings = null; try { - CleanUpStateObjectOnError(); + CleanUpStateObject(isCancelRequested: false); } finally { @@ -2698,11 +2698,11 @@ private void WriteToServerInternalRestContinuedAsync(BulkCopySimpleResultSet int try { - CleanUpStateObjectOnError(); + CleanUpStateObject(isCancelRequested: false); } catch (Exception cleanupEx) { - Debug.Fail("Unexpected exception during CleanUpstateObjectOnError (ignored)", cleanupEx.ToString()); + Debug.Fail($"Unexpected exception during {nameof(CleanUpStateObject)} (ignored)", cleanupEx.ToString()); } if (source != null) @@ -2717,11 +2717,11 @@ private void WriteToServerInternalRestContinuedAsync(BulkCopySimpleResultSet int try { - CleanUpStateObjectOnError(); + CleanUpStateObject(); } catch (Exception cleanupEx) { - Debug.Fail("Unexpected exception during CleanUpstateObjectOnError (ignored)", cleanupEx.ToString()); + Debug.Fail($"Unexpected exception during {nameof(CleanUpStateObject)} (ignored)", cleanupEx.ToString()); } if (source != null) diff --git a/src/System.Data.SqlClient/tests/ManualTests/SQL/SqlBulkCopyTest/CopyAllFromReader.cs b/src/System.Data.SqlClient/tests/ManualTests/SQL/SqlBulkCopyTest/CopyAllFromReader.cs index e4457430eeca..3a221f9f7cb9 100644 --- a/src/System.Data.SqlClient/tests/ManualTests/SQL/SqlBulkCopyTest/CopyAllFromReader.cs +++ b/src/System.Data.SqlClient/tests/ManualTests/SQL/SqlBulkCopyTest/CopyAllFromReader.cs @@ -62,7 +62,7 @@ public static void Test(string srcConstr, string dstConstr, string dstTable) DataTestUtility.AssertEqualsWithDescription((long)0, (long)stats["CursorOpens"], "Non-zero CursorOpens value: " + (long)stats["CursorOpens"]); DataTestUtility.AssertEqualsWithDescription((long)0, (long)stats["IduRows"], "Non-zero IduRows value: " + (long)stats["IduRows"]); - DataTestUtility.AssertEqualsWithDescription((long)4, stats["BuffersReceived"], "Unexpected BuffersReceived value."); + DataTestUtility.AssertEqualsWithDescription((long)3, stats["BuffersReceived"], "Unexpected BuffersReceived value."); DataTestUtility.AssertEqualsWithDescription((long)3, stats["BuffersSent"], "Unexpected BuffersSent value."); DataTestUtility.AssertEqualsWithDescription((long)0, stats["IduCount"], "Unexpected IduCount value."); DataTestUtility.AssertEqualsWithDescription((long)3, stats["SelectCount"], "Unexpected SelectCount value.");