diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlDataReader.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlDataReader.cs index 8cb6e76f97..0bfeb79a98 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlDataReader.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlDataReader.cs @@ -2966,7 +2966,7 @@ override public int GetValues(object[] values) if ((sequentialAccess) && (i < maximumColumn)) { _data[fieldIndex].Clear(); - if (fieldIndex > i && fieldIndex>0) + if (fieldIndex > i && fieldIndex > 0) { // if we jumped an index forward because of a hidden column see if the buffer before the // current one was populated by the seek forward and clear it if it was @@ -4250,34 +4250,34 @@ public override Task NextResultAsync(CancellationToken cancellationToken) source.SetException(ADP.ExceptionWithStackTrace(ADP.DataReaderClosed())); return source.Task; } - - IDisposable registration = null; - if (cancellationToken.CanBeCanceled) + using (IDisposable registration = cancellationToken.CanBeCanceled ? cancellationToken.Register(SqlCommand.s_cancelIgnoreFailure, _command) : (IDisposable)null) { - if (cancellationToken.IsCancellationRequested) + if (null != registration) { - source.SetCanceled(); + if (cancellationToken.IsCancellationRequested) + { + source.SetCanceled(); + return source.Task; + } + } + + Task original = Interlocked.CompareExchange(ref _currentTask, source.Task, null); + if (original != null) + { + source.SetException(ADP.ExceptionWithStackTrace(SQL.PendingBeginXXXExists())); return source.Task; } - registration = cancellationToken.Register(SqlCommand.s_cancelIgnoreFailure, _command); - } - Task original = Interlocked.CompareExchange(ref _currentTask, source.Task, null); - if (original != null) - { - source.SetException(ADP.ExceptionWithStackTrace(SQL.PendingBeginXXXExists())); - return source.Task; - } + // Check if cancellation due to close is requested (this needs to be done after setting _currentTask) + if (_cancelAsyncOnCloseToken.IsCancellationRequested) + { + source.SetCanceled(); + _currentTask = null; + return source.Task; + } - // Check if cancellation due to close is requested (this needs to be done after setting _currentTask) - if (_cancelAsyncOnCloseToken.IsCancellationRequested) - { - source.SetCanceled(); - _currentTask = null; - return source.Task; + return InvokeAsyncCall(new HasNextResultAsyncCallContext(this, source, registration)); } - - return InvokeAsyncCall(new HasNextResultAsyncCallContext(this, source, registration)); } finally { @@ -4515,7 +4515,7 @@ out bytesRead if (!isContinuation) { // This is the first async operation which is happening - setup the _currentTask and timeout - Debug.Assert(context._source==null, "context._source should not be non-null when trying to change to async"); + Debug.Assert(context._source == null, "context._source should not be non-null when trying to change to async"); source = new TaskCompletionSource(); Task original = Interlocked.CompareExchange(ref _currentTask, source.Task, null); if (original != null) @@ -4681,31 +4681,28 @@ public override Task ReadAsync(CancellationToken cancellationToken) return source.Task; } - IDisposable registration = null; - if (cancellationToken.CanBeCanceled) - { - registration = cancellationToken.Register(SqlCommand.s_cancelIgnoreFailure, _command); - } - - ReadAsyncCallContext context = null; - if (_connection?.InnerConnection is SqlInternalConnection sqlInternalConnection) - { - context = Interlocked.Exchange(ref sqlInternalConnection.CachedDataReaderReadAsyncContext, null); - } - if (context is null) + using (IDisposable registration = cancellationToken.CanBeCanceled ? cancellationToken.Register(SqlCommand.s_cancelIgnoreFailure, _command) : (IDisposable)null) { - context = new ReadAsyncCallContext(); - } + ReadAsyncCallContext context = null; + if (_connection?.InnerConnection is SqlInternalConnection sqlInternalConnection) + { + context = Interlocked.Exchange(ref sqlInternalConnection.CachedDataReaderReadAsyncContext, null); + } + if (context is null) + { + context = new ReadAsyncCallContext(); + } - Debug.Assert(context._reader == null && context._source == null && context._disposable == null, "cached ReadAsyncCallContext was not properly disposed"); + Debug.Assert(context._reader == null && context._source == null && context._disposable == null, "cached ReadAsyncCallContext was not properly disposed"); - context.Set(this, source, registration); - context._hasMoreData = more; - context._hasReadRowToken = rowTokenRead; + context.Set(this, source, registration); + context._hasMoreData = more; + context._hasReadRowToken = rowTokenRead; - PrepareAsyncInvocation(useSnapshot: true); + PrepareAsyncInvocation(useSnapshot: true); - return InvokeAsyncCall(context); + return InvokeAsyncCall(context); + } } finally { @@ -4859,31 +4856,29 @@ override public Task IsDBNullAsync(int i, CancellationToken cancellationTo } // Setup cancellations - IDisposable registration = null; - if (cancellationToken.CanBeCanceled) + using (IDisposable registration = cancellationToken.CanBeCanceled ? cancellationToken.Register(SqlCommand.s_cancelIgnoreFailure, _command) : (IDisposable)null) { - registration = cancellationToken.Register(SqlCommand.s_cancelIgnoreFailure, _command); - } - IsDBNullAsyncCallContext context = null; - if (_connection?.InnerConnection is SqlInternalConnection sqlInternalConnection) - { - context = Interlocked.Exchange(ref sqlInternalConnection.CachedDataReaderIsDBNullContext, null); - } - if (context is null) - { - context = new IsDBNullAsyncCallContext(); - } + IsDBNullAsyncCallContext context = null; + if (_connection?.InnerConnection is SqlInternalConnection sqlInternalConnection) + { + context = Interlocked.Exchange(ref sqlInternalConnection.CachedDataReaderIsDBNullContext, null); + } + if (context is null) + { + context = new IsDBNullAsyncCallContext(); + } - Debug.Assert(context._reader == null && context._source == null && context._disposable == null, "cached ISDBNullAsync context not properly disposed"); + Debug.Assert(context._reader == null && context._source == null && context._disposable == null, "cached ISDBNullAsync context not properly disposed"); - context.Set(this, source, registration); - context._columnIndex = i; + context.Set(this, source, registration); + context._columnIndex = i; - // Setup async - PrepareAsyncInvocation(useSnapshot: true); + // Setup async + PrepareAsyncInvocation(useSnapshot: true); - return InvokeAsyncCall(context); + return InvokeAsyncCall(context); + } } } @@ -5006,16 +5001,13 @@ override public Task GetFieldValueAsync(int i, CancellationToken cancellat } // Setup cancellations - IDisposable registration = null; - if (cancellationToken.CanBeCanceled) + using (IDisposable registration = cancellationToken.CanBeCanceled ? cancellationToken.Register(s => ((SqlCommand)s).CancelIgnoreFailure(), _command) : (IDisposable)null) { - registration = cancellationToken.Register(s => ((SqlCommand)s).CancelIgnoreFailure(), _command); - } - - // Setup async - PrepareAsyncInvocation(useSnapshot: true); + // Setup async + PrepareAsyncInvocation(useSnapshot: true); - return InvokeAsyncCall(new GetFieldValueAsyncCallContext(this, source, registration, i)); + return InvokeAsyncCall(new GetFieldValueAsyncCallContext(this, source, registration, i)); + } } private static Task GetFieldValueAsyncExecute(Task task, object state) @@ -5112,8 +5104,8 @@ internal void Clear() _source = null; _reader = null; IDisposable copyDisposable = _disposable; - _disposable = null; copyDisposable?.Dispose(); + _disposable = null; } internal abstract Func> Execute { get; } diff --git a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlDataReader.cs b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlDataReader.cs index 176b3ddf3c..69a52e7a23 100644 --- a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlDataReader.cs +++ b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlDataReader.cs @@ -4793,55 +4793,56 @@ public override Task NextResultAsync(CancellationToken cancellationToken) return source.Task; } - IDisposable registration = null; - if (cancellationToken.CanBeCanceled) + using (IDisposable registration = cancellationToken.CanBeCanceled ? cancellationToken.Register(_command.CancelIgnoreFailure) : (IDisposable)null) { - if (cancellationToken.IsCancellationRequested) + if (null != registration) { - source.SetCanceled(); - return source.Task; + if (cancellationToken.IsCancellationRequested) + { + source.SetCanceled(); + return source.Task; + } } - registration = cancellationToken.Register(_command.CancelIgnoreFailure); - } - Task original = Interlocked.CompareExchange(ref _currentTask, source.Task, null); - if (original != null) - { - source.SetException(ADP.ExceptionWithStackTrace(SQL.PendingBeginXXXExists())); - return source.Task; - } + Task original = Interlocked.CompareExchange(ref _currentTask, source.Task, null); + if (original != null) + { + source.SetException(ADP.ExceptionWithStackTrace(SQL.PendingBeginXXXExists())); + return source.Task; + } - // Check if cancellation due to close is requested (this needs to be done after setting _currentTask) - if (_cancelAsyncOnCloseToken.IsCancellationRequested) - { - source.SetCanceled(); - _currentTask = null; - return source.Task; - } + // Check if cancellation due to close is requested (this needs to be done after setting _currentTask) + if (_cancelAsyncOnCloseToken.IsCancellationRequested) + { + source.SetCanceled(); + _currentTask = null; + return source.Task; + } - PrepareAsyncInvocation(useSnapshot: true); + PrepareAsyncInvocation(useSnapshot: true); - Func> moreFunc = null; + Func> moreFunc = null; - moreFunc = (t) => - { - if (t != null) + moreFunc = (t) => { - SqlClientEventSource.Log.TryTraceEvent(" attempt retry {0}", ObjectID); - PrepareForAsyncContinuation(); - } + if (t != null) + { + SqlClientEventSource.Log.TryTraceEvent(" attempt retry {0}", ObjectID); + PrepareForAsyncContinuation(); + } - bool more; - if (TryNextResult(out more)) - { - // completed - return more ? ADP.TrueTask : ADP.FalseTask; - } + bool more; + if (TryNextResult(out more)) + { + // completed + return more ? ADP.TrueTask : ADP.FalseTask; + } - return ContinueRetryable(moreFunc); - }; + return ContinueRetryable(moreFunc); + }; - return InvokeRetryable(moreFunc, source, registration); + return InvokeRetryable(moreFunc, source, registration); + } } finally { @@ -5187,55 +5188,52 @@ public override Task ReadAsync(CancellationToken cancellationToken) return source.Task; } - IDisposable registration = null; - if (cancellationToken.CanBeCanceled) - { - registration = cancellationToken.Register(_command.CancelIgnoreFailure); - } - - PrepareAsyncInvocation(useSnapshot: true); - - Func> moreFunc = null; - moreFunc = (t) => + using (IDisposable registration = cancellationToken.CanBeCanceled ? cancellationToken.Register(_command.CancelIgnoreFailure) : (IDisposable)null) { - if (t != null) - { - SqlClientEventSource.Log.TryTraceEvent(" attempt retry {0}", ObjectID); - PrepareForAsyncContinuation(); - } + PrepareAsyncInvocation(useSnapshot: true); - if (rowTokenRead || TryReadInternal(true, out more)) + Func> moreFunc = null; + moreFunc = (t) => { - - // If there are no more rows, or this is Sequential Access, then we are done - if (!more || (_commandBehavior & CommandBehavior.SequentialAccess) == CommandBehavior.SequentialAccess) + if (t != null) { - // completed - return more ? ADP.TrueTask : ADP.FalseTask; + SqlClientEventSource.Log.TryTraceEvent(" attempt retry {0}", ObjectID); + PrepareForAsyncContinuation(); } - else + + if (rowTokenRead || TryReadInternal(true, out more)) { - // First time reading the row token - update the snapshot - if (!rowTokenRead) - { - rowTokenRead = true; - _snapshot = null; - PrepareAsyncInvocation(useSnapshot: true); - } - // if non-sequentialaccess then read entire row before returning - if (TryReadColumn(_metaData.Length - 1, true)) + // If there are no more rows, or this is Sequential Access, then we are done + if (!more || (_commandBehavior & CommandBehavior.SequentialAccess) == CommandBehavior.SequentialAccess) { // completed - return ADP.TrueTask; + return more ? ADP.TrueTask : ADP.FalseTask; + } + else + { + // First time reading the row token - update the snapshot + if (!rowTokenRead) + { + rowTokenRead = true; + _snapshot = null; + PrepareAsyncInvocation(useSnapshot: true); + } + + // if non-sequentialaccess then read entire row before returning + if (TryReadColumn(_metaData.Length - 1, true)) + { + // completed + return ADP.TrueTask; + } } } - } - return ContinueRetryable(moreFunc); - }; + return ContinueRetryable(moreFunc); + }; - return InvokeRetryable(moreFunc, source, registration); + return InvokeRetryable(moreFunc, source, registration); + } } finally { @@ -5336,36 +5334,33 @@ override public Task IsDBNullAsync(int i, CancellationToken cancellationTo } // Setup cancellations - IDisposable registration = null; - if (cancellationToken.CanBeCanceled) + using (IDisposable registration = cancellationToken.CanBeCanceled ? cancellationToken.Register(_command.CancelIgnoreFailure) : (IDisposable)null) { - registration = cancellationToken.Register(_command.CancelIgnoreFailure); - } - - // Setup async - PrepareAsyncInvocation(useSnapshot: true); + // Setup async + PrepareAsyncInvocation(useSnapshot: true); - // Setup the retryable function - Func> moreFunc = null; - moreFunc = (t) => - { - if (t != null) + // Setup the retryable function + Func> moreFunc = null; + moreFunc = (t) => { - PrepareForAsyncContinuation(); - } + if (t != null) + { + PrepareForAsyncContinuation(); + } - if (TryReadColumnHeader(i)) - { - return _data[i].IsNull ? ADP.TrueTask : ADP.FalseTask; - } - else - { - return ContinueRetryable(moreFunc); - } - }; + if (TryReadColumnHeader(i)) + { + return _data[i].IsNull ? ADP.TrueTask : ADP.FalseTask; + } + else + { + return ContinueRetryable(moreFunc); + } + }; - // Go! - return InvokeRetryable(moreFunc, source, registration); + // Go! + return InvokeRetryable(moreFunc, source, registration); + } } } @@ -5461,36 +5456,33 @@ override public Task GetFieldValueAsync(int i, CancellationToken cancellat } // Setup cancellations - IDisposable registration = null; - if (cancellationToken.CanBeCanceled) + using (IDisposable registration = cancellationToken.CanBeCanceled ? cancellationToken.Register(_command.CancelIgnoreFailure) : (IDisposable)null) { - registration = cancellationToken.Register(_command.CancelIgnoreFailure); - } - - // Setup async - PrepareAsyncInvocation(useSnapshot: true); + // Setup async + PrepareAsyncInvocation(useSnapshot: true); - // Setup the retryable function - Func> moreFunc = null; - moreFunc = (t) => - { - if (t != null) + // Setup the retryable function + Func> moreFunc = null; + moreFunc = (t) => { - PrepareForAsyncContinuation(); - } + if (t != null) + { + PrepareForAsyncContinuation(); + } - if (TryReadColumn(i, setTimeout: false)) - { - return Task.FromResult(GetFieldValueFromSqlBufferInternal(_data[i], _metaData[i])); - } - else - { - return ContinueRetryable(moreFunc); - } - }; + if (TryReadColumn(i, setTimeout: false)) + { + return Task.FromResult(GetFieldValueFromSqlBufferInternal(_data[i], _metaData[i])); + } + else + { + return ContinueRetryable(moreFunc); + } + }; - // Go! - return InvokeRetryable(moreFunc, source, registration); + // Go! + return InvokeRetryable(moreFunc, source, registration); + } } #if DEBUG