太多打开的 MySQL 连接 [英] Too many open MySQL connections

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

问题描述

我正在开发一个项目,其中网页的两个部分经常更改并获取最近的数据.我对使用 mysql_connect 还是 mysql_pconnect 有一些困惑?我有一个包含在每个页面中的配置文件.有一个我用于查询的数据库连接对象.即使大约有 70 个用户在线,它也会在我的服务器上显示 20,000 个连接.请建议我保持单个用户的单个连接活动的最佳方法,因此当有 70 个用户在线时应该有 70 个连接.目前我没有使用 mysql_close 方法来关闭连接.这就是它显示这么多连接的原因吗?您的建议将不胜感激.

I am developing a project in which two portions of webpage frequently change and fetch recent data. I have some confusion about whether to use mysql_connect or mysql_pconnect? I have one config file that is being included in every page. There is one database connection object which I use for queries. Even when approximately 70 users are online it shows 20,000 connections on my server. Please suggest me the best way to keep a single connection alive from a single user, so there should be 70 connections when there are 70 users online. Currently I'm not using mysql_close method to close connection. Is this the reason it shows these many connections? Your advice will really be appreciated.

推荐答案

这种情况下常用的模式是单例模式,这里有一些粗略的代码.

A common pattern used in this case is the singleton pattern, here's some rough code.

class DB_Instance
{
    private static $db;

    public static function getDB()
    {
        if (!self::$db)
                self::$db = new Database(); 

        return self::$db;
    }
}


function getSomething()
{
    $conn = DB_Instance::getDB();
    .
    .
    .
}

一些例子/参考

http://tutorialpedia.org/tutorials/Singleton+pattern+in+PHP.htmlhttp://www.ricocheting.com/static/code/php/mysql-v3/Database.singleton.phpshttp://netlinxinc.com/netlinx-blog/53-php/7-applying-the-singleton-pattern-to-database-connections-in-php.html

http://tutorialpedia.org/tutorials/Singleton+pattern+in+PHP.html http://www.ricocheting.com/static/code/php/mysql-v3/Database.singleton.phps http://netlinxinc.com/netlinx-blog/53-php/7-applying-the-singleton-pattern-to-database-connections-in-php.html

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

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