Hello Group,

Due to ever increasing traffic I'm converting my site into mod_perl and
quite honestly I'm confused about persistent DB connection and DB connection
pooling.. I know there is tons of info out there on the goggle groups and on
perl.apache.com, I also picked up "Mod_Perl Developers cookbook" and I also
have the awesome "Mysql" and "MySql and Perl for the web". (BTW so far the
little mod_perl section in "MySql and Perl for the web" has proven more
useful then anything I've found in the whole Mod_Perl dev cookbook).

I've seen so many example and different ways to do it, that I have no idea
if I'm doing things the safest and most optimized way. For example I've read
a bunch of posts where people said to do pooling its only a matter of doing
your connect like "$db || DBI->CONNECT", which just gives me a syntax error.
I also hear in one place that "Apache::DBI" takes care of making sure the
connection stays alive but then I hear in another that is something that I
will need to take care of.

Below is how I've set things up, hopefully someone out there that really
knows can tell me if I'm doing something wrong or if it could be done
better!!
Right now as it stands everything is running perfect under mod_perl, again
I'm just not sure  I'm doing everything the correct way.

Thanks in Advance!
-Chris

*******************************************************************
Relevant httpd.conf:

PerlRequire /home/httpd/perl/startup.pl
PerlModule Apache::DBI
<Location /perl>
        SetHandler perl-script
        PerlHandler Apache::Registry
        Options ExecCGI
        allow from all
        PerlSendHeader On
</Location>

********************************************************************
Startup.pl

use strict;
use lib qw(/websites/perl/modules);
use DBI ();
use DBD::mysql ();
use CGI ();
use CGI::Cookie ();
CGI->compile (':all');
use Data::Pageset ();
use HTML::Template ();
use Mail::Sendmail ();
use File::Find ();

# Cache all of our HTML::Templates
my @tmpl_dir = qw(/websites/html/templates/);
print STDERR "Pre-loading HTML Templates...\n";
  File::Find::find (
       sub {
           return unless /\.tmpl$/;
               HTML::Template->new(
                              filename => "$File::Find::dir/$_",
                                  cache => 1,
                              );
           },
        @tmpl_dir
       );
1;

****************************************************************************
*******
Database Connection Package:
(This .pm file lives in the "use lib" dir from above and is what I use in
all my
scripts to make a connection to the DB).

package connect_db;
require Exporter;

our @ISA = qw(Exporter);
our @EXPORT = qw(&connect_to_db $db);
our $VERSION = 1.00;

#####################
sub connect_to_db {

# DBI->trace(3, "/websites/web_files/display.log");
my $dbpassfile = '/websites/web_files/dbroot.pass';
my ($db_username,$db_password,$db_location,$db_name,$db);
open (DBPASSFILE, "$dbpassfile") or die "Couldn't open $dbpassfile: $!\n";
while (<DBPASSFILE>) {
     ($db_username,$db_password,$db_location,$db_name) = split;
     }
my $database = "DBI:mysql:database=$db_name:host=$db_location";
$db = DBI->connect($database,$db_username,$db_password) or die "Cannot
Connect: " . $db->errstr();
close DBPASSFILE;
return $db;
}
1;

****************************************************************************
***********

In My Scripts:
I should point out that I do no forking or anything of that nature, for
example before mod_perl the DB connect package was just something that was
in "MAIN" in all my scripts, all subs and everything made use of that single
connection ($db) for all DB connections.

My mod_perl scripts go something like this:

use connect_db qw(&connect_to_db $db);
use vars qw($CGI $db);

######################################################################
# Main
#
# Setup a working environment
init_global_vals();
display_page()
#done

#############################
sub init_global_vals {

# Global Vars
$CGI = new CGI();
$db = connect_db->connect_to_db;

.....
} # end of sub
##############################




---------------------------------------------------------------------
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/           (the list archive)

To request this thread, e-mail <[EMAIL PROTECTED]>
To unsubscribe, e-mail <mysql-unsubscribe-##L=##[EMAIL PROTECTED]>
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php

Reply via email to