Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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; }
Thanks,
Asaf

In reply to Singleton Objects for DBI Connection by asafp

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (6)
As of 2024-05-30 21:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.