事情是这样的:我的网站是smarty + adodb + mysql,最近经常出现 too many connections的错误。我已经把mysql的max_connection改为500,但查看mysql的threads_connected的连接数,不用几天就上升到500,接着就提示说too many connections的错误。
后来我查了资料,发现是我的adodb数据库没有关闭的原因。
我的问题是:因为我用的是smarty+adodb,其中用的最多的是$rs = $conn->GetArray(),我想问是不是每个$rs = $conn->GetArray()后面都要用$rs->Close()来释放资源,然后在页面的尾部用$conn->Close()来关闭数据库。因为我试过只在页面的尾部$conn->Close()来关闭数据库,根本没有效果,threads_connected显示的连接数还是不断上升。
分数不多,全给了,这个问题很严重,注:现在这个网站只有我一个人在访问,以后如果真的上线运营,就惨了,不到几个小时就打不开网站了

解决方案 »

  1.   

    普通数据库连接,在程序结束后会自动关闭。当然你也可以显式的提前关闭
    使用长连接的数据库连接,不会被关闭,只是在程序结束后被置为空闲。并且不能显式的关闭本来提供长连接是为了复用已存在的连接(这样可以提高远程数据库的访问速度),但从连接空闲到可被复用是要等待一点时间的
    因此 php 会因没有空闲的连接而创建新的长连接,直至资源耗尽建议你先检查一下是否使用了长连接,如果是,建议改成短连接
      

  2.   

    resource mysql_connect ([ string $server = ini_get("mysql.default_host") [, string $username = ini_get("mysql.default_user") [, string $password = ini_get("mysql.default_password") [, bool $new_link = false [, int $client_flags = 0 ]]]]] )new_link
    If a second call is made to mysql_connect() with the same arguments, no new link will be established, but instead, the link identifier of the already opened link will be returned. The new_link parameter modifies this behavior and makes mysql_connect() always open a new link, even if mysql_connect() was called before with the same parameters. In SQL safe mode, this parameter is ignored.唠叨说的是,先在PHP和MYSQL端去掉长连接选项再看看压力吧。