perl - Too many DB connection with mod_perl2 moose application -


I have a mod_perl2 based web application for which a connection to a mysql database requires a moss role of the SQL connection attributes

Simplified, the role appears as follows:

  Package project :: Role :: SQL connection; Use Moose: Role; Use the DBIKS :: connector; 'Connection' = & gt; (Is = & gt; 'rw', lazy_build = & gt; 1); 'DbH' = & gt; (Is = & gt; 'rw', lazy_build = & gt; 1); 'Db' ​​= & gt; (Is => 'RW', default => 'Alcatel RSA'); 'Port' is = & gt; (Is = & gt; 'rw', default => 3306); 'Host' = & gt; (Is => 'RW', default => '10 .125.1.21 '); 'User' = & gt; (Is = & gt; 'rw', default = & gt; 'tools'); 'Pwd' = & gt; (Is = & gt; 'rw', default => 'Alcatel'); #Make sure the connection is still alive ... first DBH = & gt; Run (my $ self = shift; $ self-> connection- & gt; play (fix up = & gt; sub {$ _- & gt; ('show table')});}; sub_build_dbh {my $ Self = shift; back $ self-> connection-> dbh;} sub_built_connection {my $ self = shift; my $ dsn = 'dbi: myscl:'. Self- & gt; db. '; Host = '. $ Self- & gt; Host.'; Port = '. $ Self- & gt; Port; My $ conn = dbix :: connector-> new ($ DSN, $ self-> user, $ Self- & gt; PWD); Return $ Con;} No Moose: Role; 1;  

Then I use this role to all

with qw (project :: role :: sql connection)

statement in the MOOS classes that require connection with DB .

When some objects are created, then I work well, when I create many objects, for example in the httpd log, I get the error:

< P> DBI connect ('alcatelRSA; host = 10.125.1.21; ports = 3306', 'device', ...) failed: many connections at many Celsius: / Perl / site / li B / dbix / connector.pm line 30

I thought about using the DBIx :: Connectors to disconnect the phone database for each connection.

< P> Do you have any alternative suggestions on this problem?

Are you copying the DBH and using it in those places when the objects outside the scope Gets out of it? The documentation specifically asks for it not to do so, instead to save the DBIx :: Connector object, and option in a handle attribute with delegate DBH Call for method.

This is what I do (I actually posted the code in response to another question; strange how DB comes in the question pack):

  Is dbixc = & gt; (= & Gt; 'ro', isa = & gt; 'dbix :: connector', lazy_build = & gt; .1, # No, save a copy of DBH. This accessor is used every time, # as ever Sometimes this will change unexpectedly! Handle => [qw (dbh)],); Sub_build_dbixc {my $ this = shift; Dbiex :: connector- & gt; New ($ this- & gt; DSN, $ this- & gt; username, $ this- & gt; password, $ this- & gt; connect_option;); } Sub call_dbh {my $ this = shift; My $ method = shift; My @args = @_; # General Behavior - Pings at every DBH Access # Return this-> gt; Dbh- & gt; $ Method (@args); # Smart Practices - Estimate that DB connections are still $$ -> gt; Dbixc- & gt; Play (FixUp = & gt; sub {$ _- & gt; $ method (@args)}); }  

You can also see how many mod_perl processes you are allowing. Each separate process or thread should have its own DB connection, but can be more than one - so you must also ensure that only the code run above (i.e. the DB management object is created) only once per process, and every subsequent effort to create such an object only gets a copy of the existing one, it is an easy way to do this, but it is a simple way to do this. He presents himself to other design issues.


Comments

Popular posts from this blog

windows - Heroku throws SQLITE3 Read only exception -

lex - Building a lexical Analyzer in Java -

python - rename keys in a dictionary -