DBUtils.SimplePooledDB (version 1.1, $Date: 2011-08-14 13:57:11 +0200 (So, 14. Aug 2011) $)
SimplePooledDB - a very simple DB-API 2 database connection pool.
Implements a pool of threadsafe cached DB-API 2 connections
to a database which are transparently reused.
This should result in a speedup for persistent applications
such as the "Webware for Python" AppServer.
For more information on the DB-API 2, see:
For more information on Webware for Python, see:
Measures are taken to make the pool of connections threadsafe
regardless of whether the DB-API 2 module used is threadsafe
on the connection level (threadsafety > 1) or not. It must only
be threadsafe on the module level (threadsafety = 1). If the
DB-API 2 module is threadsafe, the connections will be shared
between threads (keep this in mind if you use transactions).
The idea behind SimplePooledDB is that it's completely transparent.
After you have established your connection pool, stating the
DB-API 2 module to be used, the number of connections
to be cached in the pool and the connection parameters, e.g.
import pgdb # import used DB-API 2 module
from DBUtils.SimplePooledDB import PooledDB
dbpool = PooledDB(pgdb, 5, host=..., database=..., user=..., ...)
you can demand database connections from that pool,
db = dbpool.connection()
and use them just as if they were ordinary DB-API 2 connections.
It's really just a proxy class.
db.close() will return the connection to the pool, it will not
actually close it. This is so your existing code works nicely.
Ideas for improvement:
* Do not create the maximum number of connections on startup
already, but only a certain number and the rest on demand.
* Detect and transparently reset "bad" connections.
* Connections should have some sort of maximum usage limit
after which they should be automatically closed and reopened.
* Prefer or enforce thread-affinity for the connections,
allowing for both sharable and non-sharable connections.
Please note that these and other ideas have been already
implemented in in PooledDB, a more sophisticated version
of SimplePooledDB. You might also consider using PersistentDB
instead for thread-affine persistent database connections.
SimplePooledDB may still serve as a very simple reference
and example implementation for developers.
Copyright, credits and license:
* Contributed as MiscUtils/DBPool for Webware for Python
by Dan Green, December 2000
* Thread safety bug found by Tom Schwaller
* Fixes by Geoff Talvola (thread safety in _threadsafe_getConnection())
* Clean up by Chuck Esterbrook
* Fix unthreadsafe functions which were leaking, Jay Love
* Eli Green's webware-discuss comments were lifted for additional docs
* Clean-up and detailed commenting, rename and move to DBUtils
by Christoph Zwerschke in September 2005
Licensed under the Open Software License version 2.1.
|__date__ = '$Date: 2011-08-14 13:57:11 +0200 (So, 14. Aug 2011) $'|
__revision__ = '$Rev: 8218 $'
__version__ = '1.1'