Search code examples
c#mysqldatabasemysql.datatransient-failure

How to identify transient errors in NET Standard 2.1 with MySql.Data.MySqlClient.MySqlException?


I'm writing my app with NET Standard 2.1 and MySql (NuGet pack MySql.Data). The MySqlException doesn't have the property IsTransient so I can't recognize if the exception is temporary. How can I manage this? Is there a list of error code that are transient? I can't find it.

Thanks


Solution

  • If switching NuGet packages is an option for you, the MySqlConnector package (disclaimer: I'm the lead author) implements this property: https://github.com/mysql-net/MySqlConnector/issues/849

    Otherwise, you could borrow its logic:

    public static bool IsTransient(MySqlException ex) => (MySqlErrorCode) ex.Code
        is MySqlErrorCode.ConnectionCountError
        or MySqlErrorCode.LockDeadlock
        or MySqlErrorCode.LockWaitTimeout
        or MySqlErrorCode.UnableToConnectToHost
        or MySqlErrorCode.XARBDeadlock