Java Mailing List Archive

http://www.apache-httpd.com/

Home » modperl.perl »

[PATCH] Apache::DBI - Disconnect on child exit

Nick Wellnhofer

2009-07-20

Replies: Find Java Web Hosting

Author LoginPost Reply

Currently, Apache::DBI never disconnects from the database. This leads
to the following warnings in my Postgres logs every time an Apache child
exits:

LOG: unexpected EOF on client connection

Depending on MaxRequestsPerChild this can really spam your Postgres logs.

Attached is a patch against Apache::DBI that adds a PerlChildExitHandler
in order to disconnect all existing database connections.

Comments are welcome.

Nick


--
aevum gmbh
rumfordstr. 4
80469 münchen
germany

tel: +49 89 3838 0653
http://aevum.de/
--- DBI.pm.bak  2009-07-20 16:48:35.000000000 +0200
+++ DBI.pm  2009-07-20 17:01:15.000000000 +0200
@@(protected) @@
                     #  a negative value de-activates ping,
                     #  default = 0
my %LastPingTime;          # keeps track of last ping per data_source
+my $ChildExitHandlerInstalled; # set to true on installation of
+                     # PerlChildExitHandler

# Check to see if we need to reset TaintIn and TaintOut
my $TaintInOut = ($DBI::VERSION >= 1.31) ? 1 : 0;
@@(protected) @@
     }
  }

+   # this PerlChildExitHandler is supposed to disconnect all open
+   # connections to the database
+   if (!$ChildExitHandlerInstalled) {
+     $ChildExitHandlerInstalled = 1;
+     my $s;
+     if (MP2) {
+        $s = Apache2::ServerUtil->server;
+     }
+     elsif (Apache->can('push_handlers')) {
+        $s = 'Apache';
+     }
+     if ($s) {
+        debug(2, "$prefix push PerlChildExitHandler");
+        $s->push_handlers(PerlChildExitHandler => \&childexit);
+     }
+   }
+
  # this PerlCleanupHandler is supposed to initiate a rollback after the
  # script has finished if AutoCommit is off. however, cleanup can only
  # be determined at end of handle life as begin_work may have been called
@@(protected) @@
  1;
}

+# The PerlChildExitHandler disconnects all open connections
+sub childexit {
+
+   my $prefix = "$$ Apache::DBI        ";
+   debug(2, "$prefix PerlChildExitHandler");
+
+   foreach my $dbh (values(%Connected)) {
+     eval { DBI::db::disconnect($dbh) };
+   }
+
+   1;
+}
+
# The PerlCleanupHandler is supposed to initiate a rollback after the script
# has finished if AutoCommit is off.
# Note: the PerlCleanupHandler runs after the response has been sent to
©2008 apache-httpd.com - Jax Systems, LLC, U.S.A.