Re: MySQLi not closing connections

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi Fergus, you're correct, the original code was developed in a hasty
fashion by the developer that I am now working alongside.
It's a situation where he's been rushed for a year, and hadn't had the
time to get more organized.

I've been going through the code, and cleaning up where possible,
while also attempting to retain compatibility with existing code.

The mysql/mysqli implementation in the central config.php, only allows
either solution (mysql or mysqli) to be instantiated once by
config.php, based on the existence of certain constants. (And this is
regardless of how many times a script may try to include config.php)

I have not yet found *any* instance of pconnect being used anywhere.
Additionally, as mentioned, this issue only happens when I have the
logging functionality enabled, which is 4-5 lines of code that exist
in the shutdown function.

I would normally think there were problems elsewhere in the code, if
the issues didn't stop once I comment out the logging functionality.

Hopefully someone can see an error on my part. Here is my shutdown
function (instantiated in config.php):

<?php
/* Snippet */
	function shutdown() {
		global $time_start, $_COOKIE;

		$load_time = microtime(true) - $time_start;

		$activity_log = array();
		$activity_log['username'] = $_COOKIE['ID_my_site'];
		$activity_log['server'] = $_SERVER['SERVER_NAME'];
		$activity_log['url'] = $_SERVER['PHP_SELF'];
		$activity_log['referer'] = $_SERVER['HTTP_REFERER'];
		$activity_log['load_time'] = $load_time;
		$activity_log['server_var'] = serialize($_SERVER);
		if(!empty($_POST)) {
			$activity_log['post_var'] = serialize($_POST);
		}
		if(!empty($_GET)) {
			$activity_log['get_var'] = serialize($_GET);
		}

		if(defined('USE_MYSQLI') && USE_MYSQLI==true) {
			global $mysqli;
			$query_count = count($mysqli->query_log);

			$activity_log['query_log'] = serialize($mysqli->query_log);

			$mysqli->set('date_added', 'NOW()', false);
			$mysqli->insert('activity_log', $activity_log);

			$mysqli->close();
		}else{
			$activity_log = array_map('mysql_real_escape_string', $activity_log);
			$activity_log['date_added'] = 'NOW()';
			$sql = 'INSERT INTO `activity_log` (`' . implode('`,`',
array_keys($activity_log)) . '`) VALUES ("' . implode('","',
$activity_log) . '")';
			mysql_query($sql);

			mysql_close();
		}

		exit;
	}
/* /Snippet */
?>

--
Jonathan Langevin
PHP Site Solutions
http://www.phpsitesolutions.com



On Tue, Nov 25, 2008 at 3:02 PM, Fergus Gibson <fgibson75@xxxxxxxxx> wrote:
> On Tue, Nov 25, 2008 at 6:31 AM, Jonathan Langevin
> <jon@xxxxxxxxxxxxxxxxxxxx> wrote:
>> The problem is, this activity log, when enabled, results in all MySQL
>> connections being used, and we run out of open connections. I'm not
>> sure where the error is.
>>
>> Our config.php, when included, initializes the MySQL or MySQLi
>> connection as decided by the script that included the config.
> [...]
>
> Hi, Jon.  It's difficult to offer specific replies without specific
> code, but I did raise my eyebrow at the notion of mixing the mysql and
> mysqli extensions in your application.  I'm guessing you've inherited
> some pretty messy code that is time-consuming to refactor.
>
> Is any of the code working against the mysql extension calling
> mysql_pconnect() to create a persistent database connection?
> Connections so opened CANNOT be closed using mysql_close().
>
> I have never written an application using persistent connections, but
> I did get hired at a company where the other programmers describe the
> same problem you're having (a proliferation of connections that
> overwhelmed the server).  They blamed mysql_pconnect() and the lead
> programmer said that after he banned the use of that function, the
> problem went away completely.  A comment to the PHP documentation
> suggests this is the result of a bad interaction between Apache and
> PHP.
>
>
>> Within the same file, a shutdown function is registered to
>> automatically close the MySQL or MySQLi connection at the end of
>> script execution (based on which connection was initialized to begin
>> with).
>
> This should not be necessary.  The script will close all open
> resources when it terminates anyway.  The reason for having and using
> close functions is to free resources while the script is still
> running.  As mentioned above, persistent connections created by
> mysql_pconnect() cannot be closed by mysql_close() under any
> circumstances, nor will they automatically close on completion of the
> script.  I believe that's your problem.
>

-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux