Post by JNeuhoff » Wed Jan 23, 2008 4:21 am

I frequently got a
Warning: mysql_connect() [function.mysql-connect]: Too many connections in /home/vcsprodu/public_html/library/database/database.php on line 16
error message on my new website running OpenCart 0.7.7.

Am I correct in my understanding that OpenCart calls the mysql_connect function for each new request? Because for each request it calls new Locator() which in turn calls createDatabase which calls the mysql_connect.  Wouldn't it be more useful to have mysql_connect only be called on a per session-basis instead?

Export/Import Tool * SpamBot Buster * Unused Images Manager * Instant Option Price Calculator * Number Option * Google Tag Manager * Survey Plus * OpenTwig


User avatar
Guru Member

Posts

Joined
Wed Dec 05, 2007 3:38 am


Post by JNeuhoff » Wed Jan 23, 2008 5:05 am

Also, there doesn't seem to be any calls to the mysql_close function, again causing a rapid increase of new connections for each new request.

Export/Import Tool * SpamBot Buster * Unused Images Manager * Instant Option Price Calculator * Number Option * Google Tag Manager * Survey Plus * OpenTwig


User avatar
Guru Member

Posts

Joined
Wed Dec 05, 2007 3:38 am


Post by bruce » Tue Jan 29, 2008 3:25 pm

What you have observed, is probably not the source of your problem.

Have a look at the php documentation for mysql_connect and you will see that the connection is reused if the same parameters are passed which in this case, they are. Also look at the mysql_close documentation to see that it is not necessary.

For a test, I gave the code a bit of a thrashing, explicitly calling connect, as it does not get called if the $database object already exists. Note that MAX_CONNECTIONS in the MySql database used is 100 and I attempted 1000 connections.

Code: Select all

	for ($i=0; $i < 1000; $i++)
        {
	    $database =& $this->locator->get('database');
            $database->connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
            echo $i . ' ' ;
        }
I saw 1000 numbers on the screen and executing SHOW PROCESSLIST using phpmyadmin shows only one connection. I did the same with someone else connecting from another computer to the same url and there is still only one connection.

If there are holes in what I have done here, I would appreciate any comments  :)

Active Member

Posts

Joined
Wed Dec 12, 2007 2:26 pm
Who is online

Users browsing this forum: No registered users and 7 guests