Description:
Hello,
During the last year I started to get a SocketException once in a few days when opening connection to mysql server.
Here is the text from the log:
Exception type: MySql.Data.MySqlClient.MySqlException
Exception message: Unable to connect to any of the specified MySQL hosts.
Stack trace:
at MySql.Data.MySqlClient.NativeDriver.Open()
at MySql.Data.MySqlClient.Driver.Open()
at MySql.Data.MySqlClient.Driver.Create(MySqlConnectionStringBuilder settings)
at MySql.Data.MySqlClient.MySqlPool.CreateNewPooledConnection()
at MySql.Data.MySqlClient.MySqlPool.GetPooledConnection()
at MySql.Data.MySqlClient.MySqlPool.TryToGetDriver()
at MySql.Data.MySqlClient.MySqlPool.GetConnection()
at MySql.Data.MySqlClient.MySqlConnection.Open()
at RA.Common.DAL.DbManager.InitConnection() in C:\VCProjects\Alex\RA\RA.Common\DAL\DbManager.cs:line 42
at RA.Common.DAL.DbManager..ctor(Boolean createSeparateConnectionEvenIfUnderLocalTransaction) in C:\VCProjects\Alex\RA\RA.Common\DAL\DbManager.cs:line 79
at RA.Common.DAL.DbManager..ctor() in C:\VCProjects\Alex\RA\RA.Common\DAL\DbManager.cs:line 71
at RA.Common.DAL.ServerAccessor.GetServerCurrentDateTime() in C:\VCProjects\Alex\RA\RA.Common\DAL\ServerAccessor.cs:line 13
at RA.ClientForms.MainForm.CheckTimeDifferenceWithServer(Object sender, EventArgs e) in C:\VCProjects\Alex\RA\RA\ClientForms\MainForm.cs:line 492
at System.Windows.Forms.Timer.OnTick(EventArgs e)
at System.Windows.Forms.Timer.TimerNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
Inner exception information:
Exception type: System.Net.Sockets.SocketException
Exception message: A socket operation was attempted to an unreachable host 192.168.1.141:3306
Stack trace:
at System.Net.Sockets.Socket.EndConnect(IAsyncResult asyncResult)
at MySql.Data.Common.StreamCreator.CreateSocketStream(IPAddress ip, Boolean unix)
at MySql.Data.Common.StreamCreator.GetStream(UInt32 timeout)
at MySql.Data.MySqlClient.NativeDriver.Open()
The weird thing is - I have thousands of queries in my project and there many times a connection is opened during the application lifetime - but this exception always occurs in the same place. I mean, there are hundreds of connections to the server are created every day, but this exception occurs only in one place.
Here is the CheckTimeDifferenceWithServer() method, which is called every 10 minutes on timer:
public static DateTime GetServerCurrentDateTime()
{
using(var db = new DbManager())
{
return Convert.ToDateTime(db.ExecuteScalar("SELECT NOW()"));
}
}
DbManager class is just a very thin wrapper around ADO.NET and MySql classes. Here is its InitConnection() method, which is called from the constructor:
private void InitConnection()
{
_conn = _dataProvider.CreateConnectionObject();
// private const string _connStringTemplate = "server={0};database={1};User Id={2};password={3};Character Set=cp1251";
_conn.ConnectionString = _connectionString;
_conn.Open();
}
Again, I see this exception only when the GetServerCurrentDateTime() method is called by a timer.
How to repeat:
Not sure