On Mon, 2003-11-24 at 15:57, Jordan Lederman wrote:
> The my $dbh line above is located at the beginning of the file. When I copy it 
> to the beginning of the sub all is fixed!

Apache::DBI tries to help you by not keeping persistent copies of
database handles that you open during startup.  In this case, the
persistence was caused by you: you were creating a closure variable by
declaring $dbh as a lexical outside of your subs and then using it
within them.  That made the database connection stay alive when apche
forked, causing mayhem as multiple processes try to write to the same
socket.

> where is the proper place for this line?

You should either call DBI->connect inside each sub, pass the database
handle around to each sub that needs it, or create a singleton class
that all subs call to get a database handle.  The advantage of the
singleton is just that you don't need to have you connection info all
over the place.  Here's an example:

package Q2::DB;

use strict;
use warnings;

use Carp qw(croak);

sub get_dbh {
  my $dbh = DBI->connect(
                         "dbi:Pg:dbname=queue",
                         "queued",
                         "",
                         {
                          AutoCommit => 1,
                          RaiseError => 1,
                          PrintError => 1,
                         },
                        )
     or croak "Cannot connect to db: $DBI::errstr";
  return $dbh;
}

Then just call my $dbh = Q2::DB::get_dbh() from your subs.

- Perrin


-- 
Reporting bugs: http://perl.apache.org/bugs/
Mail list info: http://perl.apache.org/maillist/modperl.html

Reply via email to