tôi không có bất kỳ kinh nghiệm với DBIx :: Kết nối, nhưng tôi sử dụng DBIx::Connector (thực chất là gì DBIx :: Class sử dụng trong nội bộ, nhưng inlined) và rất tuyệt vời ...
tôi hồ bơi những kết nối này với một wrapper đối tượng Moose mà trao lại trường hợp đối tượng hiện tại nếu các thông số kết nối là giống hệt nhau (điều này sẽ làm việc tương tự cho bất kỳ đối tượng DB cơ bản):
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;
}
Tôi sử dụng DBIx :: Connector (DBIx :: Class sử dụng nội bộ) và nó tuyệt vời ... Tôi gộp các kết nối này với một trình bao bọc đối tượng Moose để đưa các cá thể đối tượng hiện có trở lại nếu các tham số kết nối giống hệt nhau. Nó không khó để cuộn của riêng bạn. – Ether
@Ether - đáng để trả lời, IMHO – DVK
@DVK: ok, tôi đã mở rộng câu trả lời này ... – Ether