现在,我们有一个大型的perl应用程序,它使用原始DBI连接到MySQL并执行SQL语句。它每次都会创建一个连接并终止。我们开始接近mysql的连接限制(一次200个)
看起来DBIx::Connection支持应用层连接池。
有人有过使用DBIx::Connection的经验吗?对于连接池,还有其他的考虑因素吗?
我还看到了mod_dbd,它是一个Apache mod,看起来像是处理连接池。http://httpd.apache.org/docs/2.1/mod/mod_dbd.html
发布于 2010-07-17 04:50:51
我没有任何使用DBIx::Connection的经验,但我使用DBIx::Connector (本质上是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;
}发布于 2010-08-15 01:15:44
只是确认一下:你知道DBI->connect_cached(),对吧?它是connect()的临时替代品,在您的perl脚本的整个生命周期中尽可能重用dbh。也许您的问题可以通过添加7个字符来解决:)
而且,MySQL的连接相对便宜。在max_connections=1000或更高版本上运行数据库本身不会导致问题。(如果客户要求的工作超过了数据库的处理能力,这是一个更严重的问题,较低的max_connections可能会推迟,但当然不能解决。)
https://stackoverflow.com/questions/3267591
复制相似问题