Search code examples
c#mysqlexceptionexecutiondevart

How to find whats causing EntityCommandExecutionException appearance


I'm working with mysql database using entity framework(DevArt's data providers). I've noticed that sometimes in logs i get such exception:

System.Data.EntityCommandExecutionException: An error occurred while executing the command definition. See the inner exception for details.
---> Devart.Data.MySql.MySqlException: Lost connection to MySQL server during query
---> System.TimeoutException: Server did not respond within the specified timeout interval.
---> System.IO.IOException: Unable to read data from the transport connection: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
---> System.Net.Sockets.SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

And that exception is happening several times simultaneously(? dropping several connections at once..) and after it happens - everything works fine for some time. I've tried

I know that this error is probably too general, but need an advice on what to do to avoid that?.. Tried increasing connections in pool and Validate Connection=true; - no luck.


Solution

  • The possible reasons of the "Lost connection to MySQL server during query" error:

    1. connection was closed by MySQL server:

      • look for any errors in the logs of MySQL server

      • check the value of wait_timeout variable at your MySQL server

    2. network problems

    3. the issue is specific to dotConnect for MySQL: set "Validate Connection=true;" option in the connection string to validate connection each time on retrieving it from the pool (less performance, should be used in the environment with unstable connections). If "Validate Connection=false;" (default) - connection is validated only in the pool every ~30 seconds (no check no retrieving it from the pool)

    If you are explicitly providing plain ADO.NET connection to your DbContext (or ObjectContext) object, take into account the following points:

    1. the MySqlConnection object is not thread safe. You should avoid using the same MySqlConnection in several threads at the same time

    2. you can implement a failover logic in your application: