[Dbix-class] Setting up database connection at run-time
Matt S Trout
dbix-class at trout.me.uk
Thu Jan 17 03:07:41 GMT 2008
On Mon, Jan 14, 2008 at 10:18:29AM -0500, Juan Camacho wrote:
> 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;
> }
That's horrible. That means if you somehow manage to bypass the context
check or hang onto a reference to the $schema, you're going to hit a random
client's database.
And it doesn't even gain you anything, except maybe an award for 'stupidest
piece of premature optimisation I've seen so far this week'.
--
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 DBIx-Class
mailing list