现在我们有一个大型的perl应用程序,它使用原始DBI连接到MySQL并执行SQL语句。它每次都创建一个连接并终止。开始接近mysql的连接限制(一次200)
看起来像 DBIx ::连接 支持应用程序层连接池。
有没有人有任何经验 DBIx::Connection
?。连接池是否还有其他注意事项?
我也看到了 mod_dbd
这是一个看起来像处理连接池的Apache mod。
http://httpd.apache.org/docs/2.1/mod/mod_dbd.html
现在我们有一个大型的perl应用程序,它使用原始DBI连接到MySQL并执行SQL语句。它每次都创建一个连接并终止。开始接近mysql的连接限制(一次200)
看起来像 DBIx ::连接 支持应用程序层连接池。
有没有人有任何经验 DBIx::Connection
?。连接池是否还有其他注意事项?
我也看到了 mod_dbd
这是一个看起来像处理连接池的Apache mod。
http://httpd.apache.org/docs/2.1/mod/mod_dbd.html
我对DBIx :: Connection没有任何经验,但是我使用了 DBIx ::连接器 (基本上是DBIx :: Class在内部使用的内容,但内联)并且很棒......
我将这些连接与Moose对象包装器集合在一起,如果连接参数相同,则会将现有对象实例交给它(对于任何底层数据库对象,这都是相同的):
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的连接相对便宜。在你的DB上运行 max_connections=1000
或更多本身不会导致问题。 (如果您的客户要求的工作量超出您的数据库所能处理的范围,那么这是一个更严重的问题,一个更低的问题 max_connections
可能会推迟,但当然不能解决。)