Search code examples
c#mysqltimeout

C# MySQL Wait Timeout


I'm currently working with a mysql connection in my vs 2017 project and I need to check if the wait_timeout exceeded. Unfortunately the state of the connection still shows open after the timeout. Is there a way to check that without try catch (try catch doesnt catch the thrown exception in my case)?


Solution

  • When the wait_timeout is exceeded, the MySQL Server simply closes the connection. No notification that this has happened is "pushed" to the client, so the connection state won't change; this can only be detected by trying to read from the underlying TCP socket. (For a deep technical writeup on this, see https://github.blog/2020-05-20-three-bugs-in-the-go-mysql-driver/.) Any subsequent use of that connection will typically throw an exception.

    There is a way to test the connection without throwing an exception: call MySqlConnection.Ping; if the connection is closed it will return false.