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

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

am 20.07.2009 17:15:36 von wellnhofer

This is a multi-part message in MIME format.
--------------060709030705060906050402
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit


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/

--------------060709030705060906050402
Content-Type: text/plain;
name="dbi.diff"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
filename="dbi.diff"

--- DBI.pm.bak 2009-07-20 16:48:35.000000000 +0200
+++ DBI.pm 2009-07-20 17:01:15.000000000 +0200
@@ -39,6 +39,8 @@
# 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;
@@ -134,6 +136,23 @@
}
}

+ # 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
@@ -216,6 +235,19 @@
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

--------------060709030705060906050402--

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

am 21.07.2009 01:18:19 von Adam Prime

Seems like a reasonable addition to me. Apache::DBI 1.07 is kind of
broken right now (see bugs in RT), it'd be nice to get a new release
with this fixed and some of those bugs closed.

Adam


Nick Wellnhofer wrote:
> 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
>
>
>

mod_perl / CGI.pm and "our" variables

am 21.07.2009 09:29:36 von tbazant

Hi

i'm new here and have a problem i have not been able to solve for 2 days
now:

i'm writing a web application using %SUBJ% and would like to set 'our
$dbh = ...' so that i do not need to pass db handler as an argument to
functions in different packages every time i need. but this 'global'
variable is not seen inside imported package as $::dbh (as it is in
normal non-mod_perl application).

Any idea?



--
Tomáš Bažant
Novell, SUSE Linux s.r.o.

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

am 21.07.2009 21:39:50 von jira

Nick Wellnhofer napsal(a):
> 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.
>


Thanks, it made my Postgres log useful again.

Re: mod_perl / CGI.pm and "our" variables

am 27.07.2009 16:37:20 von Perrin Harkins

2009/7/21 Tomáš Bažant :
> i'm writing a web application using %SUBJ% and would like to set 'our
> $dbh =3D ...' so that i do not need to pass db handler as an argument to
> functions in different packages every time i need. but this 'global'
> variable is not seen inside imported package as $::dbh (as it is in
> normal non-mod_perl application).

The scoping of variables is not changed by mod_perl. When you declare
a package variable, you should be able to see it from other packages,
but you won't be able to refer to it as $dbh except in the package
where you declared it.

Example:

package Foo;
our $dbh;

$dbh->ping; # works

package Bar;
$dbh->ping; # fails

You can still use the fully-qualified name to get at it: $Foo::dbh.

- Perrin

Re: mod_perl / CGI.pm and "our" variables

am 05.08.2009 16:45:34 von Perrin Harkins

2009/8/5 Tomáš Bažant :
> yest that works for me if all the packages are in the same file, but if
> i put the Bar package into a separate file and import it with 'use'
> directive, $::dbh always returns undef. or could it be that i am using a
> closure? i'll try to avoid closures...

It's not a closure. You just need to understand Perl's variable
naming and package rules a little better. If there's something
specific you want to show us, we could probably spot the scoping
problem.

- Perrin