Skip to content

Commit

Permalink
Fix SqlUtils.WithRetry() to open a new connection if the last attempt…
Browse files Browse the repository at this point in the history
… closed the connection due to network issues. (#221)

Co-authored-by: RAMAKANT BHANDARU <rbhandaru@mtb.com>
  • Loading branch information
microrama and RAMAKANT BHANDARU committed Jun 3, 2024
1 parent 4725c66 commit 5dbe623
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
13 changes: 9 additions & 4 deletions src/DurableTask.SqlServer/SqlUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -560,7 +560,7 @@ static IEnumerable<SqlDataRecord> GetInstanceIdRecords(IEnumerable<string> insta
var context = new SprocExecutionContext();
try
{
return await WithRetry(() => executor(command), context, traceHelper, instanceId);
return await WithRetry(command, executor, context, traceHelper, instanceId);
}
finally
{
Expand Down Expand Up @@ -610,15 +610,20 @@ public static DateTime ToSqlUtcDateTime(this DateTime dateTime, DateTime default
}
}

static async Task<T> WithRetry<T>(Func<Task<T>> func, SprocExecutionContext context, LogHelper traceHelper, string? instanceId, int maxRetries = 5)
static async Task<T> WithRetry<T>(DbCommand command, Func<DbCommand, Task<T>> executor, SprocExecutionContext context, LogHelper traceHelper, string? instanceId, int maxRetries = 5)
{
context.RetryCount = 0;

while (true)
{
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)
{
Expand Down

0 comments on commit 5dbe623

Please sign in to comment.