Cirollo has asked for the wisdom of the Perl Monks concerning the following question:

Is it possible to set a timeout on a DBI query? (fyi, I'm using Oracle) I have some queries that seem to just hang when the (rather large) database tables are being refreshed.

Replies are listed 'Best First'.
Re: DBI timeout
by eduardo (Curate) on Jul 20, 2000 at 20:53 UTC
    from the DBI manpage:
    The two most common uses of signals in relation to the DBI
    are for canceling operations when the user types Ctrl-C
    (interrupt), and for implementing a timeout using alarm()
    and $SIG{ALRM}.
    
    and according to the perlfunc manpage on alarm you would do something along these lines:
    eval { local $SIG{ALRM} = sub {die "alarm\n";}; alarm($timeout); $db->execute($query); alarm(0); }; if ($@) { die "Error" unless $@ eq "alarm\n"; #timed out } else { #didnt }
Re: DBI timeout
by maverick (Curate) on Jul 20, 2000 at 20:57 UTC
    don't know if there's a built in time out for DBI or not, but you could try using alarm.
    $SIG{'ALRM'} = sub { print "Time's up\n"; }; # give it 15 seconds alarm(15); # dbi calls # turn the alarm off alarm(0);

    /\/\averick

      gee maverick... our answers are so similar... it's like we know each other, worked next to each other for 8 months, live 3 minutes down the road from each other, and hang out damned near on a daily basis or something! :) now, my question to you about your response... how would you use the alarm going off here to get out of the DBI call going bad... you didn't encase it in a block or anything like that. also, i heard somewhere that it was good to reset alarm handlers at the end of themselves for some reason... can anyone back that up, or is that one of those things i immagined?

      update: ok, just out of curiosity... why was this voted down? i mean, no biggie if someone didn't like me having a conversation with mav, that's fine... but i can't understand why this was voted down...

        who are you? what are you talking about?

        Ya, your solution is better. 'course I pulled this out of my head in about 3 seconds, I didn't rogue it from a perldoc page :) Depending on how I wanted to handle the timeout, I would have probably ended up with an eval block anyways.

        /\/\averick

      Thanks, thats exactly what I needed. Plus, I can use the subroutine to print a nice error message in the HTML output.