简体   繁体   中英

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM