Re: multiple database users

[prev] [thread] [next] [Date index for 2005/06/06]

From: William Ross
Subject: Re: multiple database users
Date: 13:46 on 06 Jun 2005
On 6 Jun 2005, at 12:51, Hartmaier Alexander wrote:

> I like Scott's approach at
> http://wiki.class-dbi.com/index.cgi?UsingMultipleDatabases
> What I don't get is how to set which of the defined db connections =20
> to use:
>
> #snip#
> sub db_Main {
>    my $connection_key =3D "TESTDB_B";
> #snip#
>
> The used db connection is set again in db_Main and not when loading =20=

> the CDBI
> module...

so change it :)

all these multiple-database solutions are very simple: you keep a =20
hash of database handles in memory - Class::DBI::Factory has been =20
doing that for years - normally with an environment variable as the =20
key. when a request comes in you look up that environment variable, =20
and return the corresponding handle. If it doesn't exist yet, you =20
create it and store it for next time. That's what this does:

>      return $handles->{ $ENV{USER} } ||=3D DBI->connect_cached(
>          $dsn, $db_user, $db_pass, $db_options
>       );

I was assuming that in your case you would want to use $ENV{USER} as =20
the key.

will






>
> -Alex
>
>
> -----Urspr=FCngliche Nachricht-----
> Von: William Ross [mailto:will@xxxxxxx.xxx]
> Gesendet: Montag, 6. Juni 2005 13:40
> An: Hartmaier Alexander
> Cc: cdbi-talk@xxxxxx.xxxxx.xxx
> Betreff: Re: multiple database users
>
> On 6 Jun 2005, at 11:29, Hartmaier Alexander wrote:
>
>
>> And where can/should I override the default db_Main?
>> I have no idea how to do that...
>>
>
> It's just a Class::DBI method. The only confusing thing is that it
> doesn't exist until Ima::DBI places it into Class::DBI's namespace.
> You override it in the usual way, by putting a similar method in your
> base class.
>
> There's a popular example in Perrin's http://wiki.class-dbi.com/
> index.cgi?UsingWithModPerl that might help, or you could start with
> something simple like this (untested and possibly silly):
>
> my $handles =3D {};
> my $db_options =3D {
>      RaiseError         =3D> 1,
>      AutoCommit         =3D> 0,
>      FetchHashKeyName   =3D> 'NAME_lc',
>      ShowErrorStatement =3D> 1,
>      ChopBlanks         =3D> 1,
>      RootClass          =3D> 'DBIx::ContextualFetch'
> };
>
> sub db_Main {
>      my $dbh;
>      return $handles->{ $ENV{USER} } ||=3D DBI->connect_cached(
>          $dsn, $db_user, $db_pass, $db_options
>       );
> }
>
> ... where $dsn, $db_user and $db_pass come from your config and
> presumably depend on the user.
>
> with this in place, you can remove the set_db calls from your base
> class or data classes, as it will be ignored.
>
> will
>
>
>
>
>
>
>>
>> -Alex
>>
>>
>> -----Urspr=FCngliche Nachricht-----
>> Von: William Ross [mailto:will@xxxxxxx.xxx]
>> Gesendet: Freitag, 3. Juni 2005 10:43
>> An: Hartmaier Alexander
>> Cc: cdbi-talk@xxxxxx.xxxxx.xxx
>> Betreff: Re: multiple database users
>>
>> On 2 Jun 2005, at 18:43, Hartmaier Alexander wrote:
>>
>>
>>
>>> Hi list!
>>>
>>> I just found out that one of my scripts is using the wrong db user
>>> to access my database.
>>>
>>> I created different users with different access rights in oracle I
>>> used before CDBI.
>>>
>>> The connection info is stored in the base class so a different user
>>> means a different db connection regardless which parameter is
>>> different.
>>>
>>> This topic was already on the list but for different databases but
>>> I think it=92s the same problem
>>>
>>>
>> It is the same problem. If you want cdbi to use a different database
>> handle under different circumstances, you need a sub db_Main() in
>> your base class (or wherever) that selects the handle according to
>> your criteria. That sub needs to return an appropriate
>> DBIx::ContextualFetch object, btw, and should be as quick as possible
>> as it's called very frequently.
>>
>>
>> will
>>
>>
>
>

multiple database users
Hartmaier Alexander 17:43 on 02 Jun 2005

Re: multiple database users
Andreas Fromm 07:13 on 03 Jun 2005

Re: multiple database users
William Ross 08:42 on 03 Jun 2005

Re: multiple database users
Hartmaier Alexander 10:29 on 06 Jun 2005

Re: multiple database users
Mark Addison 11:20 on 06 Jun 2005

Re: multiple database users
William Ross 11:39 on 06 Jun 2005

Re: multiple database users
Tony Bowden 13:09 on 06 Jun 2005

Re: multiple database users
Mark Addison 13:34 on 06 Jun 2005

Re: multiple database users
Perrin Harkins 14:30 on 06 Jun 2005

Re: multiple database users
Hartmaier Alexander 11:31 on 06 Jun 2005

Re: multiple database users
Hartmaier Alexander 11:51 on 06 Jun 2005

Re: multiple database users
William Ross 13:46 on 06 Jun 2005

Re: multiple database users
Hartmaier Alexander 14:03 on 06 Jun 2005

Re: multiple database users
William Ross 15:30 on 06 Jun 2005

Re: multiple database users
Hartmaier Alexander 16:44 on 06 Jun 2005

Re: multiple database users
William Ross 18:26 on 06 Jun 2005

Re: multiple database users
Jay Strauss 02:53 on 07 Jun 2005

Re: multiple database users
Perrin Harkins 03:58 on 07 Jun 2005

Re: multiple database users
William Ross 10:23 on 07 Jun 2005

Re: multiple database users
Hartmaier Alexander 09:28 on 07 Jun 2005

Re: multiple database users
Perrin Harkins 12:49 on 07 Jun 2005

Re: multiple database users
Hartmaier Alexander 12:49 on 07 Jun 2005

Re: multiple database users
Perrin Harkins 12:54 on 07 Jun 2005

Re: multiple database users
Hartmaier Alexander 13:02 on 07 Jun 2005

Re: multiple database users
Mark Addison 13:47 on 07 Jun 2005

Generated at 16:35 on 28 Jul 2005 by mariachi v0.52