Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > dbi statement handlers as package globals in mod_perl script

Reply
Thread Tools

dbi statement handlers as package globals in mod_perl script

 
 
Andrew S
Guest
Posts: n/a
 
      04-06-2005
I am working to enhance the performance of a few mod_perl scripts on
some clients' web sites. One of the optimizations that I have read
about is to store DBI statement handles as package globals, so that
they can be re-used within the same process. My concern is that
something bad may happen if/when a database connection is dropped. I
realize that Apache:BI will handle dropped connections gracefully,
but all statement handles prepared from the original database handle
will become invalid, won't they?

Here is some code:

========
use Apache:BI;
use strict;
use vars($dbh $sth);
....
$dbh ||= DBI->connect(...);
$sth ||= $dbh->prepare("select * from tbl where id = ?");
....
$sth->execute($id);
@result = $sth->fetchrow_array;
....
========

The idea here is to initialize a database handle and a statement handle
once when the script is first loaded by a process. Thereafter, any web
requests handled by the same process can re-use the statement handle.
To the best of my knowledge, this is the most optimal solution. If I
initialized the statement handle every time the code was run, then I
would incur the overhead of calling the prepare method every time. But
as I said, I am a little paranoid about what would happen to $sth if
the database connection associated with $dbh has to be re-established.

Has anyone ever used package-global statement handles successfully? If
so, can you tell me whether the above code is right. Otherwise, what
recommendations would you make?

 
Reply With Quote
 
 
 
 
Brian McCauley
Guest
Posts: n/a
 
      04-06-2005


Andrew S wrote:

> I am working to enhance the performance of a few mod_perl scripts on
> some clients' web sites. One of the optimizations that I have read
> about is to store DBI statement handles as package globals, so that
> they can be re-used within the same process. My concern is that
> something bad may happen if/when a database connection is dropped. I
> realize that Apache:BI will handle dropped connections gracefully,
> but all statement handles prepared from the original database handle
> will become invalid, won't they?


Just use prepare_cache() and let someone else worry about that.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
mod_perl installed, but mod_perl.so file not created bennett@peacefire.org Perl Misc 2 01-25-2006 10:53 PM
mod_perl - dbi - DBD:Pg performance test, old vs new Pablo S Perl 0 09-01-2004 04:21 AM
exec "statement" VS. exec "statement in globals(), locals() Ted Python 1 07-22-2004 08:51 AM
exec "statement" VS. exec "statement" in globals(), locals() tedsuzman Python 2 07-21-2004 08:41 PM
mod_perl, OO and Globals M2 Perl Misc 3 10-08-2003 11:49 PM



Advertisments