Skip to content

Commit

Permalink
Adding Polly retry on sql connection Open
Browse files Browse the repository at this point in the history
  • Loading branch information
mmckechney committed Jun 23, 2022
1 parent 6195331 commit 4ef6aca
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
5 changes: 3 additions & 2 deletions src/SqlBuildManager.Console.ExternalTest/ContainerAppTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,8 @@ public void ContainerApp_EnvOnly_Queue_SBMSource_Success(string settingsFile, st
"--unittest", "true",
"--monitor", "true",
"--env", "true",
"--stream", "true"//,
//"--deletewhendone", "true"
"--stream", "true",
"--deletewhendone", "true"
};
val = rootCommand.InvokeAsync(args);
val.Wait();
Expand Down Expand Up @@ -488,6 +488,7 @@ public void ContainerApp_Queue_DacpacSource_ForceApplyCustom_Success(string sett

//Create another table in the first that will be applied when the custom DACPAC is created
DatabaseHelper.CreateRandomTable(cmdLine, firstOverride);
DatabaseHelper.CreateRandomTable(cmdLine, thirdOverride);

//enqueue the topic messages
args = new string[]{
Expand Down
7 changes: 6 additions & 1 deletion src/SqlSync.SqlBuild/SqlBuildHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using System.Linq;
using SqlBuildManager.Interfaces.Console;
using Microsoft.Extensions.Logging;
using Polly;
namespace SqlSync.SqlBuild
{
/// <summary>
Expand Down Expand Up @@ -1884,7 +1885,11 @@ internal BuildConnectData GetConnectionDataClass(string serverName, string datab
{
BuildConnectData cData = new BuildConnectData();
cData.Connection = SqlSync.Connection.ConnectionHelper.GetConnection(databaseName, serverName, this.connData.UserId, this.connData.Password, connData.AuthenticationType, this.connData.ScriptTimeout);
cData.Connection.Open();

//Add a robust retry policy on database connection opening
var pollyConnection = Policy.Handle<SqlException>().WaitAndRetry(3, retryAttempt => TimeSpan.FromSeconds(Math.Pow(1.3, retryAttempt)));
pollyConnection.Execute(() => cData.Connection.Open());

cData.HasLoggingTable = LogTableExists(cData.Connection);
cData.Connection.InfoMessage +=new SqlInfoMessageEventHandler(Connection_InfoMessage);
if(!databaseName.Equals(this.logToDatabaseName) && this.isTransactional) //we don't want a transaction for the logging database
Expand Down

0 comments on commit 4ef6aca

Please sign in to comment.