Thread: Perl - Apache / Postgress

Perl - Apache / Postgress

From
"Erik Colson"
Date:
I'm using Apache with mod_perl to access a PostgresSQL database (6.5) .
 
The script starts with connecting to the database... this means that the server is actually reconnecting everytime the script starts and disconnect when the HTML page is generated.
 
I've read about a possibility to make a 'permanent' connection to the database ? Can anyone tell how ?
 
 
Thanx !
 

Re: [GENERAL] Perl - Apache / Postgress

From
Adriaan Joubert
Date:
> Erik Colson wrote:
>
> I'm using Apache with mod_perl to access a PostgresSQL database (6.5)
> .
>
> The script starts with connecting to the database... this means that
> the server is actually reconnecting everytime the script starts and
> disconnect when the HTML page is generated.
>
> I've read about a possibility to make a 'permanent' connection to the
> database ? Can anyone tell how ?

I've never used mod_perl, but I use FastCGI with CGI.pm, and that works
just great. In the CGI script you have a loop


<Connect to database>
while (my $q = new CGI::Fast) {
  <Handle CGI requests>
}

and this works just fine. The Apache fastCGI module is available from
www.fastcgi.com.

Adriaan

Re: [GENERAL] Perl - Apache / Postgress

From
Gilles Darold
Date:
Adriaan Joubert wrote:

> > Erik Colson wrote:
> >
> > I'm using Apache with mod_perl to access a PostgresSQL database (6.5)
> > .
> >
> > The script starts with connecting to the database... this means that
> > the server is actually reconnecting everytime the script starts and
> > disconnect when the HTML page is generated.

I have the same problem, I still not do something to resolve it but I
think the solution is what you
can read below :

PERSISTENT DATABASE CONNECTIONS

Another popular use of mod_perl is to take advantage of it's persistance
to maintain open database connections. The basic idea goes like so:

 #Apache::Registry script
 use strict;
 use vars qw($dbh);

 $dbh ||= SomeDbPackage->connect(...);

Since $dbh is a global variable, it will not go out of scope, keeping the
connection open for the lifetime of a server process, establishing it
during the script's first request for that
process.

It's recommended that you use one of the Apache::* database connection
wrappers. Currently for DBI users there is Apache::DBI and for Sybase
users Apache::Sybase::DBlib.
These modules hide the peculiar code example above. In addition, different
scripts may share a connection, minimizing resource consumption. Example:

 #httpd.conf has
 # PerlModule Apache::DBI
 #DBI scripts look exactly as they do under CGI
 use strict;
 my $dbh = DBI->connect(...);

Although $dbh shown here will go out of scope when the script ends, the
Apache::DBI module's reference to it does not, keep the connection open.

WARNING: Do not attempt to open a persistent database connection in the
parent process (via PerlRequire or PerlModule). If you do, children will
get a copy of this handle, causing
clashes when the handle is used by two processes at the same time. Each
child must have it's own unique connection handle.


Gilles DAROLD