Skip to content

Commit 48383aa

Browse files
committed
Added a version attribute and new configuration parameters "failures" and "closeable".
1 parent e4765fe commit 48383aa

19 files changed

Lines changed: 180 additions & 85 deletions

DBUtils/Docs/UsersGuide.de.txt

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -263,14 +263,21 @@ Parameter angeben müssen:
263263
erzeugt, oder ein DB-API-2-Datenbankadapter-Modul
264264

265265
* ``maxusage``: Obergrenze dafür, wie oft eine einzelne Verbindung
266-
wiederverwendet werden darf (der Standardwert ``0`` oder ``False``
266+
wiederverwendet werden darf (der Standardwert ``0`` oder ``None``
267267
bedeutet unbegrenzte Wiederverwendung)
268268

269269
Sobald diese Obergrenze erreicht wird, wird die Verbindung zurückgesetzt.
270270

271271
* ``setsession``: eine optionale Liste von SQL-Befehlen zur Initialisierung
272272
der Datenbanksitzung, z.B. ``["set datestyle to german", ...]``
273273

274+
* ``failures``: eine optionale Exception-Klasse oder ein Tupel von Exceptions
275+
bei denen die Ausfallsicherung zum Tragen kommen soll, falls die Vorgabe
276+
(OperationalError, InternalError) nicht geeignet sein sollte
277+
278+
* ``closeable``: wenn dies auf einen wahren Wert gesetzt wird, dann wird das
279+
Schließen von Verbindungen erlaubt, normalerweise wird es jedoch ignoriert
280+
274281
* Die als ``creator`` angegebene Funktion oder die Funktion ``connect``
275282
des DB-API-2-Datenbankadapter-Moduls erhalten alle weiteren Parameter,
276283
wie ``host``, ``database``, ``user``, ``password`` usw. Sie können einige
@@ -298,8 +305,8 @@ Wenn Sie eine solche persistente Verbindung mit ``db.close()`` schließen,
298305
wird dies stillschweigend ignoriert, denn sie würde beim nächsten Zugriff
299306
sowieso wieder geöffnet, und das wäre nicht im Sinne persistenter Verbindungen.
300307
Stattdessen wird die Verbindung automatisch dann geschlossen, wenn der Thread
301-
endet. Sie können dieses Verhalten ändern, indem Sie ``persist._closeable``
302-
auf ``True`` setzen.
308+
endet. Sie können dieses Verhalten ändern, indem Sie den Parameter namens
309+
``closeable`` setzen.
303310

304311
PooledDB
305312
--------
@@ -330,12 +337,12 @@ Parameter angeben müssen:
330337
bedeutet unbegrenzte Anzahl von Datenbankverbindungen)
331338

332339
* ``blocking``: bestimmt das Verhalten, wenn diese Obergrenze überschritten
333-
wird (der Standardwert ``0`` oder ``False`` bedeutet, dass eine Fehlermeldung
334-
ausgegeben wird, andernfalls wird blockiert und gewartet, bis die Anzahl
335-
an Datenbankverbindungen wieder abnimmt)
340+
wird (der Standardwert ``0`` oder ein nicht wahrer Wert bedeutet, dass eine
341+
Fehlermeldung ausgegeben wird, andernfalls wird blockiert und gewartet,
342+
bis die Anzahl an Datenbankverbindungen wieder abnimmt)
336343

337344
* ``maxusage``: Obergrenze dafür, wie oft eine einzelne Verbindung
338-
wiederverwendet werden darf (der Standardwert ``0`` oder ``False``
345+
wiederverwendet werden darf (der Standardwert ``0`` oder ``None``
339346
bedeutet unbegrenzte Wiederverwendung)
340347

341348
Sobald diese Obergrenze erreicht wird, wird die Verbindung automatisch
@@ -344,6 +351,10 @@ Parameter angeben müssen:
344351
* ``setsession``: eine optionale Liste von SQL-Befehlen zur Initialisierung
345352
der Datenbanksitzung, z.B. ``["set datestyle to german", ...]``
346353

354+
* ``failures``: eine optionale Exception-Klasse oder ein Tupel von Exceptions
355+
bei denen die Ausfallsicherung zum Tragen kommen soll, falls die Vorgabe
356+
(OperationalError, InternalError) nicht geeignet sein sollte
357+
347358
* Die als ``creator`` angegebene Funktion oder die Funktion ``connect``
348359
des DB-API-2-Datenbankadapter-Moduls erhalten alle weiteren Parameter,
349360
wie ``host``, ``database``, ``user``, ``password`` usw. Sie können einige

DBUtils/Docs/UsersGuide.txt

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -243,13 +243,20 @@ of ``PersistentDB``, passing the following parameters:
243243
connection objects or a DB-API 2 compliant database module
244244

245245
* ``maxusage``: the maximum number of reuses of a single connection
246-
(the default of ``0`` or ``False`` means unlimited reuse)
246+
(the default of ``0`` or ``None`` means unlimited reuse)
247247

248248
Whenever the limit is reached, the connection will be reset.
249249

250250
* ``setsession``: an optional list of SQL commands that may serve to
251251
prepare the session, e.g. ``["set datestyle to german", ...]``
252252

253+
* ``failures``: an optional exception class or a tuple of exception classes
254+
for which the connection failover mechanism shall be applied,
255+
if the default (OperationalError, InternalError) is not adequate
256+
257+
* ``closeable``: if this is set to true, then closing connections will
258+
be allowed, but by default this will be silently ignored
259+
253260
* The creator function or the connect function of the DB-API 2 compliant
254261
database module specified as the creator will receive any additional
255262
parameters such as the host, database, user, password etc. You may
@@ -276,7 +283,7 @@ Closing a persistent connection with ``db.close()`` will be silently
276283
ignored since it would be reopened at the next usage anyway and
277284
contrary to the intent of having persistent connections. Instead,
278285
the connection will be automatically closed when the thread dies.
279-
You can change this behavior be setting ``persist._closeable`` to ``True``.
286+
You can change this behavior be setting the ``closeable`` parameter.
280287

281288
PooledDB
282289
--------
@@ -303,18 +310,22 @@ following parameters:
303310
(the default value of ``0`` means any number of connections)
304311

305312
* ``blocking``: determines behavior when exceeding the maximum
306-
(the default of ``0`` or ``False`` means report an error; otherwise
307-
block and wait until the number of connections decreases)
313+
(the default of ``0`` or any false value means report an error;
314+
otherwise block and wait until the number of connections decreases)
308315

309316
* ``maxusage``: maximum number of reuses of a single connection
310-
(the default of ``0`` or ``False`` means unlimited reuse)
317+
(the default of ``0`` or ``None`` means unlimited reuse)
311318

312319
When this maximum usage number of the connection is reached,
313320
the connection is automatically reset (closed and reopened).
314321

315322
* ``setsession``: an optional list of SQL commands that may serve to
316323
prepare the session, e.g. ``["set datestyle to german", ...]``
317324

325+
* ``failures``: an optional exception class or a tuple of exception classes
326+
for which the connection failover mechanism shall be applied,
327+
if the default (OperationalError, InternalError) is not adequate
328+
318329
* The creator function or the connect function of the DB-API 2 compliant
319330
database module specified as the creator will receive any additional
320331
parameters such as the host, database, user, password etc. You may

DBUtils/Examples/DBUtilsExample.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ def configFilename(self):
3232
primary key(name, seminar),
3333
foreign key (seminar) references seminars(id) on delete cascade)''')
3434

35+
3536
class DBUtilsExample(ExamplePage):
3637
"""Example page for the DBUtils package."""
3738

DBUtils/PersistentDB.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
creator: either an arbitrary function returning new DB-API 2
3333
connection objects or a DB-API 2 compliant database module
3434
maxusage: the maximum number of reuses of a single connection
35-
(the default of 0 or False means unlimited reuse)
35+
(the default of 0 or None means unlimited reuse)
3636
Whenever the limit is reached, the connection will be reset.
3737
setsession: an optional list of SQL commands that may serve to
3838
prepare the session, e.g. ["set datestyle to german", ...].
@@ -63,7 +63,7 @@
6363
ignored since it would be reopened at the next usage anyway and
6464
contrary to the intent of having persistent connections. Instead,
6565
the connection will be automatically closed when the thread dies.
66-
You can change this behavior be setting persist._closeable to True.
66+
You can change this behavior be setting the closeable parameter.
6767
6868
6969
Requirements:
@@ -111,23 +111,28 @@ class PersistentDB:
111111
112112
"""
113113

114+
version = __version__
115+
114116
def __init__(self, creator,
115-
maxusage=0, setsession=None, *args, **kwargs):
117+
maxusage=0, setsession=None, failures=None, closeable=0,
118+
*args, **kwargs):
116119
"""Set up the persistent DB-API 2 connection generator.
117120
118121
creator: either an arbitrary function returning new DB-API 2
119122
connection objects or a DB-API 2 compliant database module
120123
maxusage: maximum number of reuses of a single connection
121-
(number of database operations, 0 or False means unlimited)
124+
(number of database operations, 0 or None means unlimited)
122125
Whenever the limit is reached, the connection will be reset.
123126
setsession: optional list of SQL commands that may serve to prepare
124127
the session, e.g. ["set datestyle to ...", "set time zone ..."]
128+
failures: an optional exception class or a tuple of exception classes
129+
for which the connection failover mechanism shall be applied,
130+
if the default (OperationalError, InternalError) is not adequate
131+
closeable: if this is set to true, then closing connections will
132+
be allowed, but by default this will be silently ignored
125133
args, kwargs: the parameters that shall be passed to the creator
126134
function or the connection constructor of the DB-API 2 module
127135
128-
Set the _closeable attribute to True or 1 to allow closing
129-
connections. By default, this will be silently ignored.
130-
131136
"""
132137
try:
133138
threadsafety = creator.threadsafety
@@ -141,14 +146,17 @@ def __init__(self, creator,
141146
self._creator = creator
142147
self._maxusage = maxusage
143148
self._setsession = setsession
149+
self._failures = failures
150+
self._closeable = closeable
144151
self._args, self._kwargs = args, kwargs
145-
self._closeable = 0
146152
self.thread = local()
147153

148154
def steady_connection(self):
149155
"""Get a steady, non-persistent DB-API 2 connection."""
150156
return connect(self._creator,
151-
self._maxusage, self._setsession, *self._args, **self._kwargs)
157+
self._maxusage, self._setsession,
158+
self._failures, self._closeable,
159+
*self._args, **self._kwargs)
152160

153161
def connection(self, shareable=0):
154162
"""Get a steady, persistent DB-API 2 connection.
@@ -164,7 +172,6 @@ def connection(self, shareable=0):
164172
con = self.steady_connection()
165173
if not con.threadsafety():
166174
raise NotSupportedError("Database module is not thread-safe.")
167-
con._closeable = self._closeable
168175
self.thread.connection = con
169176
return con
170177

DBUtils/PersistentPg.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
by creating an instance of PersistentPg, passing the following parameters:
3333
3434
maxusage: the maximum number of reuses of a single connection
35-
(the default of 0 or False means unlimited reuse)
35+
(the default of 0 or None means unlimited reuse)
3636
When this maximum usage number of the connection is reached,
3737
the connection is automatically reset (closed and reopened).
3838
setsession: An optional list of SQL commands that may serve to
@@ -61,7 +61,7 @@
6161
ignored since it would be reopened at the next usage anyway and
6262
contrary to the intent of having persistent connections. Instead,
6363
the connection will be automatically closed when the thread dies.
64-
You can change this behavior be setting persist._closeable to True.
64+
You can change this behavior be setting the closeable parameter.
6565
6666
6767
Requirements:
@@ -104,40 +104,42 @@ class PersistentPg:
104104
105105
"""
106106

107-
def __init__(self, maxusage=0, setsession=None, *args, **kwargs):
107+
version = __version__
108+
109+
def __init__(self, maxusage=0, setsession=None, closeable=0,
110+
*args, **kwargs):
108111
"""Set up the persistent PostgreSQL connection generator.
109112
110113
maxusage: maximum number of reuses of a single connection
111-
(0 or False means unlimited reuse)
114+
(0 or None means unlimited reuse)
112115
When this maximum usage number of the connection is reached,
113116
the connection is automatically reset (closed and reopened).
114117
setsession: optional list of SQL commands that may serve to prepare
115118
the session, e.g. ["set datestyle to ...", "set time zone ..."]
119+
closeable: if this is set to true, then closing connections will
120+
be allowed, but by default this will be silently ignored
116121
args, kwargs: the parameters that shall be used to establish
117122
the PostgreSQL connections using class PyGreSQL pg.DB()
118123
119-
Set the _closeable attribute to True or 1 to allow closing
120-
connections. By default, this will be silently ignored.
121-
122124
"""
123125
self._maxusage = maxusage
124126
self._setsession = setsession
127+
self._closeable = closeable
125128
self._args, self._kwargs = args, kwargs
126-
self._closeable = 0
127129
self.thread = local()
128130

129131
def steady_connection(self):
130132
"""Get a steady, non-persistent PyGreSQL connection."""
131133
return SteadyPgConnection(
132-
self._maxusage, self._setsession, *self._args, **self._kwargs)
134+
self._maxusage, self._setsession, self._closeable,
135+
*self._args, **self._kwargs)
133136

134137
def connection(self):
135138
"""Get a steady, persistent PyGreSQL connection."""
136139
try:
137140
con = self.thread.connection
138141
except AttributeError:
139142
con = self.steady_connection()
140-
con._closeable = self._closeable
141143
self.thread.connection = con
142144
return con
143145

DBUtils/PooledDB.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@
4040
maxconnections: maximum number of connections generally allowed
4141
(the default value of 0 means any number of connections)
4242
blocking: determines behavior when exceeding the maximum
43-
(the default of 0 or False means report an error; otherwise
43+
(the default of 0 or false means report an error; otherwise
4444
block and wait until the number of connections decreases)
4545
maxusage: maximum number of reuses of a single connection
46-
(the default of 0 or False means unlimited reuse)
46+
(the default of 0 or None means unlimited reuse)
4747
When this maximum usage number of the connection is reached,
4848
the connection is automatically reset (closed and reopened).
4949
setsession: an optional list of SQL commands that may serve to
@@ -144,10 +144,12 @@ class PooledDB:
144144
145145
"""
146146

147+
version = __version__
148+
147149
def __init__(self, creator,
148150
mincached=0, maxcached=0,
149151
maxshared=0, maxconnections=0, blocking=0,
150-
maxusage=0, setsession=None,
152+
maxusage=0, setsession=None, failures=None,
151153
*args, **kwargs):
152154
"""Set up the DB-API 2 connection pool.
153155
@@ -164,14 +166,17 @@ def __init__(self, creator,
164166
maxconnections: maximum number of connections generally allowed
165167
(0 means an arbitrary number of connections)
166168
blocking: determines behavior when exceeding the maximum
167-
(0 or False means report an error; otherwise
169+
(0 or any false value means report an error; otherwise
168170
block and wait until the number of connections decreases)
169171
maxusage: maximum number of reuses of a single connection
170-
(0 or False means unlimited reuse)
172+
(0 or None means unlimited reuse)
171173
When this maximum usage number of the connection is reached,
172174
the connection is automatically reset (closed and reopened).
173175
setsession: optional list of SQL commands that may serve to prepare
174176
the session, e.g. ["set datestyle to ...", "set time zone ..."]
177+
failures: an optional exception class or a tuple of exception classes
178+
for which the connection failover mechanism shall be applied,
179+
if the default (OperationalError, InternalError) is not adequate
175180
args, kwargs: the parameters that shall be passed to the creator
176181
function or the connection constructor of the DB-API 2 module
177182
@@ -189,6 +194,7 @@ def __init__(self, creator,
189194
self._args, self._kwargs = args, kwargs
190195
self._maxusage = maxusage
191196
self._setsession = setsession
197+
self._failures = failures
192198
if maxcached:
193199
if maxcached < mincached:
194200
maxcached = mincached
@@ -221,7 +227,8 @@ def wait():
221227
def steady_connection(self):
222228
"""Get a steady, unpooled DB-API 2 connection."""
223229
return connect(self._creator,
224-
self._maxusage, self._setsession, *self._args, **self._kwargs)
230+
self._maxusage, self._setsession, self._failures, 1,
231+
*self._args, **self._kwargs)
225232

226233
def connection(self, shareable=1):
227234
""""Get a steady, cached DB-API 2 connection from the pool.

DBUtils/PooledPg.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@
3535
maxconnections: maximum number of connections generally allowed
3636
(the default value of 0 means any number of connections)
3737
blocking: determines behavior when exceeding the maximum
38-
(the default of 0 or False means report an error; otherwise
38+
(the default of 0 or false means report an error; otherwise
3939
block and wait until the number of connections decreases)
4040
maxusage: maximum number of reuses of a single connection
41-
(the default of 0 or False means unlimited reuse)
41+
(the default of 0 or None means unlimited reuse)
4242
When this maximum usage number of the connection is reached,
4343
the connection is automatically reset (closed and reopened).
4444
setsession: an optional list of SQL commands that may serve to
@@ -125,6 +125,8 @@ class PooledPg:
125125
126126
"""
127127

128+
version = __version__
129+
128130
def __init__(self,
129131
mincached=0, maxcached=0,
130132
maxconnections=0, blocking=0,
@@ -139,10 +141,10 @@ def __init__(self,
139141
maxconnections: maximum number of connections generally allowed
140142
(0 means an arbitrary number of connections)
141143
blocking: determines behavior when exceeding the maximum
142-
(0 or False means report an error; otherwise
144+
(0 or any false value means report an error; otherwise
143145
block and wait until the number of connections decreases)
144146
maxusage: maximum number of reuses of a single connection
145-
(0 or False means unlimited reuse)
147+
(0 or None means unlimited reuse)
146148
When this maximum usage number of the connection is reached,
147149
the connection is automatically reset (closed and reopened).
148150
setsession: optional list of SQL commands that may serve to prepare
@@ -172,7 +174,7 @@ def __init__(self,
172174

173175
def steady_connection(self):
174176
"""Get a steady, unpooled PostgreSQL connection."""
175-
return SteadyPgConnection(self._maxusage, self._setsession,
177+
return SteadyPgConnection(self._maxusage, self._setsession, 1,
176178
*self._args, **self._kwargs)
177179

178180
def connection(self):

DBUtils/SimplePooledDB.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@ class PooledDB:
121121
122122
"""
123123

124+
version = __version__
125+
124126
def __init__(self, dbapi, maxconnections, *args, **kwargs):
125127
"""Set up the database connection pool.
126128

0 commit comments

Comments
 (0)