Skip to content

Commit

Permalink
Implement MySqlException.IsTransient. Fixes #849
Browse files Browse the repository at this point in the history
  • Loading branch information
bgrainger committed Jan 1, 2021
1 parent a69fea0 commit 396ad9d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/MySqlConnector/MySqlException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ public sealed class MySqlException : DbException
public override string? SqlState { get; }
#endif

/// <summary>
/// Returns <c>true</c> if this exception could indicate a transient error condition (that could succeed if retried); otherwise, <c>false</c>.
/// </summary>
#if NET45 || NET461 || NET471 || NETSTANDARD1_3 || NETSTANDARD2_0 || NETSTANDARD2_1 || NETCOREAPP2_1 || NETCOREAPP3_1
public bool IsTransient => IsErrorTransient(ErrorCode);
#else
public override bool IsTransient => IsErrorTransient(ErrorCode);
#endif

#if !NETSTANDARD1_3
private MySqlException(SerializationInfo info, StreamingContext context)
: base(info, context)
Expand Down Expand Up @@ -115,7 +124,15 @@ internal MySqlException(MySqlErrorCode errorCode, string? sqlState, string messa
internal static MySqlException CreateForTimeout() => CreateForTimeout(null);

internal static MySqlException CreateForTimeout(Exception? innerException) =>
new MySqlException(MySqlErrorCode.CommandTimeoutExpired, "The Command Timeout expired before the operation completed.", innerException);
new(MySqlErrorCode.CommandTimeoutExpired, "The Command Timeout expired before the operation completed.", innerException);

private static bool IsErrorTransient(MySqlErrorCode errorCode) =>
errorCode is MySqlErrorCode.CommandTimeoutExpired
or MySqlErrorCode.ConnectionCountError
or MySqlErrorCode.LockDeadlock
or MySqlErrorCode.LockWaitTimeout
or MySqlErrorCode.UnableToConnectToHost
or MySqlErrorCode.XARBDeadlock;

IDictionary? m_data;
}
Expand Down
1 change: 1 addition & 0 deletions tests/MySqlConnector.Tests/CancellationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ public void Test(int step, int method)
var ex = Assert.Throws<MySqlException>(() => s_executeMethods[method](command));
Assert.InRange(stopwatch.ElapsedMilliseconds, 2900, 3500);
Assert.Equal(MySqlErrorCode.CommandTimeoutExpired, ex.ErrorCode);
Assert.True(ex.IsTransient);

// connection is unusable
Assert.Equal(ConnectionState.Closed, connection.State);
Expand Down
3 changes: 3 additions & 0 deletions tests/SideBySide/ConnectSync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ public void ConnectBadHost()
using var connection = new MySqlConnection(csb.ConnectionString);
Assert.Equal(ConnectionState.Closed, connection.State);
var ex = Assert.Throws<MySqlException>(connection.Open);
#if !BASELINE
Assert.True(ex.IsTransient);
#endif
Assert.Equal((int) MySqlErrorCode.UnableToConnectToHost, ex.Number);
Assert.Equal((int) MySqlErrorCode.UnableToConnectToHost, ex.Data["Server Error Code"]);
Assert.Equal(ConnectionState.Closed, connection.State);
Expand Down

0 comments on commit 396ad9d

Please sign in to comment.