perlquestion
asafp
Hi Monks,
<p>I have a web application running on Apache/mod_perl.<br/>
Each request sent to the application opens an Apache child process.<br/>
I have written a DBConnection class which connects to Oracle using DBI.<br/>I have implemented the class as a singleton so the same connection is used till the end of the request.</p>
<p>The problem is that it seems that the instance of the class is not dead when the request is done<br/> and the next request does not create a new instance but keeps using the first one.</p>
<p>Does it make sense? Do child processes share objects between them?</p>
Here's my DBConnection class:<br/>
<code>
package DBConnection;
my $dbm;
#==========#
sub new {
#==========#
my($class, $name) = @_;
my $self = { name => $name };
bless($self, $class);
return $self;
}
#============#
sub instance
#============#
{
my ($class)= @_;
if(not defined $dbm)
{
$dbm = new DBConnection();
}
return $dbm;
}
#==========================#
sub getSingleConnection
#==========================#
{
my ($self) = @_;
unless($dbm->{dbh})
{
$dbm->{dbh} = $self->getConnection();
}
return $dbm->{dbh};
}
#==================#
sub getConnection
#==================#
{
my ($self) = @_;
my $dbh;
eval {
$dbh = DBI->connect('dbi:Oracle:host=dbserver;sid=db_sid;port=1521','user','passwrd');
or return undef;
};
warn $@ if $@;
return undef if $@;
return $dbh;
}
</code>
Thanks,<br/>
Asaf