1 """SimplePooledPg - a very simple classic PyGreSQL connection pool.
3 Implements a pool of threadsafe cached connections
4 to a PostgreSQL database which are transparently reused,
5 using the classic (not DB-API 2 compliant) PyGreSQL pg API.
7 This should result in a speedup for persistent applications
8 such as the "Webware for Python" AppServer.
10 For more information on PostgreSQL, see:
11 https://www.postgresql.org/
12 For more information on PyGreSQL, see:
13 http://www.pygresql.org
14 For more information on Webware for Python, see:
15 https://webwareforpython.github.io/w4py/
17 Measures are taken to make the pool of connections threadsafe
18 regardless of the fact that the PyGreSQL pg module itself is
19 not threadsafe at the connection level. Connections will never be
20 shared between threads, so you can safely use transactions.
24 The idea behind SimplePooledPg is that it's completely transparent.
25 After you have established your connection pool, stating the
26 number of connections to be cached in the pool and the
27 connection parameters, e.g.
29 from dbutils.simple_pooled_pg import PooledPg
30 dbpool = PooledPg(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 PyGreSQL pg API
37 connections. 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. The PyGreSQL
47 pg API provides a status attribute and a reset() method for that.
48 * Connections should have some sort of "maximum usage limit"
49 after which they should be automatically closed and reopened.
50 * Prefer or enforce thread affinity for the connections.
52 Please note that these and other ideas have been already
53 implemented in in PooledPg, a more sophisticated version
54 of SimplePooledPg. You might also consider using PersistentPg
55 instead for thread-affine persistent PyGreSQL connections.
56 SimplePooledPg may still serve as a very simple reference
57 and example implementation for developers.
60 Copyright, credits and license:
62 * Contributed as supplement for Webware for Python and PyGreSQL
63 by Christoph Zwerschke in September 2005
64 * Based on the code of DBPool, contributed to Webware for Python
65 by Dan Green in December 2000
67 Licensed under the MIT license.
70 from pg
import DB
as PgConnection
72 from .
import __version__
76 """A proxy class for pooled PostgreSQL connections.
78 You don't normally deal with this class directly,
79 but use PooledPg to get new connections.
87 """Close the pooled connection."""
90 if self.
_con is not None:
96 return getattr(self.
_con, name)
103 """A very simple PostgreSQL connection pool.
105 After you have created the connection pool,
106 you can get connections using getConnection().
109 version = __version__
111 def __init__(self, maxconnections, *args, **kwargs):
112 """Set up the PostgreSQL connection pool.
114 maxconnections: the number of connections cached in the pool
115 args, kwargs: the parameters that shall be used to establish
116 the PostgreSQL connections using pg.connect()
122 from Queue
import Queue
124 from queue
import Queue
128 for i
in range(maxconnections):
129 self.
cache(PgConnection(*args, **kwargs))
132 """Add or return a connection to the pool."""
136 """Get a connection from the pool."""