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

Hello Monks,

I'm writing some code that makes database connections for me, I want it to be robust enough to retry the connection if it fails without having to wait the default DBI timeout period.

Is it possible to set the timeout value for DBI::connect()? If not, is it safe to alarm out of a connect(), or is there a better way? The pod does discuss alarm signals, but does not describe using them with a connect().

Also, do DBI database handles have a method to test if the connection to the database is active? I'd like to be able to do this both to make sure my connection was successful and also later to make sure the connection didn't drop. I'm assuming that since Apache::DBI is smart enough to re-create connections whenever they drop that it must have some way of doing this.

Thanks for any help!

Replies are listed 'Best First'.
Re: DBI Timeout and Retry on Connect
by maverick (Curate) on Mar 14, 2002 at 23:29 UTC
    check the DBD:: module for your particular database...mysql has one and it's specified like this:
    dbi:mysql:database=test;host=127.0.0.1;mysql_connect_timeout=2
    inside the dsn param to connect.

    /\/\averick
    perl -l -e "eval pack('h*','072796e6470272f2c5f2c5166756279636b672');"

      Thanks. I was kind of afraid that might be the case. I'm using DBD::Pg, so I guess I'm going to be stuck trying the alarm().

Re: DBI Timeout and Retry on Connect
by perrin (Chancellor) on Mar 14, 2002 at 23:31 UTC
    I don't know if alarm is safe for this, but I'd suggest you just go ahead and try it. As for checking whether a connection is active, you can use the ping() method. See the DBI docs for details.