connect选项打开多个新进程

connect选项打开多个新进程

本文介绍了mysqli使用p:connect选项打开多个新进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一次在这里问问题.我搜寻了Google,stackoverflow等,以寻求有关我遇到的问题的帮助.我们目前正在使用PHP 5.3.10&带有Apache 2.2.21(CentOS)的MySQL 5.0.95.

This is my first time asking a question on here. I've scoured Google, stackoverflow, etc. looking for help with the issue I'm having. We're currently using PHP 5.3.10 & MySQL 5.0.95 with Apache 2.2.21 (CentOS).

我们正在开始使用新代码从旧的mysql库过渡到mysqli的工作,我正在负责.我已经尝试过

We're in the process of starting to cut over from the old mysql library to mysqli in new code, and I'm leading the charge. I've tried

  • 确保在使用完数据库后明确关闭与数据库的连接
  • 处理完结果集后释放它们
  • 将连接限制从150个增加到250个

包含的文件(与会话检查等有关)使用旧样式mysql_pconnect()来验证某些内容.这些几乎包含在我们的所有代码中.

There are included files (having to do with session checking, etc.) that use the old style mysql_pconnect() to validate certain things. These are included in nearly all of our code.

类似于代码:

$mysqli =  new mysqli('p:'.DBHOST, DBUSER, DBPASS, $_SESSION['dbname']);
if ($mysqli->connect_error) {
    throw new Exception($mysqli->connect_error,  $mysqli->connect_errno);
    exit;
}
// do my stuff here, a bunch of SQL queries like:
$sql = 'SELECT * FROM MyTable';
$result = $mysqli->query($sql);
if (!$result) {
    throw new SQLException($sql, $mysqli);
    exit;
    // SQLException is an extension to mysqli_sql_exception that adds the
    // query into the messaging internally
}
while ($result && $row = $result->fetch_assoc()) {
// do stuff here, like show it on screen, etc., all works normally
}
$result->free(); // free up the result
$mysqli->close(); // close the connection to the database

释放结果并关闭连接是我在收到连接过多"错误后所做的事情.在此之前,我每次运行程序都会获得3-4个新的数据库连接. (在后端使用SHOW PROCESSLIST查看)

freeing the results and closing the connection were things I did after getting a "Too many connections" error. Before doing that, I would get 3-4 new database connections each time I ran my program. (viewed in the back end with SHOW PROCESSLIST)

该问题有所减轻(它添加了0到3个新连接,而不是每次添加3个新连接).

The problem is lessened somewhat (it adds 0 to 3 new connections, rather than 3 new connections each time).

我的一些读物暗示这可能与Apache线程+如果当前线程中不存在空闲连接而添加的新持久连接有关.是这个吗mysqli是否不能很好地支持持久连接? (我应该放弃持久性吗?)

Some of my reading suggests that this could have something to do with Apache threading + the new persistent connections added if there are no existing idle ones in the current thread. Is it this? Are persistent connections not supported well with mysqli? (should I give up on persistence?)

感谢您的任何建议.

推荐答案

我没有mysqli持久连接的经验,但是您的一些问题和期望对我来说似乎很奇怪.

I have no experience with mysqli persistent connections but some of your questions and expectations looks strange to me.

是的,这就是永久连接的目的

Yes, that's what permanent connections are for

您无法确保已明确将其关闭,因为您不能这样做.再次因为永久连接的唯一要点是持续打开

You cannot make sure you closed it explicitly as you just can't do that. Again because the only point of permanent connection is to lasts open

因此,您必须确保只打开一个.

So, you have to make sure you're opening only one.

  • 您的脚本中至少有两个连接-一种是旧样式,而有些则来自mysqli?
  • 实例化多少个mysqli对象?
  • 正在运行多少个php脚本来满足一个HTTP请求?确定吗?

毕竟,如果它困扰您如此之大,为什么要使用持久性连接?您从中获得任何真正的(不是想象中的)好处吗?毕竟,如果您的版本是5.3,为什么还要烦恼从mysql重写?

After all, if it bothers you so much, why you're using persistent connections? Are you any real (not imaginary) benefits from it?After all, if your version is 5.3, why bother with rewriting from mysql at all?

这篇关于mysqli使用p:connect选项打开多个新进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 06:41