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?
In reply to Can't time out a $sth-fetchrow() by Skeeve
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |