You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Clinton Gormley <cl...@drtech.co.uk> on 2002/11/05 21:11:42 UTC
When using Apache::DBI...
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.
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:
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
This way, we're not expecting the DBI handle to die during a request,
and so avoid calling a few lines of code to ->connect and
->prepare_cached several times during each request.
Have I got the right idea?
thanks
Clint
Re: When using Apache::DBI...
Posted by Perrin Harkins <pe...@elem.com>.
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.
>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)
>
It also adds a cleanup handler to issue a rollback of any uncommitted
work on that database handle after the request completes.
>So would this be the right balance between efficiency and safety:
>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
>
Yes. The call to prepare_cached won't do anything if that statement is
already cached.
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