MySqlConnection Connect () Método muy lento para lanzar una excepción cuando la base de datos se ha ido C#

i'm creating a C# Server, that opens a named pipe and collects data that clients send to it. Basically, i'm storing the collected information in a mysql database, using .NET MySql Connector and the MySqlConnection Class. I also added a fallback rule, which in case the DB is out of reach, buffers the info sent by the clients in an SQL file and imports it into the DB when it is available again. Everything works fine, except when the DB is gone completely (for example when the server that runs the DB is not on the network anymore because it crashed), the Connect() Method takes forever to throw an exception. This is unacceptable, because the clients send in A LOT of data. Is there any way to fix this? If the query fails because a table is not there or something like that, it returns an error immediately.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;
using MySql.Data.MySqlClient;

namespace PipeServer
{
    class MySqlConnector
    {
        //register logger
        private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        public delegate void MessageReceivedHandler(string message);
        public event MessageReceivedHandler MessageReceived;

        private string host;
        private string database;
        private string user;
        private string password;
        private MySqlConnection connection;
        private List<string> rows = new List<string>();



        public MySqlConnector()
        {
            this.host = Settings.Default.db_host;
            this.database = Settings.Default.db_schema;
            this.user = Settings.Default.db_user;
            this.password = Settings.Default.db_pw;
            string myConnectionString = "SERVER=" + this.host + ";" +
                        "DATABASE=" + this.database + ";" +
                        "UID=" + this.user + ";" +
                        "PASSWORD=" + this.password + ";";

            connection = new MySqlConnection(myConnectionString);




        }

        public bool Connect()
        {
            if (connection.State != System.Data.ConnectionState.Open)
            {
                try
                {
                    connection.Open();
                    this.MessageReceived("Connected to Database");
                    return true;
                }
                catch (MySqlException ex)
                {
                    log.Error("failed to connect! mysql error: " + ex);
                    this.MessageReceived("No Database connection - Error: "+ex);
                    return false;
                }

            }
            else
            {
                this.MessageReceived("Connected to Database");
                return true;
            }

        }
        .
        .
        .
    }
}

Any hint is highly appreciated!Thanks in advance!

preguntado el 03 de mayo de 12 a las 16:05

1 Respuestas

When you mean forever you mean something like 30s ?

You're probably looking for adding TimeOut setting into the ConnectionString.

http://www.connectionstrings.com/mysql

contestado el 03 de mayo de 12 a las 16:05

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.