On Mon, 15 Jan 2001, Vivek Khera wrote:
> I tend to write my apps in a modular fashion, so that each module
> connects to the database and fetches its data and then disconnects.
> Often, a program will require data from several modules resulting in a
> lot of wasted connect/disconnect ops.  Apache::DBI does solve this
> issue, but I don't want nor need to keep lingering connections for a
> lightly-used application.  (The DB is not used for the majority of
> hits to the site.)

I use a singleton for the database connection:

sub get_dbh {
  my $dbh = Apache->request()->pnotes('dbh');
  if (!$dbh) {
    $dbh = _new_database_connection();
    Apache->request()->pnotes('dbh', $dbh);
  }
  return $dbh;
}

Using pnotes is better than using a global, because mod_perl will make
sure it gets cleaned up after the request even if something goes wrong.

You can use Apache::DBI with this or not, without changing the code.

> My guess is that my handler will clearnup the cache before
> Apache::DBI's handler gets a chance to auto-rollback.

You might get some errors about undefined methods and such from that.

- Perrin

Reply via email to