Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 869609
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T10:20:07+00:00 2026-05-15T10:20:07+00:00

I have a mod_perl2 based web app that requires a connection to a mysql

  • 0

I have a mod_perl2 based web app that requires a connection to a mysql database. I have implemented the SQL connection specifics in a moose role.

Simplified, the role looks as follows:

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;

I then use this role in all moose classes that require a connection to the DB with a

 with qw(Project::Role::SQLConnection);

statement.

While this works well when a few objects are created, i soon run into troubles when to many objects are created. In the httpd log for instance, i get the error:

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

I thought about using DBIx::Connectors “disconnect” call to close the connection to the database each time, but the performance impact seem to severe to open / close connections as required.

Do you have any alternative suggestions on this problem?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T10:20:08+00:00Added an answer on May 15, 2026 at 10:20 am

    Are you copying the dbh and using it in places when the out of the DBIx::Connector object goes out of scope? The the documentation specifically says not to do that. Instead, save the DBIx::Connector object itself, and delegate the dbh method call to it with a handles option in the attribute.

    This is what I do (I actually just posted this code yesterday in response to another question; funny how DB questions come in packs):

    has dbixc => (
        is => 'ro', isa => 'DBIx::Connector',
        lazy_build => 1,
        # DO NOT save a copy of the dbh. Use this accessor every time, as
        # sometimes it will change unexpectedly!
        handles => [ qw(dbh) ],
    );
    
    sub _build_dbixc
    {
        my $this = shift;
        DBIx::Connector->new(
            $this->dsn,
            $this->username,
            $this->password,
            $this->connect_options,
        );
    }
    
    sub call_dbh
    {
        my $this = shift;
        my $method = shift;
        my @args = @_;
    
        # the normal behaviour -- pings on every dbh access
        #return $this->dbh->$method(@args);
    
        # the smart behaviour -- presume the DB connection still works
        $this->dbixc->run(fixup => sub { $_->$method(@args) });
    }
    

    You may also want to look at how many mod_perl processes you are allowing. Each separate process or thread necessarily must have its own DB connection, but there may be more than one — so you probably also need to ensure that the code above only runs (i.e a db management object is built) only once per process, and each subsequent attempt to build such an object simply returns a copy of the existing one. One easy way to do this is with MooseX::Singleton, but this introduces other design issues of its own.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My company develop web apps using combination of mod_perl, axkit and apache. We have
I have some mod_rewrite rewrites that correctly redirect the URLs to a script. From
I have an htaccess file that uses mod_rewrite to redirect /controller to /index.php?controller=%controller% Like
I have a website that employs a generic mod_rewrite rule to push all requests
I have a strange problem with mod_rewrite, the rules that are relevant here are:
On my site I have mod_rewrite rules to make the URLs more search engine
I have Apache with mod_rewrite, and whenever I enter a URI with an accented
I have been sold on mod_wsgi and apache rather than mod_python. I have all
I have a few problems with my mod_rewrite rules. There a three different url
I have an Apache 2 installation on Debian with mod_ssl installed. The server private

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.