http://qs321.pair.com?node_id=912595

Uragan has asked for the wisdom of the Perl Monks concerning the following question:

Hi all! I use Catalyst and DBI. I want to connect my database only in one place. But now every model has it's own connection and I must write in config file params(dsn, username, password) for every model, like this:

<Model::Name_of_model> dsn DBI:mysql:name_of_database user username password user_password </Model::Name_of_Model>

I must do it for every model. Is it possible to do it only once and in one place? And that each model would take these params.

Replies are listed 'Best First'.
Re: Catalyst and DBI
by Anonymous Monk on Jul 04, 2011 at 06:56 UTC
Re: Catalyst and DBI
by trwww (Priest) on Jul 04, 2011 at 18:58 UTC

    How did you create your model? I ask because if you're having to add in connection info for each model class then you're doing it wrong. You only need to set the database configuration in one place. See:

    Catalyst::Model::DBIC::Schema

      I don't use DBIx. I use Catalyst::Model::DBI. When I create a model, I write next:

      ./app_create.pl model Name_of_Model DBI:mysql:auth username password

      In this case dsn, username and password pass into the model configuration. So each model has it's own configuration (the same). Without this configuration model can not connect to database. I use DBI, because I must write live queries without any ORM. I now thar it's wrong write configuration in every model and when every model connects to DB separately, but I can not understand, how I can do only one connection and write connection params in one place(((

        I use DBI, because I must write live queries without any ORM... But now every model has it's own connection...

        Do you need more than one DBI model per database, and if so, why?

        --Solo

        --
        You said you wanted to be around when I made a mistake; well, this could be it, sweetheart.
Re: Catalyst and DBI
by sundialsvc4 (Abbot) on Jul 04, 2011 at 12:50 UTC

    As an aside, not directly related to Catalyst but worth bringing up anyway, I recently “discovered” DBIx::Connector ...

    DBIx::Connector provides a simple interface for fast and safe DBI connection and transaction management.   Connecting to a database can be expensive; you don't want your application to re-connect every time you need to run a query.   The efficient thing to do is to hang on to a database handle to maintain a connection to the database in order to minimize that overhead.   DBIx::Connector lets you do that without having to worry about dropped or corrupted connections.”

    It made a very dramatic speed difference to the kludgy old app that I am busy rehabilitating, and stood up quite well to initial stress-tests.