[Catalyst] Multiple DB, single instance of Cat

Matt S Trout dbix-class at trout.me.uk
Sun Aug 17 20:08:01 BST 2008

On Fri, Aug 15, 2008 at 11:05:03AM +0100, Nigel Stuckey wrote:
> Another question about multiple databases.
> I have an existing application that uses a DBIx DB in the conventional  
> way: as a model initialised at run time. All great so far...

DBIx is the CPAN namespace for DBI extensions. There are hundres of
packages in there and you haven't told us which one of them you're using.

I'm going to assume you mean DBIx::Class but if you want to abbreviate
that please use 'DBIC' - it's really unfair to the authors of other
DBIx:: modules otherwise (compare to people saying "Linux 9" when they
actually mean "Red Hat Linux 9" or whatever :)

> However, I now need to make the application multi-organisation in the  
> same instance of catalyst: users will select their organisation or  
> repository when they login or it will be stored as a preference, etc.  
> Due to the size of the repository, this is implemented as multiple  
> databases. Thus a single running Catalyst instance will need to open  
> several DBs sharing the same schema, dynamically after run time. Then  
> it will have to close unused connections after a while.
> What is the best way to do this with DBIx and where is the best place  
> to store the handles/class references? A key to the active DB can be  
> stored in the session.

Something like the following (please write this up on the wiki once you've
got a working version, mine's typed straight into mutt and obviously
isn't -quite- complete :)

package MyApp::Model::DBIC;

use Moose;

extends 'Catalyst::Model::DBIC::Schema';

with 'Catalyst::Component::InstancePerContext';

sub build_per_context_instance {
  my ($self, $c) = @_;
  my $db_name = $c->session->{db_name};
  my $new = $self->new({ %$self });
  <do something here to make @connect_info from $db_name and config>
  return $new;


      Matt S Trout       Need help with your Catalyst or DBIx::Class project?
   Technical Director                    http://www.shadowcat.co.uk/catalyst/
 Shadowcat Systems Ltd.  Want a managed development or deployment platform?
http://chainsawblues.vox.com/            http://www.shadowcat.co.uk/servers/

More information about the Catalyst mailing list