[Israel.pm] Class::DBI with several databases from the same script
Gabor Szabo
gabor at perl.org.il
Wed Jun 2 21:01:06 PDT 2004
In version 0.96 of Class::DBI it can be done already, just the
documentation is a bit lacking.
Anyway here is my code.
Any corrections, ideas are welcome.
package My::DBI;
use base 'Class::DBI';
use DBI;
my %dbh;
my $current_dbh;
sub myinit {
my ($dsn) = @_;
if ($dbh{$dsn}) {
$current_dbh = $dbh{$dsn};
} else {
$current_dbh = $dbh{$dsn} = DBI->connect_cached($dsn, $username, $password,
{
# defaults of Class::DBI
RaiseError => 1,
AutoCommit => 0,
PrintError => 0,
Taint => 1,
RootClass => "DBIx::ContextualFetch",
});
if (not $current_dbh) {
warn "Could not connect to '$dsn' $DBI::errstr";
return 0;
}
}
return 1;
}
sub db_Main {
my $self = shift;
return $current_dbh;
}
1;
# and in the nearby script:
My::DBI::myinit($dsn_a);
My::DBI->create(....)
My::DBI::myinit($dsn_b);
My::DBI->create(....)
More information about the Perl
mailing list