How to selectively start and end a persistent Apache::DBI for perl from the client/browser?

346 views Asked by At

This question is related to our web-based application, which uses perl, apache, Apache::DBI and a database (MySQL, SQLite, or anything).

We know that Apache::DBI is used to create persistent db connections. These connections live in memory from the time the Apache web server starts to the time it shuts down.

My Question is: Is it possible to create persistent db connections at any arbitrary time between the start and end of Apache process? We don't want to have persistent connections throughout the life of the Apache web server process.

We need to create persistent connections any time after the Apache web server is started. And we need to end persistent connections any time before the Apache web server is shut down.

1

There are 1 answers

3
benzebuth On

I'm not pretty sure about the way to do it. But i would suggest to create a module, specifically to launch your connection, use it, and end it. Inside it you get your connection as a scalar, say $dbh, that will be common to everyone calling DBI functions (ùaking request to your mysql server).


package myBDDConnection;
use DBI;
our @EXPORT_OK = qw(&Query);

our $dbh = Connect();

sub Connect(){
my $dbh = DBI->connect(...);
...
return $dbh;
}
sub Query() {
 if(!$dbh) {$dbh=Connect()}
  //then perform query
}

sub Close(){
 $dbh->close() //or finish, i'm not sure
}

then, in your other modules, you import your myBDDConnection, and perform queries through the Query function from the previous module.