PHP PDO不能关闭连接

时间:2017-09-15 13:50:25

标签: php mysql pdo

我有一个PHP PDO代码,用于关闭连接。 像这样:PDO closing connection

但是在mysql工作台中,总连接的计数器不会减少。 每次刷新网站时,连接都会计算在内,并且在此之后不会减少。

我已将wait_timeout和interactive_timeout设置为5,但没有任何反应。

[编辑]

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);

0 个答案:

没有答案