Re: Closing DB handler with PerlCleanupHandler

2004-11-10 Thread Perrin Harkins
On Wed, 2004-11-10 at 02:37, Batara Kesuma wrote:
> > Also, why do you want to avoid persistent connections?
> 
> Because the DB is running out of memory just to keep the persistent
> connections, I just want to try it without persistent connections and
> see the result. If the overhead is not too big, I might just change it
> to use normal connections.

Okay.  I did something like that recently by adjusting the timeout on
the database side.  This was a MySQL database and we set the
wait_timeout very low (15 seconds).  If you use Apache::DBI, it just
reconnects when necessary.  If you are using your own globals to hold
persistent handles, you can get in trouble doing this.

> I tried this to disconnect the handler in PerlCleanupHandler, but it
> didn't work.
[...]
> sub handler {
> my ($r) = @_;
> our $dbh;
> if ($dbh) {
> print STDERR "Got ya!!";
> $dbh->disconnect();
> }

That won't work because this is not the same package that the other $dbh
is declared in.

> How can I disconnect 'our
> $dbh' created by scripts within ModPerl::Registry in PerlCleanupHandler?

The package that ModPerl::Registry puts your script in is the same every
time.  You can see it by printing out the value of __PACKAGE__ in your
script.  Then access it as $Full::Name::Of::Package::dbh.

If you use Apache::DBI, all of the handles up in a nice clean hash that
you can go through and disconnect in a cleanup handler.  There's also
probably a way to ask DBI what connections are open.

- Perrin


-- 
Report problems: http://perl.apache.org/bugs/
Mail list info: http://perl.apache.org/maillist/modperl.html
List etiquette: http://perl.apache.org/maillist/email-etiquette.html



Re: Closing DB handler with PerlCleanupHandler

2004-11-10 Thread Timour Ezeev
On Wed, 10 Nov 2004, Batara Kesuma wrote:
[...]
## httpd.conf

 SetHandler perl-script
 PerlResponseHandler ModPerl::Registry
 PerlOptions +ParseHeaders
 PerlCleanupHandler MyApache::Cleanup
 Options +ExecCGI

## MyApache::Cleanup.pm
package MyApache::Cleanup;
use strict;
sub handler {
   my ($r) = @_;
   our $dbh;
   if ($dbh) {
   print STDERR "Got ya!!";
   $dbh->disconnect();
   }
   return 200;
}
1;
I can't get $dbh from module that is called by PerlCleanupHandler. $dbh
is created by scripts in ModPerl::Registry. How can I disconnect 'our
$dbh' created by scripts within ModPerl::Registry in PerlCleanupHandler?
BTW, our() has a package scope and thus you are referring to different $dbh here 
IMHO.

Regards,
- Timour
--
Timour Ezeev
Pivotal Dynamics
[EMAIL PROTECTED]
V: 305.406.9904
F: 305.406.9689
--
Report problems: http://perl.apache.org/bugs/
Mail list info: http://perl.apache.org/maillist/modperl.html
List etiquette: http://perl.apache.org/maillist/email-etiquette.html


Re: Closing DB handler with PerlCleanupHandler

2004-11-10 Thread Glenn Strauss
On Wed, Nov 10, 2004 at 12:51:56PM +0900, Batara Kesuma wrote:
> Hi Stas,
> 
> > 1) use Apache::DBI
> > 
> > 2) if not, refer to:
> > http://perl.apache.org/docs/2.0/user/handlers/http.html#PerlCleanupHandler
> > http://perl.apache.org/docs/2.0/user/coding/coding.html#Getting_the_C__r__Object
> 
> 
> Thank you for the answer. I tried to use Apache::DBI with
> dbi_connect_method => 'connect'. But I have a problem here, because I
> use 'our' on $dbh so other functions can use it. It looks like:
> 
> ---
> sub show_name {
>   our $dbh;
>   my $sth = $dbh->prepare("SELECT name FROM member WHERE id=?");
>   $sth->execute(1);
>   ...
> }
>
> So even if I use dbi_connect_method => 'connect', the connection to the
> DB will be alive until the child die. I want to make the connection not
> persistent. The only way I know now is to unload Apache::DBI, then call
> $dbh->disconnect() at the end of every scripts. Is there any other more
> efficient way to do this with little change to the scripts themselves?

use strict;
our($dbh);

sub hander {
local $dbh = DBI->connect(...);
...
}

When your handler exits, the localized global goes out of scope.  Voila.
You can also force a variable out of scope with:
undef $dbh;


'my' variables are slightly faster than globals, so you are better
off performance-wise to pass around the $dbh handle than to keep it
in a global, but since the code is already written, 'local'izing is
probably the most convenient thing to do.

Cheers,
Glenn

-- 
Report problems: http://perl.apache.org/bugs/
Mail list info: http://perl.apache.org/maillist/modperl.html
List etiquette: http://perl.apache.org/maillist/email-etiquette.html



Re: Closing DB handler with PerlCleanupHandler

2004-11-09 Thread Batara Kesuma
Hi Perrin,

> It looks to me like you are creating this problem by using "our".  Why
> 
> not use "my" and pass the handle around, or put it somewhere like 
> $r->pnotes() instead?

Yes I should have used 'my' and passed the handle around :(

> Also, why do you want to avoid persistent connections?

Because the DB is running out of memory just to keep the persistent
connections, I just want to try it without persistent connections and
see the result. If the overhead is not too big, I might just change it
to use normal connections.

I tried this to disconnect the handler in PerlCleanupHandler, but it
didn't work.

## httpd.conf

  SetHandler perl-script
  PerlResponseHandler ModPerl::Registry
  PerlOptions +ParseHeaders
  PerlCleanupHandler MyApache::Cleanup
  Options +ExecCGI



## MyApache::Cleanup.pm
package MyApache::Cleanup;
use strict;
sub handler {
my ($r) = @_;
our $dbh;
if ($dbh) {
print STDERR "Got ya!!";
$dbh->disconnect();
}
return 200;
}
1;

I can't get $dbh from module that is called by PerlCleanupHandler. $dbh
is created by scripts in ModPerl::Registry. How can I disconnect 'our
$dbh' created by scripts within ModPerl::Registry in PerlCleanupHandler?

Regards,
--bk


-- 
Report problems: http://perl.apache.org/bugs/
Mail list info: http://perl.apache.org/maillist/modperl.html
List etiquette: http://perl.apache.org/maillist/email-etiquette.html



Re: Closing DB handler with PerlCleanupHandler

2004-11-09 Thread Perrin Harkins
Batara Kesuma wrote:
But I have a problem here, because I
use 'our' on $dbh so other functions can use it. It looks like:
---
sub show_name {
  our $dbh;
  my $sth = $dbh->prepare("SELECT name FROM member WHERE id=?");
  $sth->execute(1);
  ...
}
## MAIN
my $mn = MyPackage::Main::->new();
our $dbh = $mn->load_dbh();
It looks to me like you are creating this problem by using "our".  Why 
not use "my" and pass the handle around, or put it somewhere like 
$r->pnotes() instead?

Also, why do you want to avoid persistent connections?
- Perrin
--
Report problems: http://perl.apache.org/bugs/
Mail list info: http://perl.apache.org/maillist/modperl.html
List etiquette: http://perl.apache.org/maillist/email-etiquette.html


Re: Closing DB handler with PerlCleanupHandler

2004-11-09 Thread Stas Bekman
Batara Kesuma wrote:
Hi Stas,

1) use Apache::DBI
2) if not, refer to:
http://perl.apache.org/docs/2.0/user/handlers/http.html#PerlCleanupHandler
http://perl.apache.org/docs/2.0/user/coding/coding.html#Getting_the_C__r__Object

Thank you for the answer. I tried to use Apache::DBI with
dbi_connect_method => 'connect'. But I have a problem here, because I
use 'our' on $dbh so other functions can use it. It looks like:
[...]
I want to make the connection not
persistent. The only way I know now is to unload Apache::DBI, then call
$dbh->disconnect() at the end of every scripts. Is there any other more
efficient way to do this with little change to the scripts themselves?
In which case item #2 is probably the way to go.

--
__
Stas BekmanJAm_pH --> Just Another mod_perl Hacker
http://stason.org/ mod_perl Guide ---> http://perl.apache.org
mailto:[EMAIL PROTECTED] http://use.perl.org http://apacheweek.com
http://modperlbook.org http://apache.org   http://ticketmaster.com
--
Report problems: http://perl.apache.org/bugs/
Mail list info: http://perl.apache.org/maillist/modperl.html
List etiquette: http://perl.apache.org/maillist/email-etiquette.html


Re: Closing DB handler with PerlCleanupHandler

2004-11-09 Thread Batara Kesuma
Hi Stas,

> 1) use Apache::DBI
> 
> 2) if not, refer to:
> http://perl.apache.org/docs/2.0/user/handlers/http.html#PerlCleanupHandler
> http://perl.apache.org/docs/2.0/user/coding/coding.html#Getting_the_C__r__Object


Thank you for the answer. I tried to use Apache::DBI with
dbi_connect_method => 'connect'. But I have a problem here, because I
use 'our' on $dbh so other functions can use it. It looks like:

---
sub show_name {
  our $dbh;
  my $sth = $dbh->prepare("SELECT name FROM member WHERE id=?");
  $sth->execute(1);
  ...
}

## MAIN
my $mn = MyPackage::Main::->new();
our $dbh = $mn->load_dbh();
---

So even if I use dbi_connect_method => 'connect', the connection to the
DB will be alive until the child die. I want to make the connection not
persistent. The only way I know now is to unload Apache::DBI, then call
$dbh->disconnect() at the end of every scripts. Is there any other more
efficient way to do this with little change to the scripts themselves?


-- 
Report problems: http://perl.apache.org/bugs/
Mail list info: http://perl.apache.org/maillist/modperl.html
List etiquette: http://perl.apache.org/maillist/email-etiquette.html



Re: Closing DB handler with PerlCleanupHandler

2004-11-09 Thread Stas Bekman
Batara Kesuma wrote:
Hi,
How can I pass $dbh value from PerlResponseHandler to PerlCleanupHandler
to be disconnected? My scripts are running in ModPerl::Registry, and I
don't want to do $dbh->disconnect() at the end of every scripts. So I
try to write a module in PerlCleanupHandler to clean up the DB handler
at the end of every request. 
1) use Apache::DBI
2) if not, refer to:
http://perl.apache.org/docs/2.0/user/handlers/http.html#PerlCleanupHandler
http://perl.apache.org/docs/2.0/user/coding/coding.html#Getting_the_C__r__Object
--
__
Stas BekmanJAm_pH --> Just Another mod_perl Hacker
http://stason.org/ mod_perl Guide ---> http://perl.apache.org
mailto:[EMAIL PROTECTED] http://use.perl.org http://apacheweek.com
http://modperlbook.org http://apache.org   http://ticketmaster.com
--
Report problems: http://perl.apache.org/bugs/
Mail list info: http://perl.apache.org/maillist/modperl.html
List etiquette: http://perl.apache.org/maillist/email-etiquette.html


Closing DB handler with PerlCleanupHandler

2004-11-09 Thread Batara Kesuma
Hi,

How can I pass $dbh value from PerlResponseHandler to PerlCleanupHandler
to be disconnected? My scripts are running in ModPerl::Registry, and I
don't want to do $dbh->disconnect() at the end of every scripts. So I
try to write a module in PerlCleanupHandler to clean up the DB handler
at the end of every request. 

--bk


-- 
Report problems: http://perl.apache.org/bugs/
Mail list info: http://perl.apache.org/maillist/modperl.html
List etiquette: http://perl.apache.org/maillist/email-etiquette.html