[Catalyst] One App, multiple databases

Jose Luis Martinez jlmartinez-lists-catalyst at capside.com
Wed Nov 19 17:54:01 GMT 2008


Hello,

This question has been asked a couple of times on the list, and I have 
found yet another solution to it, but I would like to hear if maybe I'm 
doing something wrong, or I will suffer serious pain by doing it my way :)

We have an app that will connect to one database or another depending on 
the logged in user.

My solution:

package App::Model::DB;

use strict;
use base 'Catalyst::Model::DBIC::Schema';
...
sub ACCEPT_CONTEXT {
     my ($self, $c) = @_;

     my $user_db = $c->lookup_the_users_db();
     $self->config->{'connect_info'}->[0] =~ s/#DATABASE#/$user_db/;
     $self->schema->connection(@{$self->config->{'connect_info'}});

     return $self;
}


1;

I've done the same with Catalyst::Model::DBI:

package App::Model::AnotherDB;

use strict;
use base 'Catalyst::Model::DBI';

sub ACCEPT_CONTEXT {
     my ($self, $c) = @_;

     my $user_db = $c->lookup_the_users_db();
     $self->{'dsn'} =~ s/#DATABASE#/$user_db/;

     return $self;
}

1;

And the two seem to be working OK, but I'm worried about what will 
happen when we fire it up in a FastCGI environment (I suspect the 
connection to  user1's database will be kept live, and the next user 
will get the connection to it). Am I right? Any pointers?

Does this way of using the models trigger any warning lights to Catalyst 
gurus?

Thanks in advance,

Jose Luis Martinez
jlmartinez at capside.com



More information about the Catalyst mailing list