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.


In reply to How to set DBI connection timeout for Win32 Perl->MySQL ? by roadrunner

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.