Hello everyone.
First post here, sorry if this has already been covered elsewhere. I'd
be happy to be redirected to other references, but I couldn't find the
answer in the usual places.
Anyway, the question is simple:
I have an $sth being passed to a function. I want to get access to its
$dbh, from within that function, but the $dbh isn't passed as a
parameter.
How can I do that?
The reason why I'm asking is that I'm working on a
load-balancer/redirector from within mod_perl and, in order not to
change any of my code, I have to overload the standard connect, prepare
and execute methods.
The problem with execute is that I only get access to the $sth, and I
need to have the $dbh to recreate the connect to another server in case
the execute fails.
The following sample code traps for a connect to dev_server, kills it
and recreates one on prod_server.
My workaround to the problem stated above was to use a global var
called $OVERLOAD::dbh to hold the last accessed $dbh, within the
prepare statement. That obviously sucks. Any ideas? Thanks!
sub execute {
my $sth = shift;
my $dbname = $sth->{Database}->{Name};
$dbname =~ /server=([\w]+)/;
$dbname = $1;
my $dbh = $OVERLOAD::dbh;
if ($dbname =~ /dev_server/) {
my $sql = $DBI::lasth->{Statement};
$sth->finish;
$conn_str = join ('',
'dbi:', 'Sybase',
':database=xxx',
';server=prod_server',
);
$dbh = DBI->connect ($conn_str, 'user','pass',
{
PrintError => 0,
RaiseError => 0,
ChopBlanks => 1,
AutoCommit => 1
}) or die "Can't connect: $DBI::errstr";
$sth = $dbh->prepare($sql);
}
my $res = &DBI::st::execute ($sth);
return $res;
}
Henri Asseily
[EMAIL PROTECTED]