in reply to Are connections automatically pooled when using mod_perl?

In a mod_perl environment, your variables (like database handles) can live longer than a single request if you declare them in a scope "outside" of the request.

For example:

package MyHandler; use strict; use warnings 'all'; use Apache2::RequestRec; use Apache2::RequestUtil; # Declare it here: my $persistent_thing; sub handler : method { my ($class, $r) = @_; # The first time this is executed in a child process, it will be cre +ated. # After that, you will just reuse the same one: $persistent_thing ||= make_the_thing(); } 1;

Please also check out Ima::DBI::Contextual as it solves the whole thing.

package MyDBI; use base 'Ima::DBI::Contextual'; my @dsn = ( 'DBI:mysql:dbname:hostname', 'username', 'password', { # Other options go here: RaiseError => 1, }); __PACKAGE__->set_db('Main', @dsn); 1;

Then, elsewhere:

use MyDBI; # Automatically pooled and recreated if the old one times out or dies: my $dbh = MyDBI->db_Main;

Replies are listed 'Best First'.
Re^2: Are connections automatically pooled when using mod_perl?
by Anonymous Monk on Dec 20, 2011 at 07:14 UTC
    FYI, FWIW, AFAIK, technically, neither Apache::DBI nor Ima::DBI::Contextual provide an actual pool , they merely provide a cache. The Apache::DBI docs explain this explicitly and in detail. The Ima::DBI::Contextual are more funny than informative :)

      The Ima::DBI::Contextual (docs) are more funny than informative :)

      You're right. That should probably be fixed.