Hi,
I keep persistent db connections and would like to
explicitly disconnect db when killing httpd.
I have custom module where I initialize db connection, prepare sth's
( will use them in Mason components ) once for per children
and several methods which have deal with my database. Here I have
DESTROY method which supposed to disconnect all connections.
sub DESTROY {
my $self= shift;
$self->disconnect;
warn "Db connection finished $self ...\n";
}
$self->disconnect is a sub which finish all sth's and disconnects connection
with db. But I have nothing in error log when I kill httpd and I didn't
understand what's happens because if I comment call $self->disconnect
I see messages from DESTROY method foreach children:
Db connection finished My::DB=HASH(0x80cd240) ...
So, is't worth to try explicitly disconnect db when httpd gets killed
and how to do this using standard method like DESTROY without
catching signals and etc.
Another question:
If I keep persistent connection httpd <-> db and did some changes in
db, say dump/reload table. How to refresh existing dbh ?
Regards,
Oleg
I wrote script to emulate above. I expected warn message from DESTROY
method when script finished.
1. DESTROY is just warn
18:38[zeus]:~/app/discovery/test/bench/dbi>tt.pl
Db connection finished My::DB=HASH(0x80cd240) ...
2. DESTROY calls $self->disconnect and then warn
18:40[zeus]:~/app/discovery/test/bench/dbi>tt.pl
Nothing is there.
test script:
#!/usr/local/bin/perl
use strict;
use My::DBtest;
# use this connect string
$ENV{DBI_DSN} = "dbi:Pg(RaiseError=>1,AutoCommit=>0):dbname=discovery";
#DBI->trace(2);
my ($dbh, $count );
$dbh = My::DB->new(1);
# $dbh->disconnect;
--
Simplified version of module My/DBtest.pm
package My::DB;
$VERSION = 0.1;
use strict;
use DBI;
my %query = (
sth0 => 'select count(*) from messages',
);
my $self = {};
sub new {
my $proto = shift;
my $class = ref($proto) || $proto;
$self->{dbh} = DBI->connect() or die ("Connections fails; $!\n");
prepare_sth($self);
bless ( $self, $class);
$self;
}
sub prepare_sth {
my $self = shift;
foreach my $sth (keys %query) {
$self->{$sth} = $self->{dbh}->prepare ( $query{$sth});
}
}
sub disconnect {
my $self = shift;
$self->sth_finish;
$self->{dbh}->disconnect;
warn "disconnected ..\n";
}
sub sth_finish {
my $self = shift;
foreach my $sth (keys %query) {
$self->{$sth}->finish;
}
}
sub DESTROY {
my $self= shift;
$self->disconnect;
warn "Db connection finished $self ...\n";
}
1;
_
Oleg Bartunov, sci.researcher, hostmaster of AstroNet,
Sternberg Astronomical Institute, Moscow University (Russia)
Internet: [EMAIL PROTECTED], http://www.sai.msu.su/~megera/
phone: +007(095)939-16-83, +007(095)939-23-83