Skip to content

Commit

Permalink
Remove unnecessary try-catch when executing DbReader
Browse files Browse the repository at this point in the history
  • Loading branch information
APErebus committed Jun 10, 2021
1 parent 24e8275 commit dc6bf6f
Showing 1 changed file with 33 additions and 47 deletions.
80 changes: 33 additions & 47 deletions source/Nevermore/Transient/DbCommandExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,59 +48,45 @@ public static Task<int> ExecuteNonQueryWithRetryAsync(this DbCommand command, Re

public static DbDataReader ExecuteReaderWithRetry(this DbCommand command, RetryPolicy commandRetryPolicy, CommandBehavior behavior = CommandBehavior.Default, RetryPolicy connectionRetryPolicy = null, string operationName = "ExecuteReader")
{
try
GuardConnectionIsNotNull(command);
var effectiveCommandRetryPolicy = (commandRetryPolicy ?? RetryPolicy.NoRetry).LoggingRetries(operationName);
return effectiveCommandRetryPolicy.ExecuteAction(() =>
{
GuardConnectionIsNotNull(command);
var effectiveCommandRetryPolicy = (commandRetryPolicy ?? RetryPolicy.NoRetry).LoggingRetries(operationName);
return effectiveCommandRetryPolicy.ExecuteAction(() =>
var weOwnTheConnectionLifetime = EnsureValidConnection(command, connectionRetryPolicy);
try
{
var weOwnTheConnectionLifetime = EnsureValidConnection(command, connectionRetryPolicy);
try
{
return command.ExecuteReader(behavior);
}
catch (Exception)
{
if (weOwnTheConnectionLifetime && command.Connection != null &&
command.Connection.State == ConnectionState.Open)
command.Connection.Close();
throw;
}
});
}
catch (Exception ex)
{
throw new Exception($"Exception occurred while executing a reader for `{command.CommandText}`", ex);
}
return command.ExecuteReader(behavior);
}
catch (Exception)
{
if (weOwnTheConnectionLifetime && command.Connection != null &&
command.Connection.State == ConnectionState.Open)
command.Connection.Close();
throw;
}
});
}

public static async Task<DbDataReader> ExecuteReaderWithRetryAsync(this DbCommand command, RetryPolicy commandRetryPolicy, CommandBehavior commandBehavior, RetryPolicy connectionRetryPolicy = null, string operationName = "ExecuteReader", CancellationToken cancellationToken = default)
{
try
GuardConnectionIsNotNull(command);
var effectiveCommandRetryPolicy =
(commandRetryPolicy ?? RetryPolicy.NoRetry).LoggingRetries(operationName);
return await effectiveCommandRetryPolicy.ExecuteActionAsync(async () =>
{
GuardConnectionIsNotNull(command);
var effectiveCommandRetryPolicy =
(commandRetryPolicy ?? RetryPolicy.NoRetry).LoggingRetries(operationName);
return await effectiveCommandRetryPolicy.ExecuteActionAsync(async () =>
var weOwnTheConnectionLifetime = EnsureValidConnection(command, connectionRetryPolicy);
try
{
var weOwnTheConnectionLifetime = EnsureValidConnection(command, connectionRetryPolicy);
try
{
return await command.ExecuteReaderAsync(commandBehavior, cancellationToken);
}
catch (Exception)
{
if (weOwnTheConnectionLifetime && command.Connection != null &&
command.Connection.State == ConnectionState.Open)
await command.Connection.CloseAsync();
throw;
}
});
}
catch (Exception ex)
{
throw new Exception($"Exception occurred while executing a reader for `{command.CommandText}`", ex);
}
return await command.ExecuteReaderAsync(commandBehavior, cancellationToken);
}
catch (Exception)
{
if (weOwnTheConnectionLifetime && command.Connection != null &&
command.Connection.State == ConnectionState.Open)
await command.Connection.CloseAsync();
throw;
}
});
}

public static object ExecuteScalarWithRetry(this DbCommand command, RetryPolicy commandRetryPolicy, RetryPolicy connectionRetryPolicy = null, string operationName = "ExecuteScalar")
Expand All @@ -121,7 +107,7 @@ public static object ExecuteScalarWithRetry(this DbCommand command, RetryPolicy
}
});
}

public static async Task<object> ExecuteScalarWithRetryAsync(this DbCommand command, RetryPolicy commandRetryPolicy, RetryPolicy connectionRetryPolicy = null, string operationName = "ExecuteScalar", CancellationToken cancellationToken = default)
{
GuardConnectionIsNotNull(command);
Expand Down

0 comments on commit dc6bf6f

Please sign in to comment.