c# - 如何找到导致 EntityCommandExecutionException 出现的原因

标签 c# mysql exception execution devart

我正在使用 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"错误的可能原因:

  1. 连接被 MySQL 服务器关闭:

    • 在 MySQL 服务器的日志中查找任何错误

    • 在您的 MySQL 服务器上检查 wait_timeout 变量的值

  2. 网络问题

  3. 此问题特定于 dotConnect for MySQL:设置“Validate Connection=true;”连接字符串中的选项,用于每次从池中检索连接时验证连接(性能较低,应在连接不稳定的环境中使用)。如果“验证连接=假;” (默认)- 每隔约 30 秒仅在池中验证一次连接(不检查不从池中检索它)

如果您明确提供与 DbContext(或 ObjectContext)对象的纯 ADO.NET 连接,请考虑以下几点:

  1. MySqlConnection 对象不是线程安全的。您应该避免同时在多个线程中使用相同的 MySqlConnection

  2. 您可以在您的应用程序中实现故障转移逻辑:

关于c# - 如何找到导致 EntityCommandExecutionException 出现的原因,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20838401/

相关文章:

c# - 连接到 RFID 阅读器时出错 : The maximum baud rate for the device is 8190

c# - 当不是 8 位 char 时,使用零填充将字符串转换为 C# 中的二进制序列

c# - IdentityServer4在asp.net core中注册UserService并从数据库中获取用户

MySQL,将数据插入多列

php - 设置根据 MySQL 值选择的下拉列表

c# - 如何将 Foreach 语句转换为 linq 表达式?

java - 如何从 Java 中的PreparedStatement 获取日期和时间?

java - 如何在java中截断长异常消息

c++ - -fno-exceptions 对于某些库?

java - Java 中的高效异常处理