Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Singleton Objects for DBI Connection

by asafp (Novice)
on Oct 06, 2010 at 12:13 UTC ( #863769=perlquestion: print w/replies, xml ) Need Help??
asafp has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I have a web application running on Apache/mod_perl.
Each request sent to the application opens an Apache child process.
I have written a DBConnection class which connects to Oracle using DBI.
I have implemented the class as a singleton so the same connection is used till the end of the request.

The problem is that it seems that the instance of the class is not dead when the request is done
and the next request does not create a new instance but keeps using the first one.

Does it make sense? Do child processes share objects between them?

Here's my DBConnection class:
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; }

Replies are listed 'Best First'.
Re: Singleton Objects for DBI Connection
by pfaut (Priest) on Oct 06, 2010 at 18:39 UTC

    If you're using mod_perl, your code is becoming part of the web server. Apache loads up your code during initialization. Your code isn't forked, executed, and killed. Instead, it's called when needed. After it returns, it remains resident to be called again when a new request for it arrives.

    Since your code never runs down, your database connection remains open and the same connection will be used by the next request. This is one of the advantages to running under mod_perl - your module's initialization code gets executed once when Apache starts up instead of once per connection. As a result, you can get right down to processing the request which makes your application faster to respond.

    90% of every Perl application is already written.
      I think I understood.
Re: Singleton Objects for DBI Connection
by pajout (Curate) on Oct 06, 2010 at 17:35 UTC
    Firstly, is your Apache just forking processes or does threads? I have no experience with second case.

    If Apache just forks, I am mostly sure that Apache children should not share dbhs. But it is very common for mod_perl applications to keep dbh till the child lives, especially with Oracle (Oracle does not dedicate one db process for one connection necessarily). Simply, 1 Apache child == 1 dbh.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://863769]
Approved by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2017-07-26 03:52 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (383 votes). Check out past polls.