From 7595acb7405c88031c4e0c14a08d31ac9f3bcac1 Mon Sep 17 00:00:00 2001 From: MattMo Date: Thu, 2 Feb 2023 08:42:29 -0800 Subject: [PATCH] Added MySql ErrorCode to MySqlCommandException. Cleaned up code. Bumped version to 1.0.9 --- MySqlPlus/MySqlCommandException.cs | 18 ++++++++++++++++ MySqlPlus/MySqlManagedConnection.cs | 32 ++++++++++++++--------------- MySqlPlus/MySqlPlus.csproj | 2 +- 3 files changed, 35 insertions(+), 17 deletions(-) diff --git a/MySqlPlus/MySqlCommandException.cs b/MySqlPlus/MySqlCommandException.cs index 8bacfc7..593babc 100644 --- a/MySqlPlus/MySqlCommandException.cs +++ b/MySqlPlus/MySqlCommandException.cs @@ -17,6 +17,11 @@ namespace MontoyaTech.MySqlPlus /// public MySqlCommand Command = null; + /// + /// The MySqlErrorCode if any for this exception. -1 means no code. + /// + public int ErrorCode = -1; + /// /// Creates a new MySqlCommandException with a command. /// @@ -35,5 +40,18 @@ namespace MontoyaTech.MySqlPlus { this.Command = command; } + + /// + /// Creates a new MySqlCommandException with a command and inner exception if any. + /// + /// + /// + /// + public MySqlCommandException(MySqlCommand command, int errorCode, Exception inner) : base($"Failed to execute MySqlCommand. ErrorCode: {errorCode} Query: {command.CommandText}", inner) + { + this.Command = command; + + this.ErrorCode = errorCode; + } } } diff --git a/MySqlPlus/MySqlManagedConnection.cs b/MySqlPlus/MySqlManagedConnection.cs index b9c5e47..2907369 100644 --- a/MySqlPlus/MySqlManagedConnection.cs +++ b/MySqlPlus/MySqlManagedConnection.cs @@ -182,13 +182,13 @@ namespace MontoyaTech.MySqlPlus innerException = innerException.InnerException; //Try to get the MySql error code - int code = -1; + int errorCode = -1; if (ex is MySqlException) - code = ((MySqlException)ex).Number; + errorCode = ((MySqlException)ex).Number; //See if we have reached our max retry. if (i + 1 >= maxRetrys || !retry) - throw new MySqlCommandException(command, ex); + throw new MySqlCommandException(command, errorCode, ex); //See if the connection was invalid if (innerException.GetType().IsAssignableFrom(typeof(System.Net.Sockets.SocketException)) || @@ -200,12 +200,12 @@ namespace MontoyaTech.MySqlPlus { //Attempt to reconnect, but if this fails, it means we can't try any more since the reconnect retrys more than once. if (!this.Reconnect(maxRetrys, exponentialBackoff)) - throw new MySqlCommandException(command, ex); + throw new MySqlCommandException(command, errorCode, ex); } //See if we should retry or just throw. - else if (!ShouldRetryBasedOnMySqlErrorNum(code)) + else if (!ShouldRetry(errorCode)) { - throw new MySqlCommandException(command, ex); + throw new MySqlCommandException(command, errorCode, ex); } else { @@ -259,13 +259,13 @@ namespace MontoyaTech.MySqlPlus innerException = innerException.InnerException; //Try to get the MySql error code if we can - int code = -1; + int errorCode = -1; if (ex is MySqlException) - code = ((MySqlException)ex).Number; + errorCode = ((MySqlException)ex).Number; //See if we have reached our max retry. if (i + 1 >= maxRetrys || !retry) - throw new MySqlCommandException(command, ex); + throw new MySqlCommandException(command, errorCode, ex); //See if the connection was invalid if (innerException.GetType().IsAssignableFrom(typeof(System.Net.Sockets.SocketException)) || @@ -277,12 +277,12 @@ namespace MontoyaTech.MySqlPlus { //Attempt to reconnect, but if this fails, it means we can't try any more since the reconnect retrys more than once. if (!this.Reconnect(maxRetrys, exponentialBackoff)) - throw new MySqlCommandException(command, ex); + throw new MySqlCommandException(command, errorCode, ex); } //See if we should retry or just throw. - else if (!ShouldRetryBasedOnMySqlErrorNum(code)) + else if (!ShouldRetry(errorCode)) { - throw new MySqlCommandException(command, ex); + throw new MySqlCommandException(command, errorCode, ex); } else { @@ -300,14 +300,14 @@ namespace MontoyaTech.MySqlPlus } /// - /// Returns whether or not we should retry a query based on the MySql error number. + /// Returns whether or not we should retry a query based on the MySql error code. /// - /// + /// /// - private static bool ShouldRetryBasedOnMySqlErrorNum(int number) + private static bool ShouldRetry(int errorCode) { //List of codes here: https://www.briandunning.com/error-codes/?source=MySQL - switch (number) + switch (errorCode) { case 1021: case 1023: diff --git a/MySqlPlus/MySqlPlus.csproj b/MySqlPlus/MySqlPlus.csproj index 6f2249a..c31b3b7 100644 --- a/MySqlPlus/MySqlPlus.csproj +++ b/MySqlPlus/MySqlPlus.csproj @@ -7,7 +7,7 @@ MontoyaTech.MySqlPlus MontoyaTech.MySqlPlus MontoyaTech.MySqlPlus - 1.0.8 + 1.0.9 MontoyaTech A simple C# library to help work with MySql. MontoyaTech 2023