Marcello has asked for the wisdom of the Perl Monks concerning the following question:

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

Replies are listed 'Best First'.
Re: DBI connect problem
by Ovid (Cardinal) on Jul 30, 2002 at 20:27 UTC

    Try using RaiseError and an "or die" statement:

    my $dbh = DBI->connect( "DBI:mysql:$self->{DB_NAME}:$self->{DB_HOSTNAME}:$self->{DB_PORT}" +, $self->{DB_USERNAME}, $self->{DB_PASSWORD}, { PrintError => 0, RaiseError => 1, } ) or croak DBI->errstr;

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Re: DBI connect problem
by hiseldl (Priest) on Jul 30, 2002 at 20:33 UTC
    It seems as though your logic is backwards... my $result = defined($dbh); ...so far so good... if (!$result) { ...this is saying if $result is not defined, and this does not correlate with your statements...

    you should change if (!$result) { to if (!defined($dbh) ) { giving the following code, which should make more sense

    if (!defined($dbh) ) { $errStr .= " [dbh UNDEFINED]".$testStr; $self->log_comment("ERROR: db_connect => ".$errStr); } else { $errStr .= " [dbh DEFINED]".$testStr; }

    --
    hiseldl

      I would refactor code like
      if (!defined($dbh) ) { $errStr .= " [dbh UNDEFINED]".$testStr; $self->log_comment("ERROR: db_connect => ".$errStr); } else { $errStr .= " [dbh DEFINED]".$testStr; }
      into
      if (defined $dbh) { $errStr .= " [dbh DEFINED]".$testStr; } else { $errStr .= " [dbh UNDEFINED]".$testStr; $self->log_comment("ERROR: db_connect => ".$errStr); }
      Idea of this refactoring is that if you have an if/else construct it makes sense to swap branches if it simplifies the condition.

      --
      Ilya Martynov (http://martynov.org/)

Re: DBI connect problem
by Cine (Friar) on Jul 30, 2002 at 20:25 UTC
    I can only see one way: DBI dies, but does not set $@. Try setting RaiseError => 0

    T I M T O W T D I