SQLSTATE[08004] [1040] Too Many Connections

Asked

Viewed 878 times

-4

This is my connection class:

/** * Conn.class [ CONNECTION ] * Abstract connection class. Singleton standard. * Returns a PDO object by the static getConn() method; * * @copyright (c) 2017, Horacio Pedro */ Abstract class Conn {

private static $Host = HOST;
private static $User = USER;
private static $Pass = PASS;
private static $Dbsa = DBSA;

/** @var PDO */
private static $Connect = null;

/**
 * Conecta com o banco de dados com o pattern singleton.
 * Retorna um objeto PDO!
 */
private static function Conectar() {
    try {
        if (self::$Connect == null):
            $dsn = 'mysql:host=' . self::$Host . ';dbname=' . self::$Dbsa;
            $options = [ PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES UTF8'];
            self::$Connect = new PDO($dsn, self::$User, self::$Pass, $options);
        endif;
    } catch (PDOException $e) {
        PHPErro($e->getCode(), $e->getMessage(), $e->getFile(), $e->getLine());
        die;
    }

    self::$Connect->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    return self::$Connect;
}

/** Retorna um objeto PDO Singleton Pattern. */
protected static function getConn() {
    return self::Conectar();
}

}

The site was functioning normally, after almost 30 days I came up with this: > SQLSTATE[08004] [1040] Too Many Connections

Do you have a viable solution to this kind of situation? What will be the cause behind this?

  • 2

    Probably the cause is too many connections to the bank, as the error says. For more details, you will need to describe, in the question ([Edit]), what is the structure of your application, put the codes referring to the problem, describe the architecture of your server, etc.

  • I had the same error in another technology, I just reviewed the code and had a connection being opened, but it was not closed, I just closed and no more this error happened.

1 answer

0


You can revise your code and try to isolate it better and subclasses, so you will better control the call of the connections. Try not to make multiple calls in the same method. If even dividing better the code the error continue you can increase the number of simultaneous connections in Mysql settings.

  • Thanks for the suggestion, thanks to the idea you gave me to review the Mysql settings I could notice that the problem was not in my code but in the Mysqlserver of the web hosting.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.