John Smith
John Smith

Reputation: 91

MySQL connection limit

I have a problem with reaching my connection limit too quickly... Am I right in thinking the following will help resolve this?

On older files using mysql_query

<?php
mysql_close($link);
if (isset($link2)) {
    mysql_close($link2);
}
?>

On newer files using mysqli class

class DB extends MySQLi {
  function __destruct() {
    $this->close();
  }
}

Upvotes: 0

Views: 898

Answers (3)

symcbean
symcbean

Reputation: 48357

<?php
 mysql_close($link);
 if (isset($link2)) {
     mysql_close($link2);
 }
 ?>

This doesn't make any sense - if know that both variables may contain mysql connection resources then close the both!

Better yet - if your code is a mess and you can't make sense of it, then...

<?php
@mysql_close();
@mysql_close();
@mysql_close();
?>

But the only place you can sensibly put this (without analysing the code behaviour in some details - in which case you would know what resources you have open) is at the end of the script - which is where the connections get closed anyway.

Similarly, the destruct method is only called when all references to an object are deleted - this is marginally better but depending on the code structure you may get no benefit at all.

It makes far more sense to identify which URLs are taking a long time to process and trying to re-factor the code (both PHP and SQL) in these.

Upvotes: 0

Rob Bailey
Rob Bailey

Reputation: 1787

You may also be keeping connections open via persistent connections (pconnect), causing your database server to pool and stack up the connections. I've had troubles with this up until about PHP5.2?

Upvotes: 1

Silver Light
Silver Light

Reputation: 45912

Connection is closed automatically when script finishes it's work even if you forget to mysql_close(). Consider raising max_clients setting my.cnf

Also, if you are using only one database, you wont need, you don't need two connections - use one instead.

Upvotes: 0

Related Questions