In a mod_perl environment, your variables (like database handles) can live longer than a single request if you declare them in a scope "outside" of the request.

For example:

package MyHandler; use strict; use warnings 'all'; use Apache2::RequestRec; use Apache2::RequestUtil; # Declare it here: my $persistent_thing; sub handler : method { my ($class, $r) = @_; # The first time this is executed in a child process, it will be cre +ated. # After that, you will just reuse the same one: $persistent_thing ||= make_the_thing(); } 1;

Please also check out Ima::DBI::Contextual as it solves the whole thing.

package MyDBI; use base 'Ima::DBI::Contextual'; my @dsn = ( 'DBI:mysql:dbname:hostname', 'username', 'password', { # Other options go here: RaiseError => 1, }); __PACKAGE__->set_db('Main', @dsn); 1;

Then, elsewhere:

use MyDBI; # Automatically pooled and recreated if the old one times out or dies: my $dbh = MyDBI->db_Main;

In reply to Re: Are connections automatically pooled when using mod_perl? by jdrago999
in thread Are connections automatically pooled when using mod_perl? by PerlOnTheWay

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.