Perl连接池

现在我们有一个使用原始DBI连接到MySQL并执行SQL语句的大型perl应用程序。 它每次创build一个连接并终止。 开始接近mysql的连接限制(一次200)

它看起来像DBIx :: Connection支持应用程序层连接池。

有没有人有任何使用DBIx::Connection经验? 连接池是否还有其他注意事项?

我也看到mod_dbd ,这是一个Apache的mod,看起来像处理连接池。 http://httpd.apache.org/docs/2.1/mod/mod_dbd.html

我没有DBIx :: Connection的任何经验,但我使用DBIx ::连接器 (实质上是什么DBIx :: Class内部使用,但内联),这是美好的…

我将这些连接与一个Moose对象包装器集中在一起,如果连接参数是相同的(这对于任何底层的DB对象都是一样的)。

 package MyApp::Factory::DatabaseConnection; use strict; use warnings; use Moose; # table of database name -> connection objects has connection_pool => ( is => 'ro', isa => 'HashRef[DBIx::Connector]', traits => ['Hash'], handles => { has_pooled_connection => 'exists', get_pooled_connection => 'get', save_pooled_connection => 'set', }, default => sub { {} }, ); sub get_connection { my ($self, %options) = @_; # some application-specific parsing of %options here... my $obj; if ($options{reuse}) { # extract the last-allocated connection for this database and pass it # back, if there is one. $obj = $self->get_pooled_connection($options{database}); } if (not $obj or not $obj->connected) { # look up connection info based on requested database name my ($dsn, $username, $password) = $self->get_connection_info($options{database}); $obj = DBIx::Connector->new($dsn, $username, $password); return unless $obj; # Save this connection for later reuse, possibly replacing an earlier # saved connection (this latest one has the highest chance of being in # the same pid as a subsequent request). $self->save_pooled_connection($options{database}, $obj) unless $options{nosave}; } return $obj; } 

只要确保:你知道DBI->connect_cached() ,对不对? 这是一个替代connect() ,在可能的情况下,在你的perl脚本生命周期中重用dbh。 也许你的问题可以通过添加7个字符来解决:)

而且,MySQL的连接相对便宜。 在max_connections=1000以上运行数据库本身不会造成问题。 (如果你的客户要求更多的工作比你的数据库可以处理,这是一个更严重的问题,一个较低的max_connections可能推迟,但当然不能解决。