Perl连接池

bon*_*nez 11 mysql apache perl mod-perl

现在我们有一个大型的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

Eth*_*her 9

我对DBIx :: Connection没有任何经验,但我使用的是DBIx :: Connector(本质上是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;
}
Run Code Online (Sandbox Code Playgroud)


小智 5

只是确定:你知道DBI->connect_cached(),对吗?这是一个下拉更换为connect()一个可重用胸径的,如果可能的话,在你的Perl脚本的生活.也许你的问题可以通过添加7个字符来解决:)

而且,MySQL的连接相对便宜.与您的数据库运行max_connections=1000或更多本身不会导致问题.(如果您的客户要求的工作量超出您的数据库所能处理的范围,那么这是一个更严重的问题,一个较低的问题max_connections可能会推迟,但当然无法解决.)

  • MySQL没有"付费"版本.这都是GPL.最好的情况是,Oracle可能会有一份您可以支付的支持合同,但该软件本身是完全免费且未开发的. (2认同)