Thread: Release connections in MODPERL

Release connections in MODPERL

From
"D. Duccini"
Date:
How do we tell Postgresql, in PERL (specifically under modperl) to
shutdown and release the database handler?

basically what's happening is that the system is opening up a new handle
and holding onto it with each hit to the server until we run out of
available database connections

normally in PERL it shuts it down when the perl goes away, but in mod
perl, it doesn't go away

-duck

-----------------------------------------------------------------------------
david@backpack.com            BackPack Software, Inc.        www.backpack.com
+1 651.645.7550 voice       "Life is an Adventure.
+1 651.645.9798 fax            Don't forget your BackPack!"
-----------------------------------------------------------------------------


Re: Release connections in MODPERL

From
"Brett W. McCoy"
Date:
On Wed, 10 Jan 2001, D. Duccini wrote:

> normally in PERL it shuts it down when the perl goes away, but in mod
> perl, it doesn't go away

Yes, mod_perl keeps your code in memory, and you can't be sloppy like you
can be with plain old CGI, so you need to explicitly close your database
conenctions (in DBI, I think it's the disconnect() method).

Note also, if you are using Apache::DBI, it does some cool things like
connection pooling and stuff, specifically for use with mod_perl.

-- Brett
                                     http://www.chapelperilous.net/~bmccoy/
---------------------------------------------------------------------------
I know how to do SPECIAL EFFECTS!!


Re: Release connections in MODPERL

From
Neil Conway
Date:
On Wed, Jan 10, 2001 at 09:50:30PM -0500, Brett W. McCoy wrote:
> Note also, if you are using Apache::DBI, it does some cool things like
> connection pooling and stuff, specifically for use with mod_perl.

Actually, it simply provides persistent connections, not connection
pooling. I believe true pooling will be part of mod_perl 2.0, which
takes advantage of the new threading/memory architecture in Apache
2.0.

Neil

--
Neil Conway <neilconway@home.com>
Get my GnuPG key from: http://klamath.dyndns.org/mykey.asc
Encrypted mail welcomed

If you wish to strive for peace of soul then believe;
if you wish to be a devotee of truth, then inquire.
        -- Friedrich Nietzsche

Re: Release connections in MODPERL

From
"Brett W. McCoy"
Date:
On Wed, 10 Jan 2001, Neil Conway wrote:

> On Wed, Jan 10, 2001 at 09:50:30PM -0500, Brett W. McCoy wrote:
> > Note also, if you are using Apache::DBI, it does some cool things like
> > connection pooling and stuff, specifically for use with mod_perl.
>
> Actually, it simply provides persistent connections, not connection
> pooling. I believe true pooling will be part of mod_perl 2.0, which
> takes advantage of the new threading/memory architecture in Apache
> 2.0.

Yeah, you're right... I meant more that it was doing persistent
connections.  Thanks for the clarification.

Apache::DBI was developed by the same guy who developed the Pg.pm and
DBD::Pg modules, by the way -- Edmund Mergl.

-- Brett
                                     http://www.chapelperilous.net/~bmccoy/
---------------------------------------------------------------------------
HOLY MACRO!


Re: Release connections in MODPERL

From
"D. Duccini"
Date:
Get this, all it takes to release the connection (since PQfinish() doesn't
work and there isn't any Pg::disconnectdb() )

is to set the your handle to null !!

PERL's garbage collection picks it up and voila!  no extra processes
hanging in memory.

so if you do this:

 $DBNAME = 'template1';
 $SQLCONNECT = "dbname = " . $DBNAME;
 $SQL = Pg::connectdb($SQLCONNECT);

you can simply do this:

$SQL = null;

otherwise Apache/modperl holds onto the connection and the number of
postmaster tasks in memory grows

-----------------------------------------------------------------------------
david@backpack.com            BackPack Software, Inc.        www.backpack.com
+1 651.645.7550 voice       "Life is an Adventure.
+1 651.645.9798 fax            Don't forget your BackPack!"
-----------------------------------------------------------------------------


Re: Release connections in MODPERL

From
"D. Duccini"
Date:
> Except you mean undef, right?  There is no null in Perl.  Even so, though,
> undef does not call object destructors (i.e., DESTROY method), only
> decrements the reference count, which hopefully means it wil get garbage
> collected.


well, $SQL = null;

compiles and produces the results described.

undef would probably work as well

-----------------------------------------------------------------------------
david@backpack.com            BackPack Software, Inc.        www.backpack.com
+1 651.645.7550 voice       "Life is an Adventure.
+1 651.645.9798 fax            Don't forget your BackPack!"
-----------------------------------------------------------------------------


Re: Release connections in MODPERL

From
"Brett W. McCoy"
Date:
On Wed, 10 Jan 2001, D. Duccini wrote:

> Get this, all it takes to release the connection (since PQfinish() doesn't
> work and there isn't any Pg::disconnectdb() )

Right, because it relies on the DESTROY method to be called when the
reference goes away, but this doesn't work in mod_perl, alas.

However, in DBI, you can (and should) explicitly close the database
connection with the disconnect method, if you want it closed.

> is to set the your handle to null !!
>
> PERL's garbage collection picks it up and voila!  no extra processes
> hanging in memory.

Except you mean undef, right?  There is no null in Perl.  Even so, though,
undef does not call object destructors (i.e., DESTROY method), only
decrements the reference count, which hopefully means it wil get garbage
collected.

-- Brett
                                     http://www.chapelperilous.net/~bmccoy/
---------------------------------------------------------------------------
I know not with what weapons World War III will be fought, but World
War IV will be fought with sticks and stones.
        -- Albert Einstein


Re: Release connections in MODPERL

From
"Brett W. McCoy"
Date:
On Wed, 10 Jan 2001, D. Duccini wrote:

> > Except you mean undef, right?  There is no null in Perl.  Even so, though,
> > undef does not call object destructors (i.e., DESTROY method), only
> > decrements the reference count, which hopefully means it wil get garbage
> > collected.
>
> well, $SQL = null;
>
> compiles and produces the results described.
>
> undef would probably work as well

Just a caveat, since null is not defined in Perl:

Using null gives me either

    Unquoted string "null" may clash with future reserved word at
          pgtest line 7.

or (if using strict)

    Bareword "null" not allowed while "strict subs" in use at pgtest
          line 7

Another trick to help with mod_perl is not to make your database
connection a global variable, but a lexically scoped variable.

-- Brett
                                     http://www.chapelperilous.net/~bmccoy/
---------------------------------------------------------------------------
"All God's children are not beautiful.    Most of God's children are, in fact,
barely presentable."
-- Fran Lebowitz