[Dbix-class] Apache::DBI and DBIx::Class

Mark Hedges hedges at ucsd.edu
Wed Apr 5 20:08:50 CEST 2006



On Wed, 5 Apr 2006, Brandon Black wrote:
> 
> Yes.  Basically, what Apache::DBI does is enable a hook in DBI itself
> to cache connections based on connect info.  If you call ->connect
> twice with the same arguments, and the old connection is still up, it
> will re-use that connection.  The idea (AFAIK) was to help converting
> legacy CGI applications which might have been designed to ->connect to
> the database on every request, into mod_perl applications that keep
> their connection persistent.  In an application designed for mod_perl
> (or other persistent) usage that knows to only connect once at process
> startup, Apache::DBI doesn't help you.

I've always found in a handler that you have to "connect" at
the beginning of every request, because sometimes the child
sits around until the database times out the connection.  
(Which is a good thing, unless you have unlimited DB memory.)

Calling DBI->connect is the best way to get Apache::DBI to
ping the database handle and reconnect if it went away.
Apache::DBI by default does a $dbh->ping first on connect (you 
can change the timing behavior.)  Then it reconnects if the 
ping failed.

So any good mod_perl application should make use of Apache::DBI
and "connect" on every request.  If it's busy, a ping per request
is a lot less overhead than a connect.  If it's not busy, and
you don't use Apache::DBI and connect every request, the handler 
will eventually try operations on a stale handle, which is bad.

If DBIC were o.k. with Apache::DBI, all that would be needed
would be a class method to cause DBIC to do a DBI->connect.
This method should be called at the start of a mod_perl handler 
request.  Then let Apache::DBI ping the connection, and it will
go connect it again if it's stale.

It makes sense that if DBIC isolates itself from Apache::DBI, 
that if any part of the process connects to the database in some 
other way (which my AuthenHandler does to be quick about it), 
that would result in twice the number of connections needed.

But since Apache::DBI is useful, why not let DBIC ignore it and 
let it do its job?  The responsibility would be on the mod_perl 
coder to know that the connection handle would be shared, and 
transactions as such would be touchy.  (But if you need that 
kind of speed, you're probably using MyISAM anyway.)

Mark



More information about the Dbix-class mailing list