[Dbix-class] multiple database connection

Rippl, Steve rippls at woodlandschools.org
Fri Oct 21 15:37:12 GMT 2011


On Fri, Oct 21, 2011 at 7:52 AM, Roland Philibert <rphilibert at aptina.com>wr=
ote:

>
> Hi all,
> Could anybody send me an example on how to connect multiple databases
> using DBIC::Schema?
>
> I have:
>
> package MyApp::Model::DB;
>
> use strict;
> use base 'Catalyst::Model::DBIC::Schema';
>
> __PACKAGE__->config(
>    schema_class =3D> 'MyApp::Schema',
>
>    connect_info =3D> {
>        dsn =3D> 'dbi:mysql:database=3Ddb1:host=3Dhost1',
>        user =3D> 'root',
>        password =3D> 'root',
>    }
> );
>
> And I need to connect to another mysql db (db2) under a different host
> (host2)
>
>
[assuming catalyst app]

Create a second model  MyApp::Model::DB2 with connection info for the second
db.  I autocreate them using...

# create/update model for netadmin db
script/netadmin_create.pl model *DB* DBIC::Schema NetAdmin::Schema
create=3Dstatic components=3DTimeStamp
'dbi:Pg:dbname=3Dnetadmin;host=3Dhostname;port=3D5432' 'username' 'password=
' '{
AutoCommit =3D> 1, quote_char =3D> q{"}, }'

# create/update model for radius db
script/netadmin_create.pl model *RadiusDB* DBIC::Schema
NetAdmin::RadiusSchema create=3Dstatic components=3DTimeStamp
'dbi:Pg:dbname=3Dradius;host=3Dhostname2' 'username2' 'password2' '{ AutoCo=
mmit
=3D> 1 }'

Then I use one connection using $c->model('*DB*::Table')->...  and the other
using $c->model('*RadiusDB*::OtherTable')->...

Don't know if this is the best way, but it's what I use?!!



-- =

Steve Rippl
Technology Director
Woodland Public Schools
360 841 2730
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.scsys.co.uk/pipermail/dbix-class/attachments/20111021/eda=
c76bf/attachment.htm


More information about the DBIx-Class mailing list