CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: DBI:
DBI error handling, or not handling



Mar 23, 2012, 12:27 PM

Post #1 of 2 (46213 views)
DBI error handling, or not handling Can't Post

I seem to always post by beginning with an apology. I am very familiar with the content on some forums and others not so much. I'm certain that this must have been tackled by someone else so I must just be using the wrong search words, etc.

We've probably all been there... 200 various DBs that "touch" our subject matter area OR the random "drive by" from a colleague: "Did you <MissionCriticalDatabase> is down?!"

As a common admin task, I want to make a script that will read through a flat file with database names, host names, user ids, passwords, and ports. For each entry, a test of connectivity to that database would be executed and a simple success/failure would be echo'd out. I'll add all the bells and whistles I need (like arguments for ALL or specific instances, etc)

Where I'm stuck is that in my perl code, when the connection information is correct, it works just fine but if I change the port number or password to be incorrect (negative testing), I get an error (or no error) and the script fails. I would like the fact that the script cannot connect to the database to NOT be a fatal error.

I've successfully suppressed the output of the error but the script still exits. This is DB2, on AIX 5.3, Perl version 5.8.8 I won't go in to everything I've done but I've tried many things involving redirecting STDERR, setting RaiseError, PrintError, using an IF-THEN on the $dbh var, etc.

Again, I don't want to reinvent the wheel so if some knows of a simple script that I can gently plagiarize that does just this thing, please point me to it.

open STDERR, "> /dev/null" || die "Cant redirect standard error: $!";

#...sub connect to the database
sub TestDBConnect {
my ($inDB, $inHost, $inPort) = ($_[0], $_[1], $_[2]);
my ($inUID, $inPWD) = ($_[3], $_[4]);
print "Testing DB connection to: $inDB @ port: $inPort, with $inUID/********";
my $dsn = "dbi:DB2:DATABASE=$inDB; HOSTNAME=$inHost; PORT=$inPort; UID=$inUID; PWD=$inPWD";
my $dbh = DBI->connect($dsn, $inUID, $inPWD, {PrintError => 0});


Mar 25, 2012, 11:46 AM

Post #2 of 2 (46111 views)
Re: [dw.worker.bee] DBI error handling, or not handling [In reply to] Can't Post

You can 'trap' fatal errors with eval. (Refer: perldoc -f eval).
Good Luck,


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives