nerique nerique Wed Jun 15 12:44:23 -0400 2011

Subject: php-activerecord using too many ressoucres


i'm currently trying to rewirte some pieces of code written by other developers, and it appears I often reach the amount of connections allowed on my server.

is there a way to limit the number of connections php-activerecord creates? or close the one it's using?


Daniel Lowrey Thu Jun 16 03:03:19 -0400 2011

For one thing, php-ar uses PDO connection objects and those have no "close()" method ... so there's not an easy way to kill a connection.

I'm not sure how a number of connections would become a problem though -- the ConnectionManager class uses the singleton pattern to avoid opening multiple connections to the same resource.

If the number of connections are maxing out I'd guess that the problem is poor programming, a poor hosting service or a combination of the two. If php-ar is used correctly it would be very difficult to max out those connections without a number of very long-running scripts working simultaneously.

ziyaaf gaffoor Wed Nov 30 11:43:25 -0500 2011


I have developed a multilingual website. I'm storing the translations in the database. I have a function to get the translation based on the key given. For example :

function get_translation($key,$language){
// Get Thai translation from database for the given $key

If im getting translation for 50 words. Will the DB connection called 50 times ?
Because i came across the error 'too many connection'.

Any best practices to avoid "too many connection" error ?


Daniel Lowrey Wed Nov 30 14:11:35 -0500 2011

How are you initializing the ActiveRecord Config? Is it like the following?

 1 // Specify available connections
 2 $conns = array(
 3   'development'  => 'mysql://user:pass@',
 4   'production'  => 'mysql://user:pass@'
 5 );
 7 // Initialize ActiveRecord configuration
 8 ActiveRecord\Config::initialize(function($c) use ($conns) {
 9   $c->set_model_directory(MODELS);
10   $c->set_connections($conns);
11   $c->set_default_connection('development');
12 });

Because when you do it this way the connections are stored in the Singleton ConnectionManager object where they'll only be established once. Then, when a model needs a connection it checks to see if a connection of the same name has been already been created in the ConnectionManager. If so, it reuses the existing connection and won't establish a new one.

This may be slightly off topic but you also have the capability of specifying which connection specific models use by setting the static $connection property inside a model. So, for example:

1 // This model uses the production server instead of the default
2 class MyModel extends ActiveRecord\Model
3 {
4   static $connection = 'production';
5 }
ziyaaf gaffoor Thu Dec 01 00:07:47 -0500 2011

Thanks for the reply .

My init methods is :

$cfg->set_connections(array('development' => 'mysql://root:@'));

Does this method has any issues ? My site has not gone live yet , so maximum 10-20 people may be using it for testing. But still i got the 'too many connection' error . Any idea whats causing it ?
(default max connections is 101)