Gossamer Forum
Home : General : Perl Programming :

eval and db failover

Quote Reply
eval and db failover
Hi. I put this perl code in my application in order to switch automatically to the backup server if the primary server is down.

I found that the backup ip has been used many times even, if the primary was up and running Crazy

Can you evaluate this and let me know your ideas and/or experience in similar situations?



Code:


eval {

$db='DBI:mysql:database='.$db_name.';hostname='.$db_host;

my $dbh = DBI->connect($db,$db_user, $db_pass);

$dbh->disconnect;

};

if ($@) {

$db_host ="192.168.1.21";


...
Quote Reply
Re: [robyone] eval and db failover In reply to
That should be ok. If you are using strict though, you may need to define the 'my' variables outside the eval { } part.

Other than that, it should work fine :)

Cheers

Andy (mod)
andy@ultranerds.co.uk


IMPORTANT: I've now moved to ultranerds.co.uk, and the .com will no longer work!
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package (plugins total "value" $3,325 & rising, for just $350)| GLinks ULTRA Package PRO (plugins total "value" $5,625 & rising, for just $500)
Support Forum | Links SQL Plugins | DMOZ Dumps | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Compare our different Plugin packages *new* Free CSS Templates
Quote Reply
Re: [Andy] eval and db failover In reply to
Hi. Thanks for your reply.

Do you mean .. not sure it works.

Why it is failing sometimes only? A delay on the connection?

Code:


my $db_host ="192.168.1.20";

eval {

$db='DBI:mysql:database='.$db_name.';hostname='.$db_host;

my $dbh = DBI->connect($db,$db_user, $db_pass);

$dbh->disconnect;

};

if ($@) {

my $db_host ="192.168.1.21";

...
Quote Reply
Re: [robyone] eval and db failover In reply to
Not sure. You could try;

Code:
my $db_host ="192.168.1.20";

my $db = 'DBI:mysql:database='.$db_name.';hostname='.$db_host;
my $backup_db = 'DBI:mysql:database='.$db_name.';hostname=192.168.1.21';

my $dbh = DBI->connect($db,$db_user, $db_pass) || DBI->connect($backup_db,$db_user, $db_pass);

$dbh->disconnect;

Untested, so it may not work.

Cheers

Andy (mod)
andy@ultranerds.co.uk


IMPORTANT: I've now moved to ultranerds.co.uk, and the .com will no longer work!
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package (plugins total "value" $3,325 & rising, for just $350)| GLinks ULTRA Package PRO (plugins total "value" $5,625 & rising, for just $500)
Support Forum | Links SQL Plugins | DMOZ Dumps | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Compare our different Plugin packages *new* Free CSS Templates
Quote Reply
Re: [Andy] eval and db failover In reply to
not working Frown

ideas to get the reason why it fails.. and use ip .21 only a few times (not always)

thanks.
Quote Reply
Re: [robyone] eval and db failover In reply to
Afraid I'm stumped Frown

Cheers

Andy (mod)
andy@ultranerds.co.uk


IMPORTANT: I've now moved to ultranerds.co.uk, and the .com will no longer work!
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package (plugins total "value" $3,325 & rising, for just $350)| GLinks ULTRA Package PRO (plugins total "value" $5,625 & rising, for just $500)
Support Forum | Links SQL Plugins | DMOZ Dumps | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Compare our different Plugin packages *new* Free CSS Templates
Quote Reply
Re: [Andy] eval and db failover In reply to
just a delay on server 1

i guess i fixed it. thanks