Go to the documentation of this file. 1 """SimplePooledDB - a very simple DB-API 2 database connection pool.
3 Implements a pool of threadsafe cached DB-API 2 connections
4 to a database which are transparently reused.
6 This should result in a speedup for persistent applications
7 such as the "Webware for Python" AppServer.
9 For more information on the DB-API 2, see:
10 https://www.python.org/dev/peps/pep-0249/
11 For more information on Webware for Python, see:
12 https://webwareforpython.github.io/w4py/
14 Measures are taken to make the pool of connections threadsafe
15 regardless of whether the DB-API 2 module used is threadsafe
16 on the connection level (threadsafety > 1) or not. It must only
17 be threadsafe on the module level (threadsafety = 1). If the
18 DB-API 2 module is threadsafe, the connections will be shared
19 between threads (keep this in mind if you use transactions).
23 The idea behind SimplePooledDB is that it's completely transparent.
24 After you have established your connection pool, stating the
25 DB-API 2 module to be used, the number of connections
26 to be cached in the pool and the connection parameters, e.g.
28 import pgdb # import used DB-API 2 module
29 from dbutils.simple_pooled_db import PooledDB
30 dbpool = PooledDB(pgdb, 5, host=..., database=..., user=..., ...)
32 you can demand database connections from that pool,
34 db = dbpool.connection()
36 and use them just as if they were ordinary DB-API 2 connections.
37 It's really just a proxy class.
39 db.close() will return the connection to the pool, it will not
40 actually close it. This is so your existing code works nicely.
42 Ideas for improvement:
44 * Do not create the maximum number of connections on startup
45 already, but only a certain number and the rest on demand.
46 * Detect and transparently reset "bad" connections.
47 * Connections should have some sort of maximum usage limit
48 after which they should be automatically closed and reopened.
49 * Prefer or enforce thread-affinity for the connections,
50 allowing for both shareable and non-shareable connections.
52 Please note that these and other ideas have been already
53 implemented in in PooledDB, a more sophisticated version
54 of SimplePooledDB. You might also consider using PersistentDB
55 instead for thread-affine persistent database connections.
56 SimplePooledDB may still serve as a very simple reference
57 and example implementation for developers.
60 Copyright, credits and license:
62 * Contributed as MiscUtils/DBPool for Webware for Python
63 by Dan Green, December 2000
64 * Thread safety bug found by Tom Schwaller
65 * Fixes by Geoff Talvola (thread safety in _threadsafe_getConnection())
66 * Clean up by Chuck Esterbrook
67 * Fix unthreadsafe functions which were leaking, Jay Love
68 * Eli Green's webware-discuss comments were lifted for additional docs
69 * Clean-up and detailed commenting, rename and move to DBUtils
70 by Christoph Zwerschke in September 2005
72 Licensed under the MIT license.
75 from .
import __version__
79 """General PooledDB error."""
83 """DB-API module not supported by PooledDB."""
87 """A proxy class for pooled database connections.
89 You don't normally deal with this class directly,
90 but use PooledDB to get new connections.
98 """Close the pooled connection."""
101 if self.
_con is not None:
107 return getattr(self.
_con, name)
114 """A very simple database connection pool.
116 After you have created the connection pool,
117 you can get connections using getConnection().
120 version = __version__
122 def __init__(self, dbapi, maxconnections, *args, **kwargs):
123 """Set up the database connection pool.
125 dbapi: the DB-API 2 compliant module you want to use
126 maxconnections: the number of connections cached in the pool
127 args, kwargs: the parameters that shall be used to establish
128 the database connections using connect()
131 threadsafety = dbapi.threadsafety
134 if threadsafety == 0:
136 "Database module does not support any level of threading.")
137 elif threadsafety == 1:
142 from Queue
import Queue
144 from queue
import Queue
149 elif threadsafety
in (2, 3):
153 from threading
import Lock
162 "Database module threading support cannot be determined.")
165 for i
in range(maxconnections):
175 """Get a connection from the pool."""
179 """Add a connection to the pool."""
183 """Return a connection to the pool.
185 In this case, the connections need to be put
186 back into the queue after they have been used.
187 This is done automatically when the connection is closed
188 and should never be called explicitly outside of this module.
198 """Get a connection from the pool."""
212 """Add a connection to the pool."""
216 """Return a connection to the pool.
218 In this case, the connections always stay in the pool,
219 so there is no need to do anything here.
def _unthreadsafe_get_connection(self)
def __getattr__(self, name)
def __init__(self, pool, con)
def _threadsafe_return_connection(self, con)
def _threadsafe_get_connection(self)
def _unthreadsafe_return_connection(self, con)
def _unthreadsafe_add_connection(self, con)
def _threadsafe_add_connection(self, con)
def __init__(self, dbapi, maxconnections, *args, **kwargs)