1

I got strange exception at connection.Open() to remote Mysql base. This code is working fine but sometimes this error pops

Here is a code:

private static string
            DatabaseServer = "",
            DatabasePort = "",
            DatabaseName = "",
            DatabaseUserID = "",
            DatabasePassword = "";

    public static string ConnectionString = String.Format("SERVER={0};PORT={1};DATABASE={2};UID={3};PASSWORD={4}",

                DatabaseServer, DatabasePort, DatabaseName, DatabaseUserID, DatabasePassword);

                using (MySqlConnection connection = new MySqlConnection(ConnectionString))
                {
                    try
                    {
                        if (connection != null)
                        {
                            connection.Open();

                            string query = @"....";

                            MySqlCommand cmd = new MySqlCommand(query, connection);
                            cmd.CommandType = CommandType.Text;

                            using (MySqlDataReader dataReader = cmd.ExecuteReader())
                            {
                                while (dataReader.Read())
                                {

                                }
                            }        
                        }
                    }
                    catch
                    {}
                }

Problem is when it comes at connection.Open(); even if it's in try{}catch{} it's terminating application.

Exception log:

System.Reflection.TargetInvocationException: An exception occurred during the operation, making the result invalid. Check InnerException for exception details. InnerException. ---> MySql.Data.MySqlClient.MySqlException: Unable to connect to any of the specified MySQL hosts. ---> System.Exception: Call to GetHostEntry failed after 00:00:00 while querying for hostname '': SocketErrorCode=TryAgain, ErrorCode=11002, NativeErrorCode=11002. ---> System.Net.Sockets.SocketException: This is usually a temporary error during hostname resolution and means that the local server did not receive a response from an authoritative server.

   в System.Net.Dns.GetAddrInfo(String name)
   в System.Net.Dns.InternalGetHostByName(String hostName, Boolean includeIPv6)
   в System.Net.Dns.GetHostEntry(String hostNameOrAddress)
   в MySql.Data.Common.StreamCreator.GetDnsHostEntry(String hostname)
   ---  ---
   в MySql.Data.Common.StreamCreator.GetDnsHostEntry(String hostname)
   в MySql.Data.Common.StreamCreator.GetHostEntry(String hostname)
   в MySql.Data.Common.StreamCreator.GetStreamFromHost(String pipeName, String hostName, UInt32 timeout)
   в MySql.Data.Common.StreamCreator.GetStream(UInt32 timeout)
   в MySql.Data.MySqlClient.NativeDriver.Open()
   ---  ---
   в MySql.Data.MySqlClient.NativeDriver.Open()
   в MySql.Data.MySqlClient.Driver.Open()
   в MySql.Data.MySqlClient.Driver.Create(MySqlConnectionStringBuilder settings)
   в MySql.Data.MySqlClient.MySqlPool.GetPooledConnection()
   в MySql.Data.MySqlClient.MySqlPool.TryToGetDriver()
   в MySql.Data.MySqlClient.MySqlPool.GetConnection()
   в MySql.Data.MySqlClient.MySqlConnection.Open()
   в System.ComponentModel.BackgroundWorker.OnDoWork(DoWorkEventArgs e)
   в System.ComponentModel.BackgroundWorker.WorkerThreadStart(Object argument)
   ---  ---
   в System.ComponentModel.AsyncCompletedEventArgs.RaiseExceptionIfNecessary()
   в System.ComponentModel.RunWorkerCompletedEventArgs.get_Result()
   в Server.Engines.DonateSQL.bw_RunWorkerCompleted(Object sender, RunWorkerCompletedEventArgs e) в System.ComponentModel.BackgroundWorker.OnRunWorkerCompleted(RunWorkerCompletedEventArgs e)
   в System.ComponentModel.BackgroundWorker.AsyncOperationCompleted(Object arg)
   в System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(Object state)
   в System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
   в System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
   в System.Threading.ThreadPoolWorkQueue.Dispatch()
   в System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()

I don't known what to do. Tried everything and can't find because of what this is happening.

5
  • 3
    Have you tried to read the exception message? "strange exception" --- what is "strange"? Commented Mar 18, 2013 at 23:42
  • The problem that it works fine but sometimes this error happens. Commented Mar 18, 2013 at 23:44
  • "strange exception" for me :) Because it's happening while everything must be ok and connection is possible. Commented Mar 18, 2013 at 23:51
  • look at the exact class and method the exception was thrown. Thoughts? Commented Mar 18, 2013 at 23:52
  • Too tired and before going to sleep need to solve it and didn't pay attention for obvious :( Commented Mar 19, 2013 at 0:04

2 Answers 2

2

System.Exception: Call to GetHostEntry failed after 00:00:00 while querying for hostname ''

Have a look at your calling code, see if there's some reason why Databaseserver is an empty string. If that's not happening then wake your network boys up, as it would appear that your DNS is having having hiccups. Don't know what your set up is but if you can afford to replace the names with IPs and it stops happening, then you can point a big dirty finger at them.

Sign up to request clarification or add additional context in comments.

1 Comment

Databaseserver string is empty here because I removed DNS address before post. Problem isn't in this.
0

Could there be an Network and/or DNS issue:

System.Reflection.TargetInvocationException: An exception occurred during the operation, making the result invalid. Check InnerException for exception details. InnerException. ---> MySql.Data.MySqlClient.MySqlException: Unable to connect to any of the specified MySQL hosts. ---> System.Exception: Call to GetHostEntry failed after 00:00:00 while querying for hostname '': SocketErrorCode=TryAgain, ErrorCode=11002, NativeErrorCode=11002. ---> System.Net.Sockets.SocketException: This is usually a temporary error during hostname resolution and means that the local server did not receive a response from an authoritative server.

2 Comments

"Could there be an Network and/or DNS issue:" additional proof to your answer is the method the exception was thrown at: System.Net.Dns.GetAddrInfo(String name)
@GladiatoR Specifying the IP would take DNS out of the picture in terms of the fault, but DNS isn't just there so you don't have to remember the IP.. You lose that as well. Using IP doesn't solve, it dodges.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.