Hi,

I use the following routine in a package to connect to a MySQL database on a Linux system (with a lot of debugging stuff to solve my problem):
sub db_connect { my $self = shift; my $errStr; my $testStr; my $dbh; eval { local $SIG{ALRM} = sub { die "timeout\n" }; # NB: \n required alarm 10; $dbh = DBI->connect("DBI:mysql:".$self->{DB_NAME}.":".$self->{ +DB_HOSTNAME}.":".$self->{DB_PORT}, $self->{DB_USERNAME}, $self->{DB_P +ASSWORD}, { PrintError => 0 }); if (defined($dbh)) { $testStr = "[Database handle SET]"; } else { $errStr = "Unable to connect to database: ".$DBI::errstr; $testStr = "[Database handle NOT SET]"; } alarm 0; }; if ($@ eq "timeout\n") { # Timeout $errStr = "Connection to database timed out"; } elsif ($@) { # Unknown error $errStr = "Unknown error: ".$@; } my $result = defined($dbh); if (!$result) { $errStr .= " [dbh DEFINED]".$testStr; } else { $errStr .= " [dbh UNDEFINED]".$testStr; $self->log_comment("ERROR: db_connect => ".$errStr); } return $result; }
I use a timeout on the database connect, the parameters to the DBI->connect routine are OK. Almost always the connection is succesful, but sometimes this line shows up in the logfile:

2002-07-30 21:48:31: ERROR: db_connect => dbh UNDEFINED

So the DBI->connect failed, but I don't know why. It seems to me that $errStr should always have a value and when the connect does NOT timeout, $testStr also. But the logfile shows otherwise!

I am missing something... but what and where?

TIA, Marcel

In reply to DBI connect problem by Marcello

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.