Exemplo n.º 1
0
        private void addConnection()
        {
            DatabaseClient dbClient = new DatabaseClient(this, totalCurrentConnections);

            totalCurrentConnections++;
            try
            {
                dbClient.connect();
            }
            catch (Exception ex)
            {
                Out.writeLine("Error while connecting to database: " + ex.Message, Out.logFlags.ImportantLogLevel);
            }
            this.dbClientCollection.Enqueue(dbClient);
            Out.writeLine("Adding new client to the database manager", Out.logFlags.lowLogLevel);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Initializes the connections
        /// </summary>
        /// <returns>boolean indicating if the initialization process was succesfull</returns>
        public void init()
        {
            if (isConnected)
            {
                return;
            }
            try
            {
                Out.writeLine("Creating new database connections.. stand by..", Out.logFlags.ImportantLogLevel);
                MySqlConnectionStringBuilder mysqlSb = new MySqlConnectionStringBuilder();

                createNewConnectionString();

                this.dbClientCollection = new Queue();
                this.refreshStack       = new Queue();
                DatabaseClient dbClient;
                if (this.beginClientAmount != 0)
                {
                    for (int i = 0; i < this.beginClientAmount; i++)
                    {
                        Out.writeLine("Opening database connection [" + (i).ToString() + "] out of [" + this.beginClientAmount + "]", Out.logFlags.lowLogLevel);
                        addConnection();
                    }
                }
                else
                {
                    dbClient = new DatabaseClient(this, -1);
                    dbClient.connect();
                    dbClient.disconnect();
                }
                //Out.writePlain(connectionString, Out.logFlags.lowLogLevel);
            }
            catch (MySqlException ex)
            {
                isConnected = false;
                throw new Exception("Could not connect the clients to the database: " + ex.Message);
            }

            isConnected = true;
            checkThread = new Thread(this.healthChecker);
            this.checkThread.Start();
            Out.writeLine("Created new connections: [" + beginClientAmount + "]", Out.logFlags.ImportantLogLevel);
        }
Exemplo n.º 3
0
        private void healthChecker()
        {
            while (isConnected)
            {
                if (this.refreshStack.Count > 0)
                {
                    lock (this.refreshStack.SyncRoot)
                    {
                        if (this.refreshStack.Count > 0)
                        {
                            DatabaseClient        toReCheck;
                            List <DatabaseClient> failedClients = new List <DatabaseClient>(this.refreshStack.Count);
                            while (this.refreshStack.Count > 0)
                            {
                                toReCheck = this.refreshStack.Dequeue() as DatabaseClient;
                                toReCheck.disconnect();
                                toReCheck.Dispose();
                                toReCheck = new DatabaseClient(this, toReCheck.getID());
                                try
                                {
                                    toReCheck.connect();

                                    reportDone(toReCheck);
                                }
                                catch (Exception ex)
                                {
                                    failedClients.Add(toReCheck);
                                    Out.writeSeriousError("Exception while reconencting client [" + toReCheck.getID() + "] Message was -> " + ex.Message);
                                }
                            }
                            foreach (DatabaseClient client in failedClients)
                            {
                                this.refreshStack.Enqueue(client);
                            }
                        }
                    }
                }
                Thread.Sleep(100);
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Initializes the connections
        /// </summary>
        /// <returns>boolean indicating if the initialization process was succesfull</returns>
        public void init()
        {
            if (isConnected)
                return;
            try
            {
                Out.writeLine("Creating new database connections.. stand by..", Out.logFlags.ImportantLogLevel);
                MySqlConnectionStringBuilder mysqlSb = new MySqlConnectionStringBuilder();

                createNewConnectionString();

                this.dbClientCollection = new Queue();
                this.refreshStack = new Queue();
                DatabaseClient dbClient;
                if (this.beginClientAmount != 0)
                {
                    for (int i = 0; i < this.beginClientAmount; i++)
                    {
                        Out.writeLine("Opening database connection [" + (i).ToString() + "] out of [" + this.beginClientAmount + "]", Out.logFlags.lowLogLevel);
                        addConnection();
                        
                    }
                }
                else
                {
                    dbClient = new DatabaseClient(this, -1);
                    dbClient.connect();
                    dbClient.disconnect();
                }
                //Out.writePlain(connectionString, Out.logFlags.lowLogLevel);

            }
            catch (MySqlException ex)
            {
                isConnected = false;
                throw new Exception("Could not connect the clients to the database: " + ex.Message);
            }

            isConnected = true;
            checkThread = new Thread(this.healthChecker);
            this.checkThread.Start();
            Out.writeLine("Created new connections: [" + beginClientAmount + "]", Out.logFlags.ImportantLogLevel);
            

        }
Exemplo n.º 5
0
        private void healthChecker()
        {
            while (isConnected)
            {
                if (this.refreshStack.Count > 0)
                {
                    lock (this.refreshStack.SyncRoot)
                    {
                        if (this.refreshStack.Count > 0)
                        {
                            DatabaseClient toReCheck;
                            List<DatabaseClient> failedClients = new List<DatabaseClient>(this.refreshStack.Count);
                            while (this.refreshStack.Count > 0)
                            {
                                toReCheck = this.refreshStack.Dequeue() as DatabaseClient;
                                toReCheck.disconnect();
                                toReCheck.Dispose();
                                toReCheck = new DatabaseClient(this, toReCheck.getID());
                                try
                                {
                                    toReCheck.connect();

                                    reportDone(toReCheck);
                                }
                                catch (Exception ex)
                                {
                                    failedClients.Add(toReCheck);
                                    Out.writeSeriousError("Exception while reconencting client [" + toReCheck.getID() + "] Message was -> " + ex.Message);
                                }
                            }
                            foreach (DatabaseClient client in failedClients)
                            {
                                this.refreshStack.Enqueue(client);
                            }
                        }
                    }
                }
                Thread.Sleep(100);

            }
        }
Exemplo n.º 6
0
 private void addConnection()
 {
     DatabaseClient dbClient = new DatabaseClient(this,totalCurrentConnections);
     totalCurrentConnections++;
     try
     {
         dbClient.connect();
     }
     catch (Exception ex)
     {
         Out.writeLine("Error while connecting to database: " + ex.Message, Out.logFlags.ImportantLogLevel);
     }
     this.dbClientCollection.Enqueue(dbClient);
     Out.writeLine("Adding new client to the database manager", Out.logFlags.lowLogLevel);
 }
Exemplo n.º 7
0
 private void addConnection(int id)
 {
     DatabaseClient item = new DatabaseClient(this, id);
     item.connect();
     this.databaseClients.Add(item);
 }
Exemplo n.º 8
0
 public void init()
 {
     try
     {
         DatabaseClient client;
         MySqlConnectionStringBuilder builder = new MySqlConnectionStringBuilder();
         this.createNewConnectionString();
         this.databaseClients = new List<DatabaseClient>((int) this.maxPoolSize);
         if (this.beginClientAmount != 0)
         {
             for (int i = 0; i < this.beginClientAmount; i++)
             {
                 client = new DatabaseClient(this, i);
                 if (this.dbEvent != null)
                 {
                     this.dbEvent(client);
                 }
                 client.connect();
                 this.databaseClients.Add(client);
             }
         }
         else
         {
             client = new DatabaseClient(this, -1);
             client.connect();
             client.disconnect();
         }
     }
     catch (MySqlException exception)
     {
         this.isConnected = false;
         throw new Exception("Could not connect the clients to the database: " + exception.Message);
     }
     this.isConnected = true;
 }