From 18f1b8da83da13bf7f15bfd47c8897d629957ef9 Mon Sep 17 00:00:00 2001 From: Jinan R Date: Tue, 27 Feb 2024 11:41:49 +0700 Subject: [PATCH 1/2] Fixed CancellationTokenSource Issue --- Agoda.Frameworks.DB.Tests/DbRepositoryTest.cs | 22 ------------------- Agoda.Frameworks.DB/DbRepository.cs | 6 +++-- Agoda.Frameworks.DB/IDbRepository.cs | 2 +- 3 files changed, 5 insertions(+), 25 deletions(-) diff --git a/Agoda.Frameworks.DB.Tests/DbRepositoryTest.cs b/Agoda.Frameworks.DB.Tests/DbRepositoryTest.cs index 2ba7f52..03ed623 100644 --- a/Agoda.Frameworks.DB.Tests/DbRepositoryTest.cs +++ b/Agoda.Frameworks.DB.Tests/DbRepositoryTest.cs @@ -666,28 +666,6 @@ public void QueryMultipleAsync_Retry_Failure() Assert.IsNotNull(_onQueryCompleteEvents[0].Error); Assert.IsNotNull(_onQueryCompleteEvents[1].Error); } - - [Test] - public async Task ExecuteReaderAsync_CancellationToken_Cancelled() - { - var cancellationToken = new CancellationTokenSource(TimeSpan.FromDays(1)); - cancellationToken.Cancel(); - var maxAttemptCount = 2; - Assert.ThrowsAsync(async () => await _db.ExecuteReaderAsync("mobile_ro", "db.v1.sp_foo", 1, - maxAttemptCount, - cancellationToken.Token, - new IDbDataParameter[] - { - new SqlParameter("@param1", "value1"), - new SqlParameter("@param2", "value2") - - }, reader => { return Task.FromResult("");}) - ); - - _dbResources.Verify(x => x.ChooseDb("mobile_ro").UpdateWeight(It.IsAny(), false), Times.Exactly(maxAttemptCount)); - } - - protected class FakeStoredProc : IStoredProc { public string DbName => "mobile_ro"; diff --git a/Agoda.Frameworks.DB/DbRepository.cs b/Agoda.Frameworks.DB/DbRepository.cs index 487b5fa..b8e2109 100644 --- a/Agoda.Frameworks.DB/DbRepository.cs +++ b/Agoda.Frameworks.DB/DbRepository.cs @@ -303,7 +303,7 @@ public Task ExecuteReaderAsync( string storedProc, int timeoutSecs, int maxAttemptCount, - CancellationToken token, + int taskCancellationTimeOutInMilliSecs, IDbDataParameter[] parameters, Func> callback) { @@ -311,6 +311,8 @@ public Task ExecuteReaderAsync( { var stopwatch = Stopwatch.StartNew(); Exception error = null; + var cancellationTokenSource = new CancellationTokenSource(); + cancellationTokenSource.CancelAfter(taskCancellationTimeOutInMilliSecs); try { using (var connection = _generateConnection(connectionStr)) @@ -332,7 +334,7 @@ public Task ExecuteReaderAsync( CommandTimeout = timeoutSecs }; sqlCommand.Parameters.AddRange(parameters); - using (var reader = await sqlCommand.ExecuteReaderAsync(token)) + using (var reader = await sqlCommand.ExecuteReaderAsync(cancellationTokenSource.Token)) { return await callback(reader); } diff --git a/Agoda.Frameworks.DB/IDbRepository.cs b/Agoda.Frameworks.DB/IDbRepository.cs index 9729254..1d84d8a 100644 --- a/Agoda.Frameworks.DB/IDbRepository.cs +++ b/Agoda.Frameworks.DB/IDbRepository.cs @@ -141,7 +141,7 @@ Task ExecuteReaderAsync( string storedProc, int timeoutSecs, int maxAttemptCount, - CancellationToken token, + int taskCancellationTimeOutInMilliSecs, IDbDataParameter[] parameters, Func> callback); From 4e9dc6f8d3676634329bc0f5ed19089dac9a16b3 Mon Sep 17 00:00:00 2001 From: Jinan R Date: Tue, 5 Mar 2024 08:45:13 +0700 Subject: [PATCH 2/2] Fixed review comments --- Agoda.Frameworks.DB/DbRepository.cs | 79 +++++++++++++++-------------- 1 file changed, 41 insertions(+), 38 deletions(-) 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); }