I am working on a script that will run on Redhat 6.5 with Perl 5.10.1 and an Oracle 11.2.0.3 client. I need to connect to a few dozen Oracle databases using 10g, 11g or 12c. Unfortunately, there are two possible passwords that could get used, and I don't have a list of which password to try. Hence, I want to try one password and if that does not work try the other. Otherwise, I will just add it to an array that will print out at the end and I can look at those databases and possibly change the password. I am using the system to connect to every database.

How should I do the error handling to deal with not being able to connect to a database? Also when I get an error and try to exit, the script doesn't exit, but restarts from the beginning. How can I change that behavior?

Thanks in advance!
sub connect_to_db { my $tns_name = shift; my $logger = shift; my $run_as_user = shift; my $system_password = shift; my $system_password2 = shift; my $dbh; my $correct_password; # try connecting with one password then the other, if both passwords +fail mark it as failed try { $dbh = DBI->connect( "DBI:Oracle:".$tns_name, $run_as_user, $sys +tem_password, { PrintError => 1, RaiseError => 1, AutoCommit => 1 } ); $correct_password = $system_password; } catch { print "wrong password, try another\n"; }; if ( undef $dbh ) { try { $logger->info( 'password two |'.$system_password2.'|'); $dbh = DBI->connect("DBI:Oracle:".$tns_name, $run_as_user, $ +system_password2, { PrintError => 1, RaiseError => 1, AutoCommit => 1 } ); $correct_password = $system_password2; } catch { print "wrong password, add it to the connection issues list\n +"; }; } if ( undef $dbh ) { push @db_connect_issues, "error:".$tns_name." -- ".$DBI::errstr; } else { $users_and_passwords{ 'INSTALLER'}{$run_as_user } = $correct_pas +sword; print ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> The right password is + $correct_password <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n"; } return $dbh; }

In reply to error handling with dbi by gandolf989

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.