I don't know if the Sybase driver supports it, but flaky servers suggest the DBI $dbh->ping method. You can open a handle to each server, say $dbx and $dby, and use the trinary op to select
my $sth = ($dbx->ping ? $dbx : $dby)->prepare('select 1+1');
and so on. That will get awkward fast.
Another approach is to first try to connect to x. If that fails, connect to y. That isn't what you want for a long running process, but for a one-shot script it is probably ok (you wanted tolerant, not immune, right?)
my $dbh = connect($x_string, $x_nam, $x_pass)
|| connect($y_string, $y_nam, $y_pass)
or die 'No db!';
In any case, you should be meticulous about error checking with each DBI call.
| [reply] [Watch: Dir/Any] [d/l] [select] |
You're going to want to write a DBI wrapper. I have something like this in every project I work on. Something like:
package DBI_Wrapper;
sub new
{
my $class = shift;
my $self = bless {
dbh => [],
active => 0,
}, $class;
}
sub add_dbhs
{
my $self = shift;
push @{$self->{dbh}}, @_;
}
sub AUTOLOAD
{
my $meth = our $AUTOLOAD;
(my $func = $meth) =~ s/.*:://;
my $self = shift;
my $start = $self->{active};
my $dbh;
while (1)
{
$dbh = $self->{dbh}->[$self->{active}];
eval { $dbh->ping };
last unless $@;
$self->{active}++;
$self->{active} %= @{$self->{dbh}};
die "No valid DBH found\n" if $self->{active} == $start;
}
return $dbh->$meth( @_ );
}
Or, something like that. :-)
------
We are the carpenters and bricklayers of the Information Age.
Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose
I shouldn't have to say this, but any code, unless otherwise stated, is untested
| [reply] [Watch: Dir/Any] [d/l] |
I wrote this module to do connect to n databases (of the same type) at the same time. You'll just have to change the connect string/driver. | [reply] [Watch: Dir/Any] |
You may want to look at three modules from CPAN which address DBI failover: DBD::Multiplex, DBIx::DBCluster, and DBIx::HA.
You'll need to spend some time to evaluate each one to see if it addresses your purposes, but depending on your requirements, this may be much easier than writing your own from scratch. | [reply] [Watch: Dir/Any] |
After looking at the modules suggested by simonm I would recommend using DBIx::HA - it appears to map quite well to your requirements.
Michael
| [reply] [Watch: Dir/Any] |