Beefy Boxes and Bandwidth Generously Provided by pair Networks Frank
P is for Practical
 
PerlMonks  

Re^2: How to share DBI connections between objects?

by adrianh (Chancellor)
on Mar 26, 2007 at 14:34 UTC ( #606589=note: print w/ replies, xml ) Need Help??


in reply to Re: How to share DBI connections between objects?
in thread How to share DBI connections between objects?

I'm pretty sure that DBI does pool connections, but i can't prove it.

The connect() method doesn't (unless you're using Apache::DBI). The connect_cached() method does.

Personally I don't like using this sort of thing since it makes it harder to inject things like test databases in a particular context. I prefer to pass in a configuration object to my application that points to the database, that way I can just build custom config objects for testing.


Comment on Re^2: How to share DBI connections between objects?
Re^3: How to share DBI connections between objects?
by f00li5h (Chaplain) on Mar 27, 2007 at 03:21 UTC

    I'm not sure I understand. I presume you're objecting to My->Db, right?

    So instead something more like this?

    package My::Underling; our $Db; sub import { my($class, $config_ref )=@_; $Db = DBI->connect( @{$config_ref->{connect_args} } ); } # ... else where in the code import My::Underling \%global_config;
    @_=qw; ask f00li5h to appear and remain for a moment of pretend better than a lifetime;;s;;@_[map hex,split'',B204316D8C2A4516DE];;y/05/os/&print;
      I'm not sure I understand. I presume you're objecting to My->Db, right?

      Kinda. I'm objecting to their being one hard coded $dbh that everything uses. This makes it harder to, for example, swap in a test database, or maybe decide to split a system over multiple databases.

      This isn't really just an issue of database handles, but all "shared" configuration information.

      Instead I pass configuration objects in at object construction time, or use factories like Rose::DB to allow me to switch things around more easily.

        I've been meaning to read up on Rose::DB, so this seems a sensible time to do it.

        In my implementation My->Db did actually get it's connect info from My->get_config('db'); but that still does not allow individual things within My to get connections to other databases, or to allow for injecting of mock database objects for testing without nastyness.

        hrm, good thing you mentioned it adrianh, or I'd just have kept plodding along with this implementation.

        @_=qw; ask f00li5h to appear and remain for a moment of pretend better than a lifetime;;s;;@_[map hex,split'',B204316D8C2A4516DE];;y/05/os/&print;

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://606589]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (7)
As of 2014-04-19 00:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (473 votes), past polls