Re: Runtime Database connections- one more time
[prev]
[thread]
[next]
[Date index for 2005/01/23]
I have been using Class::DBI for some time now and it has been kind of
disturbing not to get a real foundation for how to treat the case when I
need to use separate databases for my clients though they are using the
same application.
I am using Class::DBI in a Apache-mod_perl-HTML::Mason
environment(mod_perl 1). Nothing else.
My current configuration:
a mod_perl authentication module handle user login and puts the relevant
information in $r->pnotes('ticket')
my HTML::Mason module calls Module::DBI containing:
/*---------------------------------------------------------------
package Module::DBI;
use 5.006;
use strict;
use warnings;
use base 'Class::DBI';
our $VERSION = '0.01';
sub db_Main {
DBI->connect_cached(shift->get_user_connection);
}
sub get_user_connection {
my $class=shift;
my $r = $HTML::Mason::Commands::r;
if ($r) {
my $ticket=$r->pnotes('ticket') || $r->prev->pnotes('ticket');
if ($ticket) {
my $company = $ticket->{company};
my $dbuser = $ticket->{dbuser} || $company;
my $pw = $ticket->{pw}||'';
my $dbprefix = $r->dir_config("dbprefix") || '';
my $database = $dbprefix . $company;
my $db = "DBI:mysql:database=" . $database;
$db.= ";host=" . (($r->dir_config("dbhost")) ?
$r->dir_config("dbhost") :
"localhost");
$db.= ";port=" . $r->dir_config("dbport") if
$r->dir_config("dbport");
return(
$db,
$dbuser,
$pw,
{AutoCommit => 1, RaiseError => 1, RootClass =>
"DBIx::ContextualFetch"}
);
} else {
#
$class->_croak('Wow, no ticket in request object!');
}
} else {
$class->_croak('Wow, no request object!');
}
}
---------------------------------------------------------------*/
Is this the optimal way?
Many clients = many sleeping database processes?
/Bosse
Perrin Harkins wrote:
> Daniel Collis Puro wrote:
>
>> It works from the command line, and feels MUCH cleaner than using
>> "our()" . .
>
>
> It's identical to your previous approach, except that you are now
> re-defining a method on every single request. That looks much less
> clean to me.
>
>> any mod_perl concerns?
>
>
> You will probably cause more memory to be unshared by constantly
> re-defining this method.
>
> I still don't see why you need any of this. Why can't you make your
> Class::DBI subclasses get their connection info from the same place as
> your other classes? If the connection parameters are identical, it will
> reuse an open connection.
>
> - Perrin
--
Bo Strinnholm
www.payroll.se
Tel: +46 (0)586 725 724, +46 (0)730 639 573