Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

comment on

( #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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others chilling in the Monastery: (3)
    As of 2020-08-12 03:53 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      Which rocket would you take to Mars?










      Results (64 votes). Check out past polls.

      Notices?