How to set DBI connection timeout for Win32 Perl->MySQL ?

R

roadrunner

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.
 
B

Ben Morrow

Quoth roadrunner <[email protected]>:

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):

This I doubt under Win32, or under 5.8 on any platform by default.
Signals don't behave at all. Does an alarm interrupt any IO that's
taking a long time? For instance, an attempted connect() to a host that
is down?
Can anyone either see any problems with this code or suggest
alternative ways of doing this ?

AFAIK if you can't get DBI to timeout properly on its own then the only
thing you can do is execute the connect in a new thread, sleep for an
appropriate amount of time, and then if the worker thread is not
joinable (because it hasn't completed) detach it and leave it to fail in
its own time. This will be rather tricky to code, especially if you want
to poll rather than simply allowing the connect to always take maximum
time, and there may be issues with returning a DBI object from a thread.

Ben
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top