Clinton Gormley wrote:
Am I correct in this:
Apache::DBI can only really do its stuff when you perform a
DBI->connect, so by calling $dbh = DBI->connect(..) during PerlChildInit
and then never trying to reconnect, you are defeating the purpose of
using Apache::DBI.
That's right.
It also adds a cleanup handler to issue a rollback of any uncommitted work on that database handle after the request completes.To expand on this, when Apache::DBI intercepts a connection request: * it returns a stored live handle which matches the connection credentials * checks that the handle is indeed still live, and if it isn't, reconnects automatically (potentially destroying any $sth's that you have prepared and retained)
So would this be the right balance between efficiency and safety:Yes. The call to prepare_cached won't do anything if that statement is already cached.
1) use Apache::DBI
2) Do "my $dbh = DBI->connect()" at the beginning of each request (ie as
early as required during each request) 3) use "my $sth = $dbh->prepare_cached()" once as early as required
during each request
The way I like to do it is to have a utility class that implements a get_dbh() method. Then I just call that when I want one. Inside of that it can do the DBI->connect business and anything else I need done (maybe resetting the isolation level on that connection if I messed with that anywhere). It can also cache the dbh for the length of the request inside of $r->pnotes if I want to avoid multiple calls to Apache::DBI.
- Perrin