Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Re^4: Change DBI database on runtime

by miguelele (Beadle)
on Nov 15, 2011 at 00:11 UTC ( #938053=note: print w/replies, xml ) Need Help??

in reply to Re^3: Change DBI database on runtime
in thread Change DBI database on runtime

Obviously, there is something big that I do not understand in the Adaptor/Factory/Factory::PerRequest question.
- I have the working db class posted before, called 'DB'
- Now I wrap it from this new model called 'DBWrapper'. And I call this one from the Controller.
- I need to configure the __PACKAGE__ to avoid compile errors from DBI. Configuration data must be present. I do not understand it, because this info should be replaced from the info coming from the request.
- The ACCEPT_CONTEXT sub really executes. I see the required info printed in the log (THEALIAS comes from Fastcgi/Nginx custom variable)

In the end it always query the db named "database_one" configured here, but it ignores the dynamic one from ACCEPT_CONTEXT. I tried also static values, but obviously there is something that I am doing very bad

Really lost.

package the_application::Model::DBWrapper; use strict; use warnings; use parent 'Catalyst::Model::Factory::PerRequest'; __PACKAGE__->config( class => 'PWC::Model::DB', connect_info => { dsn => '', user => 'the_user', password => 'the_password', AutoCommit => q{1}, } ); use Moose; use namespace::autoclean; sub ACCEPT_CONTEXT { my($self, $c) = @_; # Current db name comes from fastcgi enviromental variable from Ng +inx my $current_db = $c->engine->env->{THEALIAS}; $c->log->debug('*** Reached ACCEPT_CONTEXT current db = ' . $curre +nt_db); my $new = $self->meta->clone_object($self, connect_info => { dsn => 'dbi:mysql:' . $current_db . '', user => 'the_user', password => 'the_password', AutoCommit => q{1}, } ); return $new; } 1;

Replies are listed 'Best First'.
Re^5: Change DBI database on runtime
by zwon (Abbot) on Nov 15, 2011 at 02:43 UTC
    clone_object method creates exact copy of your model object and replaces attribute with connection parameters, but at this time connection already established, so these parameters are not used. Instead of cloning you should create new object using connect method.

      Great!, now it works perfectly , but it is not like the docs on Factory::PerRequest. I can even comment out the '#use base 'Catalyst::Model::Factory::PerRequest'; line and it keeps working. Isn't it strange?

      This is what I understand reading my code:
      - The basic package config has empty values. But they must be present for the first load of the app, so Catalyst know what it is.
      - The sub ACCEPT_CONTEXT return a new object from PWC::Schema every time this model is requested.
      - So I am not instantiating anything else but this very class, with just one contextual variable that changes from each request.

      Am I right? Is it safe?

      package PWC::Model::MYDB; use strict; use warnings; use base 'Catalyst::Model::DBIC::Schema'; #use base 'Catalyst::Model::Factory::PerRequest'; __PACKAGE__->config( schema_class => 'PWC::Schema', connect_info => { dsn => '', user => '', password => '', AutoCommit => q{1}, } ); sub ACCEPT_CONTEXT { my($self, $c) = @_; my $db = $c->engine->env->{THEALIAS}; #From environment variable i +n Fastcgi/Nginx my $new = $self->meta->new_object( schema_class => 'PWC::Schema', connect_info => { dsn => 'dbi:mysql:' . $db . '', user => 'the_user', password => 'the_password', AutoCommit => q{1}, } ); return $new; } 1;

        Yes, you're right, I think Catalyst::Model::Factory::PerRequest is not supposed to be extended by replacing ACCEPT_CONTEXT method, it's more like it should extend your class. I recommend you reuse caching from it though, note that it doesn't build new object for the same context twice. Otherwise I think everything is correct.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://938053]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (4)
As of 2018-05-22 21:31 GMT
Find Nodes?
    Voting Booth?