[Catalyst] Connect DBIx::Class model on startup?

Tomas Doran bobtfish at bobtfish.net
Sat Feb 25 08:39:14 GMT 2012

> Op 21-02-12 19:30, Daniel J. Luke schreef:
>> Is there a canonical (or recommended) way to have my new fastcgi processes' model(s) connect to their databases on startup (instead of during the first request?)
>> I'd like to be able to avoid the extra delay on the first request each fastcgi process has while it's connecting to the DB. I think it probably belongs in my Catalyst::Model::DBIC::Schema class (maybe after BUILD ?)
>> Or maybe there's a reason why there's not an obvious hook and someone can point me to the pitfalls I'm not seeing.

It's not totally obvious as you can't do this at process startup (as you then fork!), so it has to be done in the FCGI process manager really (which does the forking)…

You can subclass FCGI::ProcManager and implement:

sub handling_init {
    my $self = shift;
    MyApp->model('DB')->schema->dbh->ping; # Check we have a DB connection that's working straight after forking but before starting to handle requests.

You can then pass your fastcgi.pl script the option to use your custom process manager, and you're sorted.


More information about the Catalyst mailing list