| DBUtils.SteadyDB (version 1.1, $Date: 2011-08-14 13:57:11 +0200 (So, 14. Aug 2011) $) | index /var/www/docs/Webware-1.1.1/DBUtils/SteadyDB.py |
SteadyDB - hardened DB-API 2 connections.
Implements steady connections to a database based on an
arbitrary DB-API 2 compliant database interface module.
The connections are transparently reopened when they are
closed or the database connection has been lost or when
they are used more often than an optional usage limit.
Database cursors are transparently reopened as well when
the execution of a database operation cannot be performed
due to a lost connection. Only if the connection is lost
after the execution, when rows are already fetched from the
database, this will give an error and the cursor will not
be reopened automatically, because there is no reliable way
to recover the state of the cursor in such a situation.
Connections which have been marked as being in a transaction
with a begin() call will not be silently replaced either.
A typical situation where database connections are lost
is when the database server or an intervening firewall is
shutdown and restarted for maintenance reasons. In such a
case, all database connections would become unusable, even
though the database service may be already available again.
The "hardened" connections provided by this module will
make the database connections immediately available again.
This approach results in a steady database connection that
can be used by PooledDB or PersistentDB to create pooled or
persistent connections to a database in a threaded environment
such as the application server of "Webware for Python."
Note, however, that the connections themselves may not be
thread-safe (depending on the used DB-API module).
For the Python DB-API 2 specification, see:
http://www.python.org/peps/pep-0249.html
For information on Webware for Python, see:
http://www.webwareforpython.org
Usage:
You can use the connection constructor connect() in the same
way as you would use the connection constructor of a DB-API 2
module if you specify the DB-API 2 module to be used as the
first parameter, or alternatively you can specify an arbitrary
constructor function returning new DB-API 2 compliant connection
objects as the first parameter. Passing just a function allows
implementing failover mechanisms and load balancing strategies.
You may also specify a usage limit as the second parameter
(set it to None if you prefer unlimited usage), an optional
list of commands that may serve to prepare the session as a
third parameter, the exception classes for which the failover
mechanism shall be applied, and you can specify whether is is
allowed to close the connection (by default this is true).
When the connection to the database is lost or has been used
too often, it will be transparently reset in most situations,
without further notice.
import pgdb # import used DB-API 2 module
from DBUtils.SteadyDB import connect
db = connect(pgdb, 10000, ["set datestyle to german"],
host=..., database=..., user=..., ...)
...
cursor = db.cursor()
...
cursor.execute('select ...')
result = cursor.fetchall()
...
cursor.close()
...
db.close()
Ideas for improvement:
* Alternatively to the maximum number of uses,
implement a maximum time to live for connections.
* Optionally log usage and loss of connection.
Copyright, credits and license:
* Contributed as supplement for Webware for Python and PyGreSQL
by Christoph Zwerschke in September 2005
* Allowing creator functions as first parameter as in SQLAlchemy
suggested by Ezio Vernacotola in December 2006
Licensed under the Open Software License version 2.1.
| Modules | ||||||
| ||||||
| Classes | ||||||||||||||||||||||||||||||||||
|
| ||||||||||||||||||||||||||||||||||
| Functions | ||
| ||
| Data | ||
| __date__ = '$Date: 2011-08-14 13:57:11 +0200 (So, 14. Aug 2011) $' __revision__ = '$Rev: 8218 $' __version__ = '1.1' | ||