You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Nick Wellnhofer <we...@aevum.de> on 2009/07/20 17:15:36 UTC

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

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/

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

Posted by Jiri Pavlovsky <ji...@getnet.cz>.
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

Posted by Perrin Harkins <ph...@gmail.com>.
2009/8/5 Tomáš Bažant <tb...@suse.cz>:
> 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

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

Posted by Perrin Harkins <ph...@gmail.com>.
2009/7/21 Tomáš Bažant <tb...@suse.cz>:
> 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).

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

mod_perl / CGI.pm and 'our' variables

Posted by Tomáš Bažant <tb...@suse.cz>.
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 <tb...@suse.cz>
Novell, SUSE Linux s.r.o.


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

Posted by Adam Prime <ad...@utoronto.ca>.
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
> 
> 
> 


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

Posted by Adam Prime <ad...@utoronto.ca>.
Fred Moyer wrote:
> On Mon, Jul 20, 2009 at 8:15 AM, Nick Wellnhofer<we...@aevum.de> 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.
> 
> Added a check for the eval and warn, and patch to Changes.  I was
> trying to think of a good way to test this, but Apache::DBI isn't
> setup with Apache::Test yet.
> 
> +1 here, all current tests passing.
> 

I tested it using the debug function that everything else uses instead
of the warn, but other than that it looks fine.  As for testing, getting
 Apache::Test tests working would obviously take some time.

Adam

> phred@pooky ~/dev/svn/modperl/Apache-DBI/trunk $ svn diff
> Index: lib/Apache/DBI.pm
> ===================================================================
> --- lib/Apache/DBI.pm   (revision 796606)
> +++ lib/Apache/DBI.pm   (working copy)
> @@ -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,24 @@
>          }
>      }
> 
> +    # 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 +236,24 @@
>      1;
>  }
> 
> +# The PerlChildExitHandler disconnects all open connections when the
> +# httpd child exits
> +sub childexit {
> +
> +    my $prefix = "$$ Apache::DBI            ";
> +    debug(2, "$prefix PerlChildExitHandler");
> +
> +    foreach my $dbh (values(%Connected)) {
> +
> +        eval { DBI::db::disconnect($dbh) };
> +        if ($@) {
> +            warn("error disconnecting db handle: $@");
> +        }
> +    }
> +
> +    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
> Index: Changes
> ===================================================================
> --- Changes     (revision 796606)
> +++ Changes     (working copy)
> @@ -2,6 +2,9 @@
> 
>  1.08
> 
> +  - Adds PerlChildExitHandler to disconnect existing connections
> +    Submitted by: Nick Wellnhoffer <we...@aevum.de>
> +
>  1.07 05/09/2008
>    - http://rt.cpan.org/Public/Bug/Display.html?id=31003
>      Submitted by: diafour@gmail.com
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@perl.apache.org
> For additional commands, e-mail: dev-help@perl.apache.org
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@perl.apache.org
For additional commands, e-mail: dev-help@perl.apache.org


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

Posted by Fred Moyer <fr...@redhotpenguin.com>.
On Mon, Jul 20, 2009 at 8:15 AM, Nick Wellnhofer<we...@aevum.de> 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.

Added a check for the eval and warn, and patch to Changes.  I was
trying to think of a good way to test this, but Apache::DBI isn't
setup with Apache::Test yet.

+1 here, all current tests passing.

phred@pooky ~/dev/svn/modperl/Apache-DBI/trunk $ svn diff
Index: lib/Apache/DBI.pm
===================================================================
--- lib/Apache/DBI.pm   (revision 796606)
+++ lib/Apache/DBI.pm   (working copy)
@@ -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,24 @@
         }
     }

+    # 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 +236,24 @@
     1;
 }

+# The PerlChildExitHandler disconnects all open connections when the
+# httpd child exits
+sub childexit {
+
+    my $prefix = "$$ Apache::DBI            ";
+    debug(2, "$prefix PerlChildExitHandler");
+
+    foreach my $dbh (values(%Connected)) {
+
+        eval { DBI::db::disconnect($dbh) };
+        if ($@) {
+            warn("error disconnecting db handle: $@");
+        }
+    }
+
+    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
Index: Changes
===================================================================
--- Changes     (revision 796606)
+++ Changes     (working copy)
@@ -2,6 +2,9 @@

 1.08

+  - Adds PerlChildExitHandler to disconnect existing connections
+    Submitted by: Nick Wellnhoffer <we...@aevum.de>
+
 1.07 05/09/2008
   - http://rt.cpan.org/Public/Bug/Display.html?id=31003
     Submitted by: diafour@gmail.com

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@perl.apache.org
For additional commands, e-mail: dev-help@perl.apache.org