简体   繁体   English

MySQL连接重用问题

[英]MySQL connection reuse problem

I am trying to reuse mysql connection. 我正在尝试重用mysql连接。 Hence I have a global variable in databasemanager.php class that returns a connection. 因此,我在databasemanager.php类中有一个全局变量,该变量返回连接。

The problem is somehow on one particular page mysql is executing prior query as well. 问题是,在某个特定页面上,mysql也在执行先前的查询。

Looks like there is some leftover query in connection object that gets executed if same connection is being reused . 看起来如果重新使用相同的连接,则连接对象中有一些剩余查询将被执行。 Is it possible ? 可能吗 ? how to solve this .. 如何解决这个..

 function getDBConnection(){
   global $conn;
   if (!empty($conn)){
// echo $conn ;
    return $conn;
   }
   $conn =  mysql_connect($GLOBALS['HOSTNAME'],$GLOBALS['DBUSER'],$GLOBALS['DBPASS']);

    if (!$conn) {
     die('Could not connect: ' . mysql_error());
    }

    mysql_select_db($GLOBALS['DBNAME'],$conn);
    return $conn;

} }

This how code lookslike 代码看起来像这样

Have you looked into mysql_pconnect ? 你看过mysql_pconnect吗?

According to the documentation: 根据文档:

mysql_pconnect() acts very much like mysql_connect() with two major differences. mysql_pconnect()的行为与mysql_connect()非常相似,但有两个主要区别。

First, when connecting, the function would first try to find a (persistent) link that's already open with the same host, username and password. 首先,在连接时,该函数将首先尝试查找已经使用相同的主机,用户名和密码打开的(持久)链接。 If one is found, an identifier for it will be returned instead of opening a new connection. 如果找到一个,将返回其标识符,而不是打开一个新的连接。

Second, the connection to the SQL server will not be closed when the execution of the script ends. 其次,在脚本执行结束时,不会关闭与SQL Server的连接。 Instead, the link will remain open for future use (mysql_close() will not close links established by mysql_pconnect()). 相反,该链接将保持打开状态以供将来使用(mysql_close()不会关闭mysql_pconnect()建立的链接)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM