From 4ef6aca3fe42836b4188ecc4674dcc27f39a0129 Mon Sep 17 00:00:00 2001 From: Michael McKechney Date: Thu, 23 Jun 2022 16:13:22 -0400 Subject: [PATCH] Adding Polly retry on sql connection Open --- .../ContainerAppTests.cs | 5 +++-- src/SqlSync.SqlBuild/SqlBuildHelper.cs | 7 ++++++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/SqlBuildManager.Console.ExternalTest/ContainerAppTests.cs b/src/SqlBuildManager.Console.ExternalTest/ContainerAppTests.cs index a2523f9f..ba445560 100644 --- a/src/SqlBuildManager.Console.ExternalTest/ContainerAppTests.cs +++ b/src/SqlBuildManager.Console.ExternalTest/ContainerAppTests.cs @@ -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(); @@ -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[]{ diff --git a/src/SqlSync.SqlBuild/SqlBuildHelper.cs b/src/SqlSync.SqlBuild/SqlBuildHelper.cs index 6b71ee2d..0cbf60aa 100644 --- a/src/SqlSync.SqlBuild/SqlBuildHelper.cs +++ b/src/SqlSync.SqlBuild/SqlBuildHelper.cs @@ -17,6 +17,7 @@ using System.Linq; using SqlBuildManager.Interfaces.Console; using Microsoft.Extensions.Logging; +using Polly; namespace SqlSync.SqlBuild { /// @@ -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().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