On 01/19/2012 08:48 AM, Josh Narins wrote:
The idea of Apache::DBI is that you get to pool connections.

If you call Apache::DBI->new and there is a spare connection, you get
it, if not, one is created for you.

This isn't quite accurate. Apache::DBI doesn't do connection pooling, it manages persistent connections. It's still one connection per-process (per connection attributes) and connections aren't shared between child processes (that's where things get messy).

Apache::DBI makes sure you don't create more connections than needed and makes sure that the persistent connections are still connected before handing them back.

You almost certainly don't want one $db object being shared as a member
of a class, unless your entire program also happens to represent one DB
transaction.

This is actually a pretty common and ok pattern. Remember that execution through a request is still linear (not threaded or handled by multiple processes) so if you want your whole request to be a single transaction you can, or if you want it to auto-commit you can, or if you want it to be a mix of transactions and auto-committed statements, you can do that too.

When you use Apache::DBI and then call DBI->connect or DBI->connect_cached you'll get the same database connection every time (per process of course).

--
Michael Peters
Plus Three, LP

Reply via email to