gskoli has asked for the wisdom of the Perl Monks concerning the following question:

Hi all , I am newbie to perl , My scenario is m having an array of host .

In a loop i am creating DNS and connection to database . But if any host is not responding or down , then that loop is getting break and remaining host not able to connect .

My question is how can i skip that means if any host is not able to connect then i want to skip that host and want to connect with other one...Or try to reconnect that host again ...

snippet of code is given bellow

@arr_ =('host1','host2'); foreach $host (@arr_){ @Mydsn =("dbi:mysql:$MYSQL_DATABASE:$host:$MYSQL_PORT","$MYSQL_USER +","$MYSQL_PWD"); my $my_connection = DBI->connect(@Mydsn, { RaiseError => 0, PrintErro +r => 1 } ) or die("Fail to connect Database connection"); ### Here how can i skip if mysql is not able to connect with breaking + execution of script }
Please help me out ! Thanks in advance

Replies are listed 'Best First'.
Re: if database connection failed try to reconnect
by andal (Hermit) on Mar 14, 2011 at 12:26 UTC

    Just don't use "or die" in your code. Use something like

    my $cnx = DBI->connect(); if(defined $cnx) { # success } else { # try another address }

      Thanks
Re: if database connection failed try to reconnect
by Anonymous Monk on Mar 14, 2011 at 12:29 UTC
    • perldoc -f die
    • perldoc -f eval
    • perldoc -f warn
    • perldoc -f next
    • my $my_connection = ... or do { warn "DANG, I'M DEAD @Mydsn "; next; }
      thanks...