[Dbix-class] Setting up database connection at run-time

Juan Camacho jc5826 at gmail.com
Mon Jan 14 15:18:29 GMT 2008


On Jan 13, 2008 8:03 PM, Matt S Trout <dbix-class at trout.me.uk> wrote:
> On the Catalyst side use Catalyst::Component::InstancePerContext and
> something approximating
>
> my $new = $self->new(%$self);
> $new->schema($self->schema->connect(<get connect info from $c>);
>
> I do something similar quite a lot except that rather than changing the
> DB connection I'm calling restrict_with_object (provided by
> DBIx::Class::Schema::RestrictWithObject) to impose a security context.
> Same basic principle though.
>

Thanks Matt for the tip. The InstancePerContext module seems to do the
trick. Do you see any danger in not creating a new schema each time,
but simply re-using the existing object and changing the connection if
the database is different?  Something like:

sub build_per_context_instance {
   my ( $self, $c ) = @_;
   if ( $self->{_database} ne $c->session->{database} ) {
       $self->schema->connection( ... );
       $self->{_database} = $c->session->{database};
   }
   return $self;
}

Are you using multiple databases with RestrictWithObject? Not clear
from the documentation, how I could use it in my situation. Good
module to know regardless.

Thanks, Juan



More information about the DBIx-Class mailing list