On May 10, 2006, at 1:26 PM, Jay Buffington wrote:
I want my modules to be able to get a the same database handle
anywhere they need it during a single request (if under mod_perl) or
the life time of the script they're in.
At first I was caching the database handle myself when running outside
of mod_perl. In mod_perl I was relying on Apache::DBI returning the
same handle every time I called connect(). That didn't work.
Then I read a section in Appendix A of "Practical mod_perl" that gave
me what I wanted: http://modperlbook.org/html/appa_12.html
But doesn't that solution have this problem: It will return a dead
database handle when the connection times out before the Child is
reaped. In fact, that's just the problem that Apache::DBI solved by
adding the ping() method.
I considered allocating a handle in the pnotes() at the beginning of
every request using a PerlInitHandler, but that is expensive,
especially since many requests don't even use a handle.
Jay
I use an abstraction layer
MyApp::DB
i call it to grab a DB handle, and it does magic stuff internally to
either give me a handle or die if it shouldn't - and it uses
Apache::DBI to handle the connecting
typer macdonald wrote this:
http://search.cpan.org/~crakrjack/DBIx-Transaction-0.008/lib/DBIx/
Transaction/db.pm
It does essentially the same thing, but nicer and without some of the
crazy stuff i have in my version ( mine turns subsequent connects
during an apache request into a nested transactions, which is scary
and dangerous yet fun )
I'd suggest using that. Just wrap it in its own package ( like a
poor mans singleton or factory ) and call that package to give you a
DB handle as needed.
| - - - - - - - - - - - - - - - - - - - -
| RoadSound.com / Indie-Rock.net
| Collaborative Online Management And Syndication Tools
| - - - - - - - - - - - - - - - - - - - -