in reply to Using DBI handle in several packages

Depends on how much opening and closing of the database there would be on the present typical execution and how long your application is open versus how long you actually need the database open (I usually find that database operations are the slowest part of an application, so you may as well keep a connection open). But one possibility is to have one module actually open and close the database, and and have the rest of the modules use that module's db handle.

Have every module do a 'my $dbh = MyDBI->connect(...)' and in your MyDBI module, the first call actually connects while subsequent calls just reuse the same handle (stored perhaps in a package global variable).

The disconnect would have to be at the end of the application I suppose.

Update: I take it all (well, most of it) back. Use connect_cached() and just let every module connect, just don't disconnect until the end of the app.

Another update:Looking at the docs for connect_cached(), I'm not sure the author really recommends it, so maybe my first suggestion is the way to go for production code.

  • Comment on Re: Using DBI handle in several packages

Replies are listed 'Best First'.
Re: Re: Using DBI handle in several packages
by Beatnik (Parson) on Jun 03, 2001 at 02:09 UTC
    Altho connect_cached() looks nice, I'm still not sure I should be using it.
    Programming the Perl DBI lists it as (NEW) and also states 'it can also cause problems and should be used with care. The exact behaviour of this method is liable to change'.

    To answer your question (which you then took back), the number of requests made depends on the usage of the application.

    Greetz
    Beatnik
    ... Quidquid perl dictum sit, altum viditur.