Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix SqlUtils.WithRetry() to open a new connection if the last attempt closed the connection due to network issues. #221

Merged
merged 3 commits into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 async Task<T> ExecuteSprocAndTraceAsync<T>(
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
Loading