A couple comments:

  1. Since you've set RaiseError =>1, if your connection fails, you'll get a fatal exception. For truly comprehensive exception catching, $dbh should be scoped to within your eval. Your disconnect should be in that scope, and thus there should be no disconnect in your exception handling block.

  2. As per the previous point, if (!$dbh) { is dead code.

  3. As per the previous point, if ($dbh) { a pointless test.

  4. You shouldn't be interpolating values for your insert; rather you should be using Placeholders and Bind Values (permalink: DBI). This will avoid quoting complications and errors. Something like:
    my $sql = 'INSERT INTO test_table VALUES (?,?,?,?,?,?)'; my $sth = $dbh->prepare($sql); $sth->bind_param(1, 'test_env'); $sth->bind_param(2, $partner); $sth->bind_param(3, $id); $sth->bind_param(4, $filename); $sth->bind_param(5, "$date $time"); $sth->bind_param(6, $line_count); $sth->execute();
    or, with short-hand for simple binding and a here-doc:
    my $sql = <<EOSQL; INSERT INTO test_table VALUES (?,?,?,?,?,?) EOSQL my $sth = $dbh->prepare($sql); $sth->execute('test_env', $partner, $id, $filename, "$date $time", $line_count, );
    If you want to bypass this step, you would probably just execute the SQL with a $dbh->do.

  5. If you invoke disconnect on an inactive database handle (either because it's already disconnected, or because the connection ever happened), it's fatal as well. So that would need catching as well.

#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.


In reply to Re: DB Exception Handling by kennethk
in thread DB Exception Handling by sowais

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.