我正在使用 Entity Framework (DevArt 的数据提供程序)处理 mysql 数据库。我注意到有时在日志中我会遇到这样的异常:
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
并且该异常同时发生多次(?一次断开多个连接..)并且在它发生之后 - 在一段时间内一切正常。我试过了
我知道这个错误可能太普遍了,但需要关于如何避免这种情况的建议吗?..
尝试增加池中的连接并 Validate Connection=true;
- 没有成功。
最佳答案
"Lost connection to MySQL server during query"错误的可能原因:
连接被 MySQL 服务器关闭:
在 MySQL 服务器的日志中查找任何错误
在您的 MySQL 服务器上检查 wait_timeout 变量的值
网络问题
此问题特定于 dotConnect for MySQL:设置“Validate Connection=true;”连接字符串中的选项,用于每次从池中检索连接时验证连接(性能较低,应在连接不稳定的环境中使用)。如果“验证连接=假;” (默认)- 每隔约 30 秒仅在池中验证一次连接(不检查不从池中检索它)
如果您明确提供与 DbContext(或 ObjectContext)对象的纯 ADO.NET 连接,请考虑以下几点:
MySqlConnection 对象不是线程安全的。您应该避免同时在多个线程中使用相同的 MySqlConnection
您可以在您的应用程序中实现故障转移逻辑:
- http://www.devart.com/dotconnect/mysql/docs/?Devart.Data.MySql~Devart.Data.MySql.MySqlConnection~ConnectionLost_EV.html
- http://www.devart.com/dotconnect/mysql/docs/?Devart.Data~Devart.Common.ConnectionLostCause.html
- http://www.devart.com/dotconnect/mysql/docs/?Devart.Data~Devart.Common.ConnectionLostContext.html
关于c# - 如何找到导致 EntityCommandExecutionException 出现的原因,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20838401/