diff --git a/Agoda.Frameworks.DB/DbRepository.cs b/Agoda.Frameworks.DB/DbRepository.cs index b8e2109..699dd4e 100644 --- a/Agoda.Frameworks.DB/DbRepository.cs +++ b/Agoda.Frameworks.DB/DbRepository.cs @@ -311,55 +311,58 @@ public Task ExecuteReaderAsync( { var stopwatch = Stopwatch.StartNew(); Exception error = null; - var cancellationTokenSource = new CancellationTokenSource(); - cancellationTokenSource.CancelAfter(taskCancellationTimeOutInMilliSecs); - try + using (var cancellationTokenSource = new CancellationTokenSource()) { - using (var connection = _generateConnection(connectionStr)) + cancellationTokenSource.CancelAfter(taskCancellationTimeOutInMilliSecs); + try { - if (connection is SqlConnection sqlConn) + using (var connection = _generateConnection(connectionStr)) { - await sqlConn.OpenAsync(); - } - else - { - connection.Open(); - } - SqlCommand sqlCommand = null; - try - { - sqlCommand = new SqlCommand(storedProc, connection as SqlConnection) + if (connection is SqlConnection sqlConn) { - CommandType = CommandType.StoredProcedure, - CommandTimeout = timeoutSecs - }; - sqlCommand.Parameters.AddRange(parameters); - using (var reader = await sqlCommand.ExecuteReaderAsync(cancellationTokenSource.Token)) + await sqlConn.OpenAsync(cancellationTokenSource.Token); + } + else { - return await callback(reader); + connection.Open(); } - } - finally - { - if (sqlCommand != null) + SqlCommand sqlCommand = null; + try { - sqlCommand.Parameters.Clear(); - sqlCommand.Dispose(); + sqlCommand = new SqlCommand(storedProc, connection as SqlConnection) + { + CommandType = CommandType.StoredProcedure, + CommandTimeout = timeoutSecs + }; + sqlCommand.Parameters.AddRange(parameters); + using (var reader = await sqlCommand.ExecuteReaderAsync(cancellationTokenSource.Token)) + { + return await callback(reader); + } + } + finally + { + if (sqlCommand != null) + { + sqlCommand.Parameters.Clear(); + sqlCommand.Dispose(); + } } } } + catch (Exception e) + { + error = e; + throw; + } + finally + { + stopwatch.Stop(); + RaiseOnExecuteReaderComplete( + database, storedProc, stopwatch.ElapsedMilliseconds, error); + } } - catch (Exception e) - { - error = e; - throw; - } - finally - { - stopwatch.Stop(); - RaiseOnExecuteReaderComplete( - database, storedProc, stopwatch.ElapsedMilliseconds, error); - } + }, ShouldRetry(maxAttemptCount), RaiseOnError); }