Tim*_*Tim 10 c# mysql database
.NET MySqlClient(6.9.5.0)中有什么东西,当服务器池中的MySQL服务器没有响应时(可能是由于临时网络问题),服务器被列入黑名单或被永久绕过?在我们的日志记录中,我们注意到抛出了身份验证错误:
MySql.Data.MySqlClient.MySqlException (0x80004005): Authentication to host '<HOST>' for user '<USER>' using method 'mysql_native_password' failed with message: Reading from the stream has failed. ---> MySql.Data.MySqlClient.MySqlException (0x80004005): Reading from the stream has failed. ---> System.IO.EndOfStreamException: Attempted to read past the end of the stream.
Run Code Online (Sandbox Code Playgroud)
发生此错误后,每次尝试从数据库写入或读取都会失败,并显示以下错误消息:
MySql.Data.MySqlClient.MySqlException (0x80004005): No available server found.
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?当数据库服务器移动到运行我们的应用程序的同一主机上时,问题不再出现.
这里有两件事让我印象深刻:
异常的性质:System.IO.EndOfStreamException:尝试读取超过流末尾的内容。
事实上,当数据库与调用应用程序位于同一台计算机上时,您不会遇到此问题。
对我来说,这告诉我正在发生与传输或网络相关的错误,这会中断您的流。一旦发生这种情况,您的连接将被重置,因此对数据库的后续调用将失败。
我会尝试捕获 EndOfStreamException,并尝试正常关闭并重新打开连接。像下面这样的东西。如果这允许后续调用成功,那么您就发现了问题...尽管请记住,在此结构中,原始调用(在 try 块内)不会再次被调用。在进行后续调用之前,您还需要正确地重新创建并打开连接,因为它现在总是在finally块中处理。
try
{
//your work
}
catch (System.IO.EndOfStreamException ex)
{
System.Diagnostics.Debug.WriteLine("Stream exception caught: " + ex.Message.ToString());
}
finally
{
if(myConnection != null)
{
myConnection.Close();
myConnection.Dispose();
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
667 次 |
最近记录: |