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

I know this was discussed just recently and I apologize for not being able to find the thread it was in.

I've a CGI script that uses DBI and accesses a dbase via OO methods. The dbh is currently being re-created on every dbase access. I believe I recall seeing a monk advise a fellow monk to use an anonymous subroutine to create, store, and access the handle via an eval block.

Do I recall correctly? Is this possible/advisable in a CGI session? Won't the dbh go out of scope anyway?

coreolyn Duct tape devotee.

  • Comment on Reusing a dbh from a CGI script via OO method

Replies are listed 'Best First'.
Re: Reusing a dbh from a CGI script via OO method
by merlyn (Sage) on Aug 14, 2000 at 20:22 UTC
    Every CGI invocation is a separate DB connection. If you are connecting more than once per invocation, you can save some time there. If you want to hold the connection longer than a specific CGI invocation, you need to run under mod_perl, or perhaps use a proxy connection, as described in DBI::ProxyServer and DBD::Proxy. (I'm planning on writing a WT column soon that shows how to do this. Maybe even today, since one is due!)

    -- Randal L. Schwartz, Perl hacker

      That's what I thought. But I've thought wrong before. Thanks for firming that up, and I'll look forward to that article :)

      coreolyn Duct tape devotee.

RE: Reusing a dbh from a CGI script via OO method
by steveAZ98 (Monk) on Aug 15, 2000 at 00:02 UTC
    There is some good information on using mod_perl to maintain persistant database connections in Stas Bekman's mod_perl Guide here
    HTH
RE (tilly) 1: Reusing a dbh from a CGI script via OO method
by tilly (Archbishop) on Aug 15, 2000 at 10:17 UTC
    The thread you wanted is need help developing fly OO style.

    As merlyn said, in a CGI script you cannot get much of a win if any. mod_perl gives a bigger one.

    BTW I am waiting for a chance to honestly disagree with merlyn where I don't suspect I am simply wrong. I have not found it yet, but someday...I will probably find I was wrong after all! :-)

      BTW I am waiting for a chance to honestly disagree with merlyn where I don't suspect I am simply wrong. I have not found it yet, but someday...I will probably find I was wrong after all! :-)
      Well, you might be surprised at how much I have to keep learning in a day in order to appear that I know it all. {grin} So, one day, that new information will come from you. It's inevitable.

      -- Randal L. Schwartz, Perl hacker