我有一個基於mod_perl2的web應用程序需要連接到mysql數據庫。我已經在一個駝鹿角色中實現了SQL連接細節。與mod_perl2 moose應用程序的數據庫連接太多
簡體,作用如下所示:
package Project::Role::SQLConnection;
use Moose::Role;
use DBIx::Connector;
has 'connection' => (is => 'rw', lazy_build => 1);
has 'dbh' => (is => 'rw', lazy_build => 1);
has 'db' => (is => 'rw', default => 'alcatelRSA');
has 'port' => (is => 'rw', default => 3306);
has 'host' => (is => 'rw', default => '10.125.1.21');
has 'user' => (is => 'rw', default => 'tools');
has 'pwd' => (is => 'rw', default => 'alcatel');
#make sure connection is still alive...
before dbh => sub {
my $self = shift;
$self->connection->run(fixup => sub { $_->do('show tables') });
};
sub _build_dbh {
my $self = shift;
return $self->connection->dbh;
}
sub _build_connection {
my $self = shift;
my $dsn = 'DBI:mysql:'.$self->db.';host='.$self->host.';port='.$self->port;
my $conn = DBIx::Connector->new($dsn, $self->user, $self->pwd);
return $conn;
}
no Moose::Role;
1;
我然後在需要與
with qw(Project::Role::SQLConnection);
語句的數據庫連接的所有駝鹿類使用這個角色。
雖然這在很少的對象被創建的時候效果很好,但當很多對象被創建時,我很快就會遇到麻煩。在httpd的日誌舉例來說,我得到的錯誤:
DBI connect('alcatelRSA;host=10.125.1.21;port=3306','tools',...) failed: Too many connections at C:/Perl/site/lib/DBIx/Connector.pm line 30
我想過使用DBIx ::連接「斷開」調用來關閉每次與數據庫的連接,但對性能的影響似乎嚴重開/根據需要關閉連接。
您對這個問題有其他建議嗎?
謝謝,我真的很喜歡這個代碼,但它仍然不能解決「太多連接..」的問題。我懷疑,對於手頭的問題,設計只是要求將某個角色的數據庫連接封裝起來,這是因爲創建的對象太多。 – 2010-06-18 10:16:37