Skip to content

Commit

Permalink
Merge pull request #183 from OctopusDeploy/core/pg/add-retry-begin-tr…
Browse files Browse the repository at this point in the history
…ansaction

Add retry for BeginTransaction
  • Loading branch information
paulegradie authored Mar 1, 2022
2 parents d608cc2 + 7971f63 commit de7c60e
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public IntegrationTestDatabase()
var username = Environment.GetEnvironmentVariable("NevermoreTestUsername");
var password = Environment.GetEnvironmentVariable("NevermoreTestPassword");
testDatabaseName = Environment.GetEnvironmentVariable("NevermoreBenchmarkDatabase") ?? "Nevermore-Benchmarks";
var builder = new SqlConnectionStringBuilder($"Server={sqlInstance};Database={testDatabaseName};{(username == null ? "Trusted_connection=true;" : string.Empty)}")
var builder = new SqlConnectionStringBuilder($"Server={sqlInstance};Database={testDatabaseName};{(username == null ? "Trusted_connection=true;" : string.Empty)}; Encrypt=False;")
{
ApplicationName = testDatabaseName,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public IntegrationTestDatabase()
var username = Environment.GetEnvironmentVariable("NevermoreTestUsername");
var password = Environment.GetEnvironmentVariable("NevermoreTestPassword");
testDatabaseName = Environment.GetEnvironmentVariable("NevermoreTestDatabase") ?? "Nevermore-IntegrationTests";
var builder = new SqlConnectionStringBuilder($"Server={sqlInstance};Database={testDatabaseName};{(username == null ? "Trusted_connection=true;" : string.Empty)}")
var builder = new SqlConnectionStringBuilder($"Server={sqlInstance};Database={testDatabaseName};{(username == null ? "Trusted_connection=true;" : string.Empty)}; Encrypt=False;")
{
ApplicationName = testDatabaseName,
};
Expand Down
4 changes: 2 additions & 2 deletions source/Nevermore/Advanced/ReadTransaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public async Task OpenAsync()
public void Open(IsolationLevel isolationLevel)
{
Open();
Transaction = connection!.BeginTransaction(isolationLevel, SqlServerTransactionName);
Transaction = connection!.BeginTransactionWithRetry(isolationLevel, SqlServerTransactionName);
}

public async Task OpenAsync(IsolationLevel isolationLevel)
Expand All @@ -89,7 +89,7 @@ public async Task OpenAsync(IsolationLevel isolationLevel)

// We use the synchronous overload here even though there is an async one, because the BeginTransactionAsync calls
// the synchronous version anyway, and the async overload doesn't accept a name parameter.
Transaction = connection!.BeginTransaction(isolationLevel, SqlServerTransactionName);
Transaction = connection!.BeginTransactionWithRetry(isolationLevel, SqlServerTransactionName);
}

[Pure]
Expand Down
2 changes: 1 addition & 1 deletion source/Nevermore/Nevermore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="System.Diagnostics.Contracts" Version="4.3.0" />
<PackageReference Include="System.ValueTuple" Version="4.4.0" />
<PackageReference Include="Microsoft.Data.SqlClient" Version="2.1.4" />
<PackageReference Include="Microsoft.Data.SqlClient" Version="4.1.0" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
</ItemGroup>

Expand Down
15 changes: 15 additions & 0 deletions source/Nevermore/Transient/RetryManagerSqlExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public static class RetryManagerSqlExtensions
{
public static readonly string DefaultStrategyCommandTechnologyName = "SQL";
public static readonly string DefaultStrategyConnectionTechnologyName = "SQLConnection";
public static readonly string DefaultStrategyTransactionTechnologyName = "SQLTransaction";

public static RetryPolicy GetDefaultSqlCommandRetryPolicy(this RetryManager retryManager)
{
Expand Down Expand Up @@ -41,5 +42,19 @@ public static RetryStrategy GetDefaultSqlConnectionRetryStrategy(this RetryManag
return retryManager.GetDefaultRetryStrategy(DefaultStrategyCommandTechnologyName);
}
}

public static RetryPolicy GetDefaultSqlTransactionRetryPolicy(this RetryManager retryManager)
{
if (retryManager == null) throw new ArgumentNullException("retryManager");

return new RetryPolicy(new SqlDatabaseTransientErrorDetectionStrategy(), retryManager.GetDefaultSqlTransactionRetryStrategy());
}

public static RetryStrategy GetDefaultSqlTransactionRetryStrategy(this RetryManager retryManager)
{
if (retryManager == null) throw new ArgumentNullException("retryManager");

return retryManager.GetDefaultRetryStrategy(DefaultStrategyTransactionTechnologyName);
}
}
}
20 changes: 20 additions & 0 deletions source/Nevermore/Transient/TransactionExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System.Data;
using System.Data.Common;
using Microsoft.Data.SqlClient;


namespace Nevermore.Transient
{
public static class TransactionExtensions
{
public static DbTransaction BeginTransactionWithRetry(this SqlConnection connection, IsolationLevel isolationLevel, string sqlServerTransactionName)
{
return BeginTransactionWithRetry(connection, isolationLevel, sqlServerTransactionName, RetryManager.Instance.GetDefaultSqlTransactionRetryPolicy());
}

public static DbTransaction BeginTransactionWithRetry(this SqlConnection connection, IsolationLevel isolationLevel, string sqlServerTransactionName, RetryPolicy retryPolicy)
{
return (retryPolicy ?? RetryPolicy.NoRetry).LoggingRetries("Beginning Database Transaction").ExecuteAction(() => connection.BeginTransaction(isolationLevel, sqlServerTransactionName));
}
}
}
4 changes: 1 addition & 3 deletions source/Nevermore/TransientFaultHandling.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Nevermore.Transient;

namespace Nevermore
Expand Down Expand Up @@ -36,6 +33,7 @@ public static void InitializeRetryManager()
{
{RetryManagerSqlExtensions.DefaultStrategyConnectionTechnologyName, DefaultExponentialStrategyName},
{RetryManagerSqlExtensions.DefaultStrategyCommandTechnologyName, DefaultExponentialStrategyName},
{RetryManagerSqlExtensions.DefaultStrategyTransactionTechnologyName, DefaultExponentialStrategyName}
}));
}
}}

0 comments on commit de7c60e

Please sign in to comment.