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

Hi,

I'm struggling to set a timeout value for a DB connection from Perl DBI to MySQL on Win32. No matter what I try it always seems to default to timing out after around 23 seconds (I'm testing to a machine which is not responding to ping to simulate a network failure).

I've tried setting the "mysql_connect_timeout" value as below but this seems to make no difference:

$dbh = DBI->connect("DBI:mysql:db_name:db_host:3306;mysql_connect_ti +meout=5","username","password");
I've also tried wrapping the code block in an alarm as below, but again this makes no difference (note the alarm function seems to work fine when I test it with other commands):
eval { local $SIG{ALRM} = sub { die "connect timeout\n" }; alarm 5; $dbHandle = DBI->connect("DBI:mysql:db_name:db_host:3306","username +","password") or die $DBI::errstr; alarm 0; }; if ($@) { # Timed out die unless $@ eq "connect timeout\n"; }
Can anyone either see any problems with this code or suggest alternative ways of doing this ?

I'm using ActiveState Perl v5.8.8 (build 822) on Win32 with DBI v1.58 and DBD-MySQL v4.005

Thanks,
Roadrunner.

Replies are listed 'Best First'.
Re: How to set DBI connection timeout for Win32 Perl->MySQL ?
by erroneousBollock (Curate) on Oct 13, 2007 at 12:33 UTC
    I don't know whether you're doing anything wrong w.r.t the connection timeout, but if you're relying on DNS, it might be a good idea to see how long that takes to timeout in the same situation.

    -David

      Thanks for the suggestion - I don't think there should be any DNS in play here though as I'm going direct to the IP address.
Re: How to set DBI connection timeout for Win32 Perl->MySQL ?
by Cop (Initiate) on Oct 13, 2007 at 15:59 UTC

    If you have control, you can set it in MySQL.

      I do have access to MySQL but I don't think this is where I need to tune any properties - I'm interested in setting a timeout value within Perl itself, such that if it has not managed to connect to the MySQL server after X seconds it attempts the backup MySQL server instead.
      A reply falls below the community's threshold of quality. You may see it by logging in.