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

Hi, I'm using DBI to connect to my database, like this "$oDbc = DBI->connect("dbi:Informix:$sDatabase",...). What I am having difficulties with is finding out what is stored in $oDbc. When I use the debug I get DBI::db=HASH(...) which I expect. But I can't figure out how to see what is stored in the hash. The reason I require it is I am thinking of putting this call in a loop, instead of bombing out if it doesn't connect, I sleep a while and give it another go. I therefore need to see the two instances of connected and not. from this I can make a decision on whether it is connected or will need retried. I will also put a counter within the loop so it only fails so many times. I've been searching and can't find how to do it. Please can someone enlighten me. Many thanks.

Replies are listed 'Best First'.
Re: A Database Connection Question
by Corion (Patriarch) on Oct 08, 2015 at 15:59 UTC

    Have you looked at what the DBI documentation has to say about the word "connected"?

    Also, if the connection to the database fails, I would simply use the RaiseError attribute to find out:

    my $retries = 5; my $dbh; while( !$dbh and $retries --> 0 ) { my $connected = eval { $dbh DBI->connect( $dsn, $user, $pass, { RaiseError => 1, Prin +tError => 0 ); 1 }; if( !$connected ) { warn "Couldn't connect to $dsn: $@; $retries retries left."; } else { last; }; }; if( $retries == 0 ) { die "Couldn't connect to $dsn after five (re)tries"; };
      Many Thanks. I figured it out like this... while ( $switch == 0 ) { $switch = 1; $oDbc = DBI->connect("dbi:Informix:$sDatabase",'','' ) or $switch = 0; if (!$switch) { sleep 60; $counter++; } if ($counter == 120) { exit(0); } }
Re: A Database Connection Question
by hippo (Archbishop) on Oct 08, 2015 at 16:00 UTC

    From the fine documentation:

    If the connect fails (see below), it returns undef and sets both $DBI::err and $DBI::errstr. (It does not explicitly set $!.) You should generally test the return status of connect and print $DBI::errstr if it has failed.

    So, you can check the return value for undef and/or the class vars mentioned.

A reply falls below the community's threshold of quality. You may see it by logging in.