Bug #60799 "A socket operation was attempted to an unreachable host" on opening connection
Submitted: 7 Apr 2011 23:52 Modified: 26 Jun 2011 22:55
Reporter: Pavel Bazanov Email Updates:
Status: Closed Impact on me:
Category:Connector / NET Severity:S3 (Non-critical)
Version:6.3.5 OS:Windows
Assigned to: CPU Architecture:Any
Tags: GetSream, NativeDriver, open

[7 Apr 2011 23:52] Pavel Bazanov
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

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;

Again, I see this exception only when the GetServerCurrentDateTime() method is called by a timer.

How to repeat:
Not sure
[8 Apr 2011 21:37] MySQL Verification Team
Could you please try the version 6.3.6. Thanks.
[8 May 2011 23:00] Bugs System
No feedback was provided for this bug for over a month, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".
[26 Jun 2011 22:01] Pavel Bazanov
Yes, version 6.3.6 solves the problem. Thanks.
[26 Jun 2011 22:55] Reggie Burnett
Thank you for your bug report. This issue has already been fixed in the latest released version of that product, which you can download at