[Dancer-users] Using persistent DB connections in Dancer::Tutorial
jwm at emptech.com
Wed Jan 12 01:17:18 CET 2011
On Jan 10, 2011, at 4:53 AM, David Precious wrote:
> A single connection per process, I believe. For instance, if you run it
N.B. I can't speak for Starman or other deployments as I have no experience. But for straight mod_perl & DBI:
Certainly a single connection per process is the right model under a straight mod_perl / DBI model.
The idiom I use is:
our $dbh; # DBI
In every request:
$dbh = db_connect() unless $dbh;
where db_connect() does the usual DBI->connect() and returns the handle.
If you try to reuse the DBI handle across fork(), then DBI itself will start throwing errors,
subject to DBI's RaiseError / PrintError configuration.
In other words, allocate a global handle for the DBI connection, and initialize it by opening it.
Don't fork and reuse the global handle without re-opening it in the child.
The only way to get into trouble is the DBI handle gets initialized before the child forks.
This means if I need to read state from the DB that I want inherited by all children,
then in the function called at httpd init time, I open the db, read the state into ram,
then close $dbh and undef it.
You also may find this DBI & mod_perl info helpful as the DBI persistence issue is pretty well covered:
More information about the Dancer-users