php CodeIgniter 数据库连接未关闭

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7173104/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:11:41  来源:igfitidea点击:

CodeIgniter database connections not being closed

phpdatabasecodeigniterdatabase-connection

提问by ArthurGuy

I have built a social community website in CodeIgniter which is now getting a fair bit of traffic, the hosting company have started complaining and saying that the database is receiving null connections as well as connections which aren't being closed.

我在 CodeIgniter 中建立了一个社交社区网站,该网站现在获得了相当多的流量,托管公司开始抱怨并说数据库正在接收空连接以及未关闭的连接。

I am not entirely sure what a null query is or how one would end up being issued, any ideas?

我不完全确定空查询是什么,或者最终会如何发布,有什么想法吗?

I have added in additional code to force close connections when the code reaches an end but apparently this isn't working.

我添加了额外的代码以在代码结束时强制关闭连接,但显然这不起作用。

Can anyone offer any suggestions as to where to look or start debugging an issue like this?

任何人都可以就在哪里查看或开始调试这样的问题提供任何建议吗?

Thanks

谢谢

I have the following at the bottom of my core MY_Controller

我的核心底部有以下内容 MY_Controller

public function __destruct() {
    $this->db->close();
}

回答by Wallysson Nunes

I think that you're gettting problemns with the initial configurations from Codeigniter connection database.

我认为您在 Codeigniter 连接数据库的初始配置中遇到了问题。

At this page, you can see each value from config array:
https://www.codeigniter.com/user_guide/database/configuration.html

在此页面,您可以看到配置数组中的每个值:https:
//www.codeigniter.com/user_guide/database/configuration.html

$db['default']['hostname'] = "localhost";
$db['default']['username'] = "root";
$db['default']['password'] = "";
$db['default']['database'] = "database_name";
$db['default']['dbdriver'] = "mysql";
$db['default']['dbprefix'] = "";
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = FALSE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = "";
$db['default']['char_set'] = "utf8";
$db['default']['dbcollat'] = "utf8_general_ci";
$db['default']['swap_pre'] = "";
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;

This it's an basic array config database, try to set the variable of pconnect to FALSE, when it is turned on, the system doesn't close any connection and it stay opened to news queries at any time.

这是一个基本的数组配置数据库,尝试将pconnect的变量设置为FALSE,当它打开时,系统不会关闭任何连接并随时保持打开新闻查询状态。

If you set it to false like i said, your system will continue working perfectly, but the codeigniter will close and open the connection when he need's to use the database.

如果你像我说的那样将它设置为 false,你的系统将继续完美地工作,但是当他需要使用数据库时,codeigniter 将关闭并打开连接。

Here, you can find and post inside codeigniter forum with a guy that's have a problem with pconnect variables, may help you! http://codeigniter.com/forums/viewthread/177573/#842016

在这里,您可以在 codeigniter 论坛中找到并与一个对 pconnect 变量有问题的人一起发帖,可能会对您有所帮助! http://codeigniter.com/forums/viewthread/177573/#842016

回答by simnom

Codeigniter should automatically close the database connection but you can implicitly call it with $this->db->close();

Codeigniter 应该自动关闭数据库连接,但您可以使用隐式调用它 $this->db->close();

See http://codeigniter.com/user_guide/database/connecting.html

请参阅http://codeigniter.com/user_guide/database/connecting.html

回答by Marco Monteiro

have you tried to do $this->db->close();after a query is made?

你有没有试过$this->db->close();在查询之后做?

回答by Matt Barry

I have had similar problems with CodeIgniter in the past and they were caused by persistent connections in MySQL which is enabled by default in CodeIgniter. I used the SHOW PROCESSLISTquery in MySQL to view open connections, their current running query, the time (in seconds) the connection has been open for, etc. If the connection is idle the Commandfield returned would contain Sleepand the Infofield (the query) would be NULLwhich is probably what your host is referring to. That's a good place to start with an issue like this.

过去我在 CodeIgniter 上遇到过类似的问题,它们是由 MySQL 中的持久连接引起的,在 CodeIgniter 中默认启用。我使用SHOW PROCESSLISTMySQL 中的查询来查看打开的连接、它们当前运行的查询、连接打开的时间(以秒为单位)等。如果连接空闲,Command返回的字段将包含SleepInfo字段(查询)将是NULL这可能是您的主机所指的。这是从这样的问题开始的好地方。

Another thing that I want to note is a nuance of PHP, mysql_closewhich is being called herefrom your controller's __destructmethod will not close a persistent MySQL connection. PHP will close non-persistent connections at the end of the script's execution so it is usually not necessary to call it.

我要注意的另一件事是 PHP 的细微差别,这里从控制器的方法mysql_close调用它不会关闭持久的 MySQL 连接。PHP 将在脚本执行结束时关闭非持久连接,因此通常不需要调用它。__destruct

I realize that you had said persistent connections were not enabled, this was how I went about debugging my problem which sounds very similar to your problem.

我意识到您曾说过未启用持久连接,这就是我调试问题的方式,这听起来与您的问题非常相似。

回答by blagi

I solved same problem on one CI site by changing driver 'mysql' to 'mysqli' in: Database.php:

我通过将驱动程序“mysql”更改为“mysqli”在一个 CI 站点上解决了同样的问题:Database.php:

$db['default']['dbdriver'] = "mysqli";

回答by landons

I had that same issue a while back, and the solution was multi-part (no single part of the problem surfaced until all parts became problematic together). PConnect should be off unless you know how to use it (as others have said).

不久前我遇到了同样的问题,解决方案是多部分的(在所有部分都出现问题之前,问题的任何部分都不会浮出水面)。PConnect 应该关闭,除非您知道如何使用它(正如其他人所说)。

Another thing to consider is whether your web server is threaded (such as Apache's worker mode--most common web servers are). If you're getting a lot of traffic, and your threads aren't closing quickly, you might be hitting some concurrent web server / database server connection limits, or exhausting memory/processor resources. That could cause some database connections to hang.

另一件要考虑的事情是你的 web 服务器是否是线程的(比如 Apache 的工作模式——最常见的 web 服务器是)。如果您获得大量流量,并且您的线程没有快速关闭,您可能会遇到一些并发 Web 服务器/数据库服务器连接限制,或者耗尽内存/处理器资源。这可能会导致某些数据库连接挂起。

So, I'd check for other signs of the problem, and not assume the database is the source. It could very well be a symptom. Do you have any information about the null connections (like which database user spawned them)? That could help you trace it back...

所以,我会检查问题的其他迹象,而不是假设数据库是源。这很可能是一种症状。你有关于空连接的任何信息(比如哪个数据库用户产生了它们)?这可以帮助您追溯...

Edit:One thing I forgot--sometimes PHP errors can screw up CI's destruct operations (which automatically close the connections, from my understanding), so you might check your error logs too.

编辑:我忘了一件事——有时 PHP 错误会搞砸 CI 的破坏操作(根据我的理解,它会自动关闭连接),所以你也可以检查你的错误日志。