PDO连接 - 最大连接 [英] PDO Connections - max connections

查看:104
本文介绍了PDO连接 - 最大连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下类:

 <?php 
类数据库{

protected static $ _instance;
protected $ _connection;
protected $ _dbhost = DB_HOST;
protected $ _dbname = DB_DBNAME;
protected $ _username = DB_USER;
protected $ _password = DB_PASS;
protected $ _dbType =mysql;

/ **
*单例模式实现使new不可用
* /
protected function __construct()
{
$ this - > _connection =
new PDO($ this-> _dbType。:host =。$ this-> _dbhost。; dbname =。$ this-> _dbname,$ this-> _username,$ this-> _password,array(PDO :: MYSQL_ATTR_INIT_COMMAND =>SET NAMES utf8,PDO :: ATTR_PERSISTENT => true));
}

public function getConnection()
{
return $ this-> _connection;
}

public static function getInstance()
{
if(null === self :: $ _ instance){
self :: $ _ instance = new self();
}
return self :: $ _ instance;
}

/ **
*单例模式实现使clone不可用
* /
protected function __clone()
{}
}

?>

发件人: http ://stackoverflow.com/a/3010486



现在我有了第二个类,它具有访问数据库的所有函数。

我在我的脚本中遇到最大连接数的问题,因此我使用新的数据库类。在我的帮助类中,我这样做:

 <?php 
帮助{

function getAllInvitesFromPlayer($ uid){
$ sql =SELECT request_id FROM.DBPREFIX。invites WHERE inviter_id =:uid AND joined = 1;

try {
$ db = Database :: getInstance() - > getConnection();
$ stmt = $ db-> prepare($ sql);
$ stmt-> bindParam(uid,$ uid);
$ stmt-> execute();
$ content = $ stmt-> fetch(PDO :: FETCH_LAZY);
$ db = null;
return $ content;
} catch(PDOException $ e){
echo $ e-> getMessage();
}
}
}

它正确使用 $ db = Database :: getInstance() - > getConnection(); 或者我应该外包到类变量, code> $ this-> _db; 在每个函数中并尝试?

什么更好,以避免获得太多的连接到我的数据库?



它一定更好只是在文件中初始化助手类,对吧?并不总是调用 $ helper = new helper(),因为这将总是创建一个新的DB连接,对吧?



感谢您的帮助!

解决方案



只要使用 getInstance(),它将始终是相同的单个连接,无论以何种方式或在哪里调用它。



为了封装,最好将db连接分配给类变量。



另请注意,您对try..catch的使用是错误的。



这样,

 <?php 
class helper {

protected function __construct()
{
$ this-> db = Database :: getInstance() getConnection();
}

function getAllInvitesFromPlayer($ uid){
$ sql =SELECT request_id FROM.DBPREFIX。invites WHERE inviter_id =?AND joined = 1;
$ stmt = $ this-> db-> prepare($ sql);
$ stmt-> execute(array($ uid));
return $ stmt-> fetchColumn(); //将返回一个实际的邀请
//或
return $ stmt-> fetchAll(PDO :: FETCH_COLUMN,0); //将返回所有邀请的确实
}
}


I have the following class:

<?php
class Database {

    protected static $_instance;
    protected $_connection;
    protected $_dbhost=DB_HOST;
    protected $_dbname=DB_DBNAME;
    protected $_username = DB_USER;
    protected $_password = DB_PASS;
    protected $_dbType = "mysql";

    /**
    * Singleton pattern implementation makes "new" unavailable
    */
    protected function __construct()
    {
        $this->_connection = 
            new PDO($this->_dbType . ":host=" . $this->_dbhost . ";dbname=" . $this->_dbname, $this->_username, $this->_password, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8", PDO::ATTR_PERSISTENT => true));
    }

    public function getConnection()
    {
        return $this->_connection;
    }

    public static function getInstance()
    {
        if (null === self::$_instance) {
            self::$_instance = new self();
        }
        return self::$_instance;
    }

    /**
    * Singleton pattern implementation makes "clone" unavailable
    */
    protected function __clone()
    {}
}

?>  

From: http://stackoverflow.com/a/3010486

Now I have a second class, which has all the functions that access the DB.

My question:
I had a problem with max connections in my script, therefore I use the new Database class. In my helper class I do it like this:

<?php   
class helper {

    function getAllInvitesFromPlayer($uid) {
        $sql = "SELECT request_id FROM ".DBPREFIX."invites WHERE inviter_id = :uid AND joined = 1";

        try {
            $db = Database::getInstance()->getConnection();
            $stmt = $db->prepare($sql);
            $stmt->bindParam("uid", $uid);
            $stmt->execute();
            $content = $stmt->fetch(PDO::FETCH_LAZY);
            $db = null;
            return $content;
        } catch(PDOException $e) {
            echo $e->getMessage();
        }
    }
}

After the "try" is it correct to use the $db = Database::getInstance()->getConnection(); or shall I "outsource" it to a class variable and access it like $this->_db; in each function and try ?
What is better to avoid getting too much connections to my db?

And its surely better just to initialize the helper class once in a file, right? and not always calling $helper = new helper() because this would always create a new DB connection, right?

Thank you for your help!

解决方案

It doesn't really matter.
As long as you are using getInstance(), it would be always the same single connection, no matter which way or where you call it.

For sake of incapsulation, it's better to assign db connection to a class variable.

Also note that your use of try..catch is wrong. It shouldn't be there.

So, something like this

<?php   
class helper {

    protected function __construct()
    {
        $this->db = Database::getInstance()->getConnection();
    }

    function getAllInvitesFromPlayer($uid) {
        $sql = "SELECT request_id FROM ".DBPREFIX."invites WHERE inviter_id = ? AND joined = 1";
        $stmt = $this->db->prepare($sql);
        $stmt->execute(array($uid));
        return $stmt->fetchColumn(); // will return one invite actually
        //or
        return $stmt->fetchAll(PDO::FETCH_COLUMN, 0); // will return ALL invites indeed
    }
}

这篇关于PDO连接 - 最大连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆