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

Reply via email to