From 5dbe6232ebc7ce38134fa5f3c485404d9e9bbb54 Mon Sep 17 00:00:00 2001 From: Rama Bhandaru Date: Mon, 3 Jun 2024 18:18:58 -0400 Subject: [PATCH] Fix SqlUtils.WithRetry() to open a new connection if the last attempt closed the connection due to network issues. (#221) Co-authored-by: RAMAKANT BHANDARU --- CHANGELOG.md | 8 +++++++- src/DurableTask.SqlServer/SqlUtils.cs | 13 +++++++++---- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 182e88c..060bd91 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,12 @@ # Changelog -## v1.3.0 (Unreleased) +## v1.3.1 (Unreleased) + +### Updates + +* Fix SQL retry logic to open a new connection if a previous failure closed the connection ([#221](https://github.com/microsoft/durabletask-mssql/pull/221)) - contributed by [@microrama](https://github.com/microrama) + +## v1.3.0 ### Updates diff --git a/src/DurableTask.SqlServer/SqlUtils.cs b/src/DurableTask.SqlServer/SqlUtils.cs index a06ed2b..662b123 100644 --- a/src/DurableTask.SqlServer/SqlUtils.cs +++ b/src/DurableTask.SqlServer/SqlUtils.cs @@ -29,7 +29,7 @@ static class SqlUtils { return reader.IsDBNull(columnIndex) ? null : reader.GetString(columnIndex); } - + public static TaskMessage GetTaskMessage(this DbDataReader reader) { return new TaskMessage @@ -560,7 +560,7 @@ static async Task ExecuteSprocAndTraceAsync( var context = new SprocExecutionContext(); try { - return await WithRetry(() => executor(command), context, traceHelper, instanceId); + return await WithRetry(command, executor, context, traceHelper, instanceId); } finally { @@ -610,7 +610,7 @@ public static DateTime ToSqlUtcDateTime(this DateTime dateTime, DateTime default } } - static async Task WithRetry(Func> func, SprocExecutionContext context, LogHelper traceHelper, string? instanceId, int maxRetries = 5) + static async Task WithRetry(DbCommand command, Func> executor, SprocExecutionContext context, LogHelper traceHelper, string? instanceId, int maxRetries = 5) { context.RetryCount = 0; @@ -618,7 +618,12 @@ static async Task WithRetry(Func> func, SprocExecutionContext cont { try { - return await func(); + // Open connection if network blip caused it to close on a previous attempt + if (command.Connection.State != ConnectionState.Open) + { + await command.Connection.OpenAsync(); + } + return await executor(command); } catch (Exception e) {