Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

persistent DB connection

by Anonymous Monk
on Dec 09, 2008 at 19:39 UTC ( [id://729252]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I'm creating a wxPerl app that will need a persistent DB connection. Ideally I'd like to connect once and reference the DB handle throughout the app.

1. It looks like using DBI's connect_cached method will maintain a persistent connection. Is this the best method to use?

Replies are listed 'Best First'.
Re: persistent DB connection
by almut (Canon) on Dec 09, 2008 at 19:51 UTC

    Typically, a DB(I) connection will remain open until you close (disconnect) it. In other words, not closing it before exiting the application should be sufficient to achieve persistence...  (In case you're using a lexical variable to hold the db handle, make sure it doesn't unintentionally go out of scope, or else the object's DESTROY method will automatically disconnect.)

    Update: In this particular case, where there will likely be only one set of connect() parameters, I don't really see any significant advantage of using connect_cached() over using a plain connect() plus simply keeping around and 'reusing' the obtained $dbh yourself... — Except maybe for the implicit ping being done behind the scenes, which might guard you (to some degree) against the rare case that the connection has disappeared in the meantime (due to network problems, DB restart or whatever). This might make more sense for a web app where the server part keeps running for months, but for the typical usage scenario of a standalone wxPerl GUI it seems less vital...

    In other words, if anyone feels like pointing out the specific advantages of using connect_cached(), please go ahead!

Re: persistent DB connection
by kyle (Abbot) on Dec 09, 2008 at 19:58 UTC

    This is something Apache::DBI does. If you want to do it yourself, you could look at that source for ideas. Basically it holds the database handle for you. When you ask for it, it checks (with the $dbh->ping method) to see that it's still alive and reconnects it if necessary before giving it back to you.

      The DBI->connect_cached method does all that too. Apache::DBI adds some web-specific stuff, like disabling disconnect() for porting of old CGI scripts, avoiding caching during server startup, and rolling back any unfinished transactions that could be left at the end of a request if your code died.
Re: persistent DB connection
by fmerges (Chaplain) on Dec 10, 2008 at 07:54 UTC

    Hi,

    Regardless of the strategy you gonna use, connect_cached or having your own recovery, encapsulate it in your own module instead of directly calling DBI module.

    Regards,

    fmerges at irc.freenode.net

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://729252]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2025-11-16 22:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your view on AI coding assistants?





    Results (72 votes). Check out past polls.

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.