Skeeve has asked for the wisdom of the Perl Monks concerning the following question:
I have a query which currently requires too much time. So I'd like to set a timeout and tried it like this:
my $message= "This message should not appear"; my $exitcode= $UNKNOWN; eval { my $dbh = undef; my $sth = undef; local $SIG{ALRM} = sub { if ($sth) { $sth->cancel(); } die $timeoutstr; }; alarm $timeout; $dbh = DBI->connect("dbi:Oracle:$sid", $user, $pass, { AutoCommit => 0, RaiseError => 0, PrintErro +r => 1 } ); $sth = $dbh->prepare($stm) or do_exit($UNKNOWN, $dbh->errstr); $sth->execute() or do_exit($UNKNOWN, $dbh->errstr); if(my @row = $sth->fetchrow()) { # ... } else { $exitcode= $UNKNOWN; $message= "Did not get a result: " . ($DBI::errstr || ''); } $sth->finish(); $dbh->disconnect(); alarm 0; }; if ($@ and $@ eq $timeoutstr) { do_exit($CRITICAL, "Timeout after $timeout sec\n"); }
Unfortunately the alarm handler seems to be not invoked at all until I press Ctrl-C.
Do I make a mistake here? Are there workarounds?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Can't time out a $sth-fetchrow()
by mje (Curate) on May 27, 2011 at 07:54 UTC | |
by Skeeve (Parson) on May 27, 2011 at 08:24 UTC | |
by mje (Curate) on May 27, 2011 at 09:44 UTC | |
by Skeeve (Parson) on May 27, 2011 at 09:59 UTC | |
|
Re: Can't time out a $sth-fetchrow()
by Skeeve (Parson) on May 27, 2011 at 07:51 UTC |