0

How can I connect to multiple databases in perl without it bailing out if it can't connect to one? If I have the database names in an array and go trough them in a loop, I want to try and connect and perform a query and disconnect. I am going to report it back to the big brother server monitoring page we have. I don't want the script to terminate if it can't connect to one, since it obviously needs to check every one in the array. Right now i use the DBI modules state method but I don't know that it works correctly. Thanks for everyone's time!

3 Answers 3

1

Can we see the code? I don't think a call to DBI->connect() will die unless you explicitly tell it to, as in:

DBI->connect($dsn, $user, $pass) or die "Can't connect: $DBI::errstr\n";

I'm pretty sure even using {RaiseError => 1} won't make it die automatically. So, if you are calling or die... just don't do it!

EDIT:

Given the code that @squiguy posted, I would do it like this:

foreach my $host (@hosts) { 
    $dbh = DBI->connect("dbi:Oracle:; host=$host; port=$port", $username, $password); 
    next unless $dbh;
    if ($dbh->state()) {
        # Do stuff with state information
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

foreach my $host (@hosts) { $dbh = DBI->connect("dbi:Oracle:; host=$host; port=$port", $username, $password); if ($dbh->state()) I am basically performing the connection and then i use state to see if it connected or not. This doesn't seem to work for me. Let me know if you get what I am trying to do or if I need to explain more. Thanks
If the connection fails, $dbh will be undefined and the $dbh->state() call will be a fatal runtime error. Say if ($dbh && $dbh->state()) ... instead.
Yes, go with @mob answer. You could also break them out into separate tests, so you could tell if you can't connect, or if status is retuning a false value.
Thanks for the input, I guess I will have to tinker around with it and see what happens.
0

You should look up exception handling in Perl. I don't use Perl so I do not know the syntax for exception handling, should be easy enough to find online though.

1 Comment

I am pretty familiar with how to do that but I can't find any exception to keep continuing through, all of them seem to exit. Thanks for your input though
0

I have the database names in an array and go trough them in a loop, I want to try and connect and perform a query and disconnect.

If you're looping, there's no reason to open multiple databases. In your loop, open a database, do your stuff, and close.

How can I connect to multiple databases in perl without it bailing out if it can't connect to one?

I'm assuming that this is in your loop. You can always use eval whenever you are doing any Perl command that might error out and stop your program from executing.

You do something like this:

for my $database (@database_list)  {
   my $dbh;
   eval {
       $dbh = DBI->connect($database, $user, $password);
   };
   if (not $@) {
        yadda, yadda, yadda
   }
}

The eval will catch any sort of ordinary deadly error. If $@ has a value, the call failed, and eval returned an error description. If $@ is empty, no error, and you can simply continue.

HOWEVER, BY default, DBI doesn't automatically die if it can't connect. Instead, it merely returns an undefined value. You should be able to use that to determine whether you've succeeded, or need to go to the next database:

           for my $database (@database_list)  {
   my $dbh = DBI->connect($database, $user, $password);
   if ($dbh) {
        yadda, yadda, yadda
   }
}

If I remember correctly, there's an attribute called RaiseError that if set will cause your program to die in a failed DBI call. However, the default should be not set, so you shouldn't be having any issues.

1 Comment

I will give this a shot and see what happens, thanks for pointing that out for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.