0

I have an code with PHP PDO which close the connections. Like this: PDO closing connection

But in mysql workbench the counter of total connections doesn't decrease. Every time I refresh the site the connections is counted up and will no descrease after that.

I have set already wait_timeout and interactive_timeout to 5 but nothing happened.

[EDIT]

class DatabaseFactory {
    private static $factory;
    private $connection = array(
        "DB_HOST" => "localhost",
        "DB_PASS" => "************",
        "DB_USER" => "************",
        "DB_PORT" => "3306",
        "DB_CHARSET" => "utf8"
    );

    public static function getFactory() {
        if(!self::$factory) {
            self::$factory = new DatabaseFactory();
        }
        return self::$factory;
    }

    private $db;

    public function getconnection($name) {
        echo "opened";
        try {
            $options = array(
                \PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_OBJ,
                \PDO::ATTR_ERRMODE => \PDO::ERRMODE_WARNING
            );
            $this->db = new \PDO('mysql:host='.$this->connection["DB_HOST"].';dbname='.$name.';port=' .$this->connection["DB_PORT"].';charset='.$this->connection["DB_CHARSET"], $this->connection["DB_USER"], $this->connection["DB_PASS"], $options);
        } catch (PDOException $e) {
            exit($e->getMessage());
        }
        return $this->db;
    }

    static function closeConnection(&$conn) {
        echo "closed";
        $conn = null;
    }
}

$database = DatabaseFactory::getFactory()->getconnection("Admin");
$query = $database->prepare("SELECT * FROM tester WHERE t_u_id = :u_id");
$query->execute(array(":u_id" => "281123341528-D050C4-91A0BA-1CB0C8-8112334152855AC373"));
var_dump($query->fetch());
DatabaseFactory::closeConnection($database);
Cœur
  • 37,241
  • 25
  • 195
  • 267
  • In order to help you with your problem could you provide your code. – AutoTester213 Sep 15 '17 at 13:52
  • Depends on the SAPI if the socket/connection is kept alive after `->close()`. Why does the automatic caching concern your script? Is this just about the misguided "oh, connections *have* to be closed" thing? – mario Sep 15 '17 at 13:55
  • possible duplicate of https://stackoverflow.com/questions/23613924/pdo-mysql-connection-close-unset-vs-null – mario Sep 15 '17 at 13:56
  • no because I think this is right to close the connection. But my server doesn't decrease the total connections amount. So I can't increase the max_connection value because the total connections will increased too and not be resetted. – Markus Nowotny Sep 15 '17 at 14:52

0 Answers0