大佬教程收集整理的这篇文章主要介绍了MySQL的异步调用模块,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
package MySQLDBI; use EV; use DBI; use base 'Exporter'; use Strict; our @EXPORT = qw(create_dbh_pool get_dbh put_dbh dbh_exec); my $dbh_pool = []; my ($db_name,$db_host,$db_user,$db_pass) ; #create dbh pool sub create_dbh_pool { ($db_name,$db_pass,$sizE) = @_; for ( 1 .. $size ) { my $dbh = DBI->connect( "dbi:mysql:database=$db_name;host=$db_host",{mysql_enable_utf8 => 1,'RaiseError'=>1},); $dbh->do('set SESSION wait_timeout=72000'); $dbh->do('set SESSION interactive_timeout=72000'); push(@{$dbh_pool},{ handle => $dbh,} ); } } #get dbh handle from pool sub get_dbh { return pop @{$dbh_pool} if scalar(@{$dbh_pool}); my $dbh = DBI->connect( "dbi:mysql:database=$db_name;host=$db_host",); $dbh->do('set SESSION wait_timeout=72000'); $dbh->do('set SESSION interactive_timeout=72000'); return { handle => $dbh,}; } #put dbh handle BACk to pool sub put_dbh { push(@{$dbh_pool},shift); } #exec sql statement. when mysql has result BACk,call callBACk func. sub dbh_exec { my ($st,$args,$cb) = @_; my $dbh = get_dbh(); return $cb->(undef,undef) unless $dbh ; my $sth = undef; $sth = $dbh->{handlE}->prepare($st,{async =>1}); if ( $args ) { eval {$sth->execute(@{$args})}; } else { eval { $sth->execute() } } my $w; if ( [email protected] =~ /gone/i ) { undef $dbh; $dbh = DBI->connect( "dbi:mysql:database=$db_name;host=$db_host","$db_user","$db_pass",'RaiseError'=>1} ) or die "can not connect to db!\n"; $dbh->do('set SESSION wait_timeout=72000'); $dbh->do('set SESSION interactive_timeout=72000'); $sth = $dbh->prepare($st,{async=>1}); $w = EV::io $dbh->mysql_fd,EV::rEAD,sub{ $cb->($dbh,$sth); delete $dbh->{w}; put_dbh({ handle => $dbh}); }; if ( $args ) { eval {$sth->execute(@{$args})}; } else { eval { $sth->execute() } } } $w = EV::io $dbh->{handlE}->mysql_fd,sub{ my $w=shift; $cb->($dbh,$sth); delete $dbh->{w}; put_dbh($dbh); }; $dbh->{w} = $w; } 1; __END__ =pod =head1 NAME MySQLDBI - a MySQL async caller DBI =head1 SYNOPSIS use MySQLDBI; create_dbh(...); $dbh = get_dbh(); $dbh->dbh_exec("SQL statement",$args_array_ref,sub { my ($dbh,$sth) = @_; #... });
以上是大佬教程为你收集整理的MySQL的异步调用模块全部内容,希望文章能够帮你解决MySQL的异步调用模块所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。