Re: mysql_connect_timeout problem when using times less than 1 sec



On 24.09.2006 14:12, Duncan Wren wrote:
I need a very short time out as the application i'm using has mutiple
connects to the db per page so if the dbhost is down i need it timeout
quickly to ensure the page is returned quickly to the user. Otherwise the
page hangs for several secs.

Abort if the first connection in the page times out after 1 or 2 seconds and assume the server is down instead of trying to connect again:

# start of page
my $dbh=DBI->connect(... mysql_connect_timeout => 1);
return unless $dbh; # Server is down, the assuming the following connects will also fail
# or, if you need an Exception:
# die "Can't connect to server" unless $dbh;

my $sth=$dbh->prepare ...

my $dbh2=DBI->connect(... mysql_connect_timeout => 1);
return unless $dbh2;

and so on

Maximum timeout is one second, with this code.


Alexander


The only solution i can see is ensuring that each page only has 1 connection
to the db (i know it should be like this anyway, but this is old code). As 1
x 1sec timeout is acceptable but 10 x 1 sec timeout is not.

Your correct that a combo of select/fork/kill will end up very messy so that
is not really an option.

Thanks again for you help Alexander

On 24/09/06, Alexander Foken <alexander@xxxxxxxx> wrote:

http://search.cpan.org/src/CAPTTOFU/DBD-mysql-3.0007/dbdimp.c line 1309
and following, the value is converted to an int, then passed to
mysql_options(), which is a part of the mysql API. (Ignore the cast to
const char *, that's just how the mysql_options() API call is defined.)
Looking at http://dev.mysql.com/doc/refman/5.0/en/mysql-options.html ,
there IS NO WAY to specify a timeout of less than one second.

It seems that you have to live with a minimum timeout of one second, as
alarm() also has only seconds resolution. The only way for a timeout of
less than one second is a combination of fork(), select(), kill() and a
custom signal handler, which is really, really ugly.

Let's try to solve the problem in an other way: Why do you need such a
short timeout?

Alexander

On 24.09.2006 12:23, Duncan Wren wrote:
> I have looked at the code for DBD::mysql and DBI.pm but cannot workout
> where
> the timeout is implemented or where the default is specified. Does
anyone
> know where this logic is implemented? I would rather make make the
> change in
> the module that currently implements the time out code than add some
> logic
> to add timeout in my application code.
>
> Duncan
>
> On 24/09/06, Alexander Foken <alexander@xxxxxxxx> wrote:
>>
>> Without looking at the source code of DBD::mysql, I would quess that
the
>> mysql_connect_timeout parameter is an integer. 0.5 is rounded down to
0,
>> which is the same as not specifying mysql_connect_timeout at all. If
you
>> are on a Unix/POSIX System, alarm() and Signals may help.
>>
>> Alexander
>>
>> On 23.09.2006 18:02, Duncan Wren wrote:
>> > The DBI module when connecting to a mysql database seams to only
>> support
>> > integer values for the mysql_connect_timeout if you use say 0.5 secs
>> > as the
>> > timeout value it defaults to 3secs. Does anyone know a way round this
>> > problem as i need a very short timeout in my application.
>> >
>> > for example
>> >
>> > #does not work and defaults to 3 secs timeout
>> > $dbh =
>> > DBI->connect("DBI:mysql:db_name:db_host:3306;mysql_connect_timeout=
>> > 0.5","username","password");
>> >
>> > #does work and timesout after 1 sec
>> > $dbh =
>> >
>>
DBI->connect("DBI:mysql:db_name:db_host:3306;mysql_connect_timeout=1","username","password");

>>
>> >
>> >
>> > Thanks
>> >
>> > Duncan
>> >
>>
>>
>> --
>> Alexander Foken
>> mailto:alexander@xxxxxxxx http://www.foken.de/alexander/
>>
>>
>


--
Alexander Foken
mailto:alexander@xxxxxxxx http://www.foken.de/alexander/





--
Alexander Foken
mailto:alexander@xxxxxxxx http://www.foken.de/alexander/

.



Relevant Pages

  • Re: web serivce: client side timeout?
    ... client side but only from the server side. ... System.Net.Sockets.SocketException: A connection attempt failed ... asyncResult, Int32 timeout, Exception& exception) ... John Saunders | MVP - Windows Server System - Connected System ...
    (microsoft.public.dotnet.framework.webservices)
  • Re: Timeout expired. The timeout period elapsed prior to completion of the operation or the server i
    ... This does not look like a Connection Pool issue--the message returned by the ... errors are traditionally caused when SQL Server is loaded beyond capacity. ... Message: Timeout expired. ... it appears that this error is more of a TCP/IP type ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: Outlook and Outlook Express die....after a while.
    ... >> Your server has unexpectedly terminated the connection. ... with as little as 4% packet loss. ... tolerant so it doesn't look to be a server-side timeout problem during ...
    (microsoft.public.windows.inetexplorer.ie6_outlookexpress)
  • Re: detecting the (brute) disconnection of a client
    ... crashed, and if an exception occurs, then the client is deconnected. ... You can either use the timeout itself as an indication of connection failure, or you can attempt to send data after the timeout. ... The server has no way to know if the connection has failed due to a client-side crash, or simply due to a temporary problem with the network. ... IMHO, it would be better to simply let the server continue to assume that the connection is valid unless there is some explicit, non-arbitrary verification that it's not. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Sendmail + DoS???
    ... carefully examining the behaviour of the server for those mails, ... which enforces a timeout at this stage: ... Throtle and connection rate limit are solutions for that. ... I use milter-error to stop the kind of dictionary attacks that include ...
    (comp.mail.sendmail)