[cgiapp] Re: is my app caching db connection ?

Dan Horne dan.horne at redbone.co.nz
Mon Jul 28 14:53:07 EDT 2008


On 29/07/2008, at 04:58 AM, Michael Peters wrote:

> Mark Stosberg wrote:
>
>>> the different process_id (2461, 2780) means they are two different
>>> connections ?
>> I think that's right. I'm not a FastCGI user, but it's my  
>> understanding that you would need a
>> variable to be at least package-scoped for it to be share.
>> If I'm right, then something like this would stay shared:
>> our $dbh = DBI->connect(...);
>> $self->dbh_config($dbh);
>
> But don't do that. It's just asking for problems. DB connections  
> *will* go stale. If that happens your application will stop working  
> with strange errors and only a restart will fix it. This is the  
> problem that Apache::DBI solves for mod_perl. If you want to learn  
> how to do this on your own, then I suggest looking in there for some  
> pointers. But I will warn you that Apache::DBI doesn't do the magic  
> on it's own. DBI knows about Apache::DBI. So any code you write  
> yourself won't have that advantage.

I believe the way around this is to use the package variable approach  
Mark suggests with a C::A::P::DBH callback. DBI's connect_cached  
option, is designed to handle the stale connection problem (I use DBI  
directly, but the principle is the same):

     # Use a callback to create your owh handle that is still lazy  
loaded
     $self->dbh_config(sub { DBI->connect_cached(); });




More information about the cgiapp mailing list