thcsoft has asked for the wisdom of the Perl Monks concerning the following question:
as you can see, the worker threads won't have a long lifetime. for perfomance reasons i'd like them to use cached connections to the database, but i'm afraid, that wo'nt work for the above reason.sub event_handler { my $thread_sub = sub { my $event = SOD::EventCooker->new(shift); if (exists $p_ids{$res->{$_}->{'ev_p_id'}}) +{ # do something } else { my $dbh = DBI->connect_cached("DBI:mysql: +database=$appdata{DBBASE}", $appdata{DBUSER}, $appdata{DBPASS}, { RaiseError => 1, AutoCommit => 1 }) || die $dbh->errstr; $event->evaluate($dbh); } }; my @threads; my $dbh = DBI->connect("DBI:mysql:database=$appdata{DBBASE}", $appd +ata{DBUSER}, $appdata{DBPASS}, { RaiseError => 1, AutoCommit => 1 }) || die + $dbh->errstr; while (1) { sleep 1; last if $tflag; # $tflag is a shared global. my $thread_time : shared = time; my $res = $dbh->selectall_hashref("SELECT * FROM event WHERE ev_ +time<=$thread_time", 'ev_time'); $dbh->do("DELETE FROM event WHERE ev_time<=$thread_time"); map { push @threads, threads->new(\$thread_sub->($res->{$_})); } sort keys(%$res); map { $_->join; } @threads; @threads = (); } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: DBI->connect_cached and threads?
by Anonymous Monk on May 10, 2005 at 08:17 UTC |