On 2005-03-23 02:09:40 -0800, Chris Masters wrote:
> --- Chris Masters <[EMAIL PROTECTED]> wrote:
> > Hi All,
> > 
> > A firewall is terminating my connection with a mysql
> > database after a period of inactivity. I've used the
> > following code (with ref to cpan docs and connect)
> > and
> > it still causes a hang.

I'm not sure what you are trying to achieve. Do you want to periodically
send a dummy query to prevent the firewall from terminating the connection,
or do you just want to get an error when that happens? Your code looks
like you want the latter.

> > ###################################################
> > 
> > my $result = 0;
> > my $alarm = 0;
> > eval
> > {
> >     #set alarm to timeout query hangs
> >     my $h = Sys::SigAction::set_sig_handler( 'ALRM'
> > ,sub { $alarm = 1; } );
> >     alarm(2);
> > 
> >     my $sth = $dbh->prepare("SELECT 1");
> >     $sth->execute();
> >     $result = 1;
> > 
> >     alarm(0);

That should work if the the mysql library actually returns a failure if
a read from the socket returns EINTR. If it just restarts the read,
your sig handler will set $alarm = 1, but $sth->execute won't return.

It's probably a bad idea to interrupt complex library functions with a
signal anyway, unless it is documented that doing so is safe.

It would be better to set a timeout, but this doesn't seem so easy: The
mysql docs mention a read_timeout and a write_timeout which "currently
only work on windows on tcp connections". 

The kernel should pronounce the connection dead if it never gets an ACK
for the packets it sends out, but the timeout for that may be too long
for your application.

> > Would it be better to use a select(2) wrapper?

I don't think so. What would you select on?

> > Any other ideas to prevent this hang?

You could set the idle timeout on the server to less than the timeout on
the firewall. In this case the server will terminate the connection
gracefully and the client will notice this and can reopen the
connection.

        hp

-- 
   _  | Peter J. Holzer \Beta means "we're down to fixing misspelled comments in
|_|_) | Sysadmin WSR     \the source, and you might run into a memory leak if 
| |   | [EMAIL PROTECTED]     \you enable embedded haskell as a loadable module 
and
__/   | http://www.hjp.at/ \write your plugins upside-down in lisp". [EMAIL 
PROTECTED]

Attachment: pgpZworh5riuD.pgp
Description: PGP signature

Reply via email to