Thread Timeout für mysql-Statements (5 answers)
Opened by pq at 2011-01-25 12:20

pq
 2011-01-25 13:41
#145003 #145003
User since
2003-08-04
12207 Artikel
Admin1
[Homepage]
user image
als alternativen workaround habe ich jetzt im irc einen vorschlag bekommen:
ich hole mir zuerst die connection id, und benutze das alarm mit POSIX::SigSet/POSIX::SigAction aus der DBI-doku (normales alarm funktioniert hier nicht).

im alarm-handler mache ich eine neue db-verbindung auf und kille die connection id.

Code (perl): (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
use POSIX ":signal_h";
my $mask = POSIX::SigSet->new( SIGALRM );
my $dbh = ...;
my ($t) = $dbh->selectrow_array("SELECT CONNECTION_ID()");

my $sth = $dbh->prepare("mein select");
my $action = POSIX::SigAction->new(      
    sub {
        my $dbh = ...;
        my $foo = $dbh->prepare("kill ?");
        $foo->execute($t);
        die "connection ($t) timeout";
    }, 
    $mask,
);     
my $oldaction = POSIX::SigAction->new(); 
sigaction( SIGALRM, $action, $oldaction ); 
eval {
    alarm 5;
    $sth->execute(...);
    alarm 0;
};  
alarm 0;
sigaction( SIGALRM, $oldaction );
if ($@) {
    ...
}


edit: perl-code hinzugefügt

edit: "ALRM" in SIGALRM umgewandelt, ist in perldoc DBI falsch dokumentiert!
Last edited: 2011-02-01 14:58:21 +0100 (CET)
Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live. -- Damian Conway in "Perl Best Practices"
lesen: Wiki:Wie frage ich & perlintro Wiki:brian's Leitfaden für jedes Perl-Problem

View full thread Timeout für mysql-Statements