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
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
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
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
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
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] eval and db failover In reply to
just a delay on server 1

i guess i fixed it. thanks