Skip to content

Commit 2557747

Browse files
committed
Added alias method "dedicated_connection" for "connection(shareable=False)". Exceptions in destructors are now caught and silently ignored. These can occur when instances are not yet fully initialized or referenced objects are already destroyed.
1 parent cf3c96a commit 2557747

14 files changed

Lines changed: 220 additions & 83 deletions

DBUtils/Docs/UsersGuide.de.html

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ <h1 class="title">Benutzeranleitung für DBUtils</h1>
1515
<col class="docinfo-content" />
1616
<tbody valign="top">
1717
<tr><th class="docinfo-name">Version:</th>
18-
<td>0.9.4</td></tr>
19-
<tr class="field"><th class="docinfo-name">Released:</th><td class="field-body">07/07/07</td>
18+
<td>1.0pre</td></tr>
19+
<tr class="field"><th class="docinfo-name">Released:</th><td class="field-body">09/25/08</td>
2020
</tr>
2121
<tr class="field"><th class="docinfo-name">Translations:</th><td class="field-body"><a class="reference" href="UsersGuide.html">English</a> German <a class="reference" href="UsersGuide.zh.html">Chinese</a></td>
2222
</tr>
@@ -302,13 +302,20 @@ <h2><a id="id1" name="id1">PersistentDB</a></h2>
302302
erzeugt, oder ein DB-API-2-Datenbankadapter-Modul</p>
303303
</li>
304304
<li><p class="first"><tt class="docutils literal"><span class="pre">maxusage</span></tt>: Obergrenze dafür, wie oft eine einzelne Verbindung
305-
wiederverwendet werden darf (der Standardwert <tt class="docutils literal"><span class="pre">0</span></tt> oder <tt class="docutils literal"><span class="pre">False</span></tt>
305+
wiederverwendet werden darf (der Standardwert <tt class="docutils literal"><span class="pre">0</span></tt> oder <tt class="docutils literal"><span class="pre">None</span></tt>
306306
bedeutet unbegrenzte Wiederverwendung)</p>
307307
<p>Sobald diese Obergrenze erreicht wird, wird die Verbindung zurückgesetzt.</p>
308308
</li>
309309
<li><p class="first"><tt class="docutils literal"><span class="pre">setsession</span></tt>: eine optionale Liste von SQL-Befehlen zur Initialisierung
310310
der Datenbanksitzung, z.B. <tt class="docutils literal"><span class="pre">[&quot;set</span> <span class="pre">datestyle</span> <span class="pre">to</span> <span class="pre">german&quot;,</span> <span class="pre">...]</span></tt></p>
311311
</li>
312+
<li><p class="first"><tt class="docutils literal"><span class="pre">failures</span></tt>: eine optionale Exception-Klasse oder ein Tupel von Exceptions
313+
bei denen die Ausfallsicherung zum Tragen kommen soll, falls die Vorgabe
314+
(OperationalError, InternalError) nicht geeignet sein sollte</p>
315+
</li>
316+
<li><p class="first"><tt class="docutils literal"><span class="pre">closeable</span></tt>: wenn dies auf <tt class="docutils literal"><span class="pre">True</span></tt> gesetzt wird, dann wird das Schließen
317+
von Verbindungen erlaubt, normalerweise wird es jedoch ignoriert</p>
318+
</li>
312319
<li><p class="first">Die als <tt class="docutils literal"><span class="pre">creator</span></tt> angegebene Funktion oder die Funktion <tt class="docutils literal"><span class="pre">connect</span></tt>
313320
des DB-API-2-Datenbankadapter-Moduls erhalten alle weiteren Parameter,
314321
wie <tt class="docutils literal"><span class="pre">host</span></tt>, <tt class="docutils literal"><span class="pre">database</span></tt>, <tt class="docutils literal"><span class="pre">user</span></tt>, <tt class="docutils literal"><span class="pre">password</span></tt> usw. Sie können einige
@@ -336,8 +343,8 @@ <h2><a id="id1" name="id1">PersistentDB</a></h2>
336343
wird dies stillschweigend ignoriert, denn sie würde beim nächsten Zugriff
337344
sowieso wieder geöffnet, und das wäre nicht im Sinne persistenter Verbindungen.
338345
Stattdessen wird die Verbindung automatisch dann geschlossen, wenn der Thread
339-
endet. Sie können dieses Verhalten ändern, indem Sie <tt class="docutils literal"><span class="pre">persist._closeable</span></tt>
340-
auf <tt class="docutils literal"><span class="pre">True</span></tt> setzen.</p>
346+
endet. Sie können dieses Verhalten ändern, indem Sie den Parameter namens
347+
<tt class="docutils literal"><span class="pre">closeable</span></tt> setzen.</p>
341348
</div>
342349
<div class="section">
343350
<h2><a id="id2" name="id2">PooledDB</a></h2>
@@ -366,20 +373,24 @@ <h2><a id="id2" name="id2">PooledDB</a></h2>
366373
die insgesamt überhaupt erlaubt werden sollen (der Standardwert <tt class="docutils literal"><span class="pre">0</span></tt>
367374
bedeutet unbegrenzte Anzahl von Datenbankverbindungen)</p>
368375
</li>
369-
<li><p class="first"><tt class="docutils literal"><span class="pre">blocking</span></tt>: bestimmt das Verhalten, wenn diese Obergrenze überschritten
370-
wird (der Standardwert <tt class="docutils literal"><span class="pre">0</span></tt> oder <tt class="docutils literal"><span class="pre">False</span></tt> bedeutet, dass eine Fehlermeldung
371-
ausgegeben wird, andernfalls wird blockiert und gewartet, bis die Anzahl
372-
an Datenbankverbindungen wieder abnimmt)</p>
376+
<li><p class="first"><tt class="docutils literal"><span class="pre">blocking</span></tt>: bestimmt das Verhalten bei Überschreitung dieser Obergrenze</p>
377+
<p>Wenn dies auf <tt class="docutils literal"><span class="pre">True</span></tt> gesetzt wird, dann wird so lange gewartet, bis die
378+
Anzahl an Datenbankverbindungen wieder abnimmt, normalerweise wird jedoch
379+
sofort eine Fehlermeldung ausgegeben.</p>
373380
</li>
374381
<li><p class="first"><tt class="docutils literal"><span class="pre">maxusage</span></tt>: Obergrenze dafür, wie oft eine einzelne Verbindung
375-
wiederverwendet werden darf (der Standardwert <tt class="docutils literal"><span class="pre">0</span></tt> oder <tt class="docutils literal"><span class="pre">False</span></tt>
382+
wiederverwendet werden darf (der Standardwert <tt class="docutils literal"><span class="pre">0</span></tt> oder <tt class="docutils literal"><span class="pre">None</span></tt>
376383
bedeutet unbegrenzte Wiederverwendung)</p>
377384
<p>Sobald diese Obergrenze erreicht wird, wird die Verbindung automatisch
378385
zurückgesetzt (geschlossen und wieder neu geöffnet).</p>
379386
</li>
380387
<li><p class="first"><tt class="docutils literal"><span class="pre">setsession</span></tt>: eine optionale Liste von SQL-Befehlen zur Initialisierung
381388
der Datenbanksitzung, z.B. <tt class="docutils literal"><span class="pre">[&quot;set</span> <span class="pre">datestyle</span> <span class="pre">to</span> <span class="pre">german&quot;,</span> <span class="pre">...]</span></tt></p>
382389
</li>
390+
<li><p class="first"><tt class="docutils literal"><span class="pre">failures</span></tt>: eine optionale Exception-Klasse oder ein Tupel von Exceptions
391+
bei denen die Ausfallsicherung zum Tragen kommen soll, falls die Vorgabe
392+
(OperationalError, InternalError) nicht geeignet sein sollte</p>
393+
</li>
383394
<li><p class="first">Die als <tt class="docutils literal"><span class="pre">creator</span></tt> angegebene Funktion oder die Funktion <tt class="docutils literal"><span class="pre">connect</span></tt>
384395
des DB-API-2-Datenbankadapter-Moduls erhalten alle weiteren Parameter,
385396
wie <tt class="docutils literal"><span class="pre">host</span></tt>, <tt class="docutils literal"><span class="pre">database</span></tt>, <tt class="docutils literal"><span class="pre">user</span></tt>, <tt class="docutils literal"><span class="pre">password</span></tt> usw. Sie können einige
@@ -409,7 +420,11 @@ <h2><a id="id2" name="id2">PooledDB</a></h2>
409420
Eine dedizierte Datenbankverbindung, die garantiert nicht von anderen Threads
410421
mitgenutzt wird, fordern Sie wie folgt an:</p>
411422
<pre class="literal-block">
412-
db = pool.connection(0)
423+
db = pool.connection(shareable=False)
424+
</pre>
425+
<p>Stattdessen können Sie eine dedizierte Verbindung auch wie folgt erhalten:</p>
426+
<pre class="literal-block">
427+
db = pool.dedicated_connection()
413428
</pre>
414429
<p>Wenn Sie die Datenbankverbindung nicht mehr benötigen, sollten Sie diese sofort
415430
wieder mit <tt class="docutils literal"><span class="pre">db.close()</span></tt> an den Pool zurückgeben. Sie können auf die gleiche

DBUtils/Docs/UsersGuide.de.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,11 @@ gesetzt haben, und der zugrunde liegende DB-API-2-Datenbankadapter dies erlaubt.
385385
Eine dedizierte Datenbankverbindung, die garantiert nicht von anderen Threads
386386
mitgenutzt wird, fordern Sie wie folgt an::
387387

388-
db = pool.connection(0)
388+
db = pool.connection(shareable=False)
389+
390+
Stattdessen können Sie eine dedizierte Verbindung auch wie folgt erhalten::
391+
392+
db = pool.dedicated_connection()
389393

390394
Wenn Sie die Datenbankverbindung nicht mehr benötigen, sollten Sie diese sofort
391395
wieder mit ``db.close()`` an den Pool zurückgeben. Sie können auf die gleiche

DBUtils/Docs/UsersGuide.html

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ <h1 class="title">DBUtils User's Guide</h1>
1515
<col class="docinfo-content" />
1616
<tbody valign="top">
1717
<tr><th class="docinfo-name">Version:</th>
18-
<td>0.9.4</td></tr>
19-
<tr class="field"><th class="docinfo-name">Released:</th><td class="field-body">07/07/07</td>
18+
<td>1.0pre</td></tr>
19+
<tr class="field"><th class="docinfo-name">Released:</th><td class="field-body">09/25/08</td>
2020
</tr>
2121
<tr class="field"><th class="docinfo-name">Translations:</th><td class="field-body">English <a class="reference" href="UsersGuide.de.html">German</a> <a class="reference" href="UsersGuide.zh.html">Chinese</a></td>
2222
</tr>
@@ -283,12 +283,19 @@ <h2><a id="id1" name="id1">PersistentDB</a></h2>
283283
connection objects or a DB-API 2 compliant database module</p>
284284
</li>
285285
<li><p class="first"><tt class="docutils literal"><span class="pre">maxusage</span></tt>: the maximum number of reuses of a single connection
286-
(the default of <tt class="docutils literal"><span class="pre">0</span></tt> or <tt class="docutils literal"><span class="pre">False</span></tt> means unlimited reuse)</p>
286+
(the default of <tt class="docutils literal"><span class="pre">0</span></tt> or <tt class="docutils literal"><span class="pre">None</span></tt> means unlimited reuse)</p>
287287
<p>Whenever the limit is reached, the connection will be reset.</p>
288288
</li>
289289
<li><p class="first"><tt class="docutils literal"><span class="pre">setsession</span></tt>: an optional list of SQL commands that may serve to
290290
prepare the session, e.g. <tt class="docutils literal"><span class="pre">[&quot;set</span> <span class="pre">datestyle</span> <span class="pre">to</span> <span class="pre">german&quot;,</span> <span class="pre">...]</span></tt></p>
291291
</li>
292+
<li><p class="first"><tt class="docutils literal"><span class="pre">failures</span></tt>: an optional exception class or a tuple of exception classes
293+
for which the connection failover mechanism shall be applied,
294+
if the default (OperationalError, InternalError) is not adequate</p>
295+
</li>
296+
<li><p class="first"><tt class="docutils literal"><span class="pre">closeable</span></tt>: if this is set to true, then closing connections will
297+
be allowed, but by default this will be silently ignored</p>
298+
</li>
292299
<li><p class="first">The creator function or the connect function of the DB-API 2 compliant
293300
database module specified as the creator will receive any additional
294301
parameters such as the host, database, user, password etc. You may
@@ -315,7 +322,7 @@ <h2><a id="id1" name="id1">PersistentDB</a></h2>
315322
ignored since it would be reopened at the next usage anyway and
316323
contrary to the intent of having persistent connections. Instead,
317324
the connection will be automatically closed when the thread dies.
318-
You can change this behavior be setting <tt class="docutils literal"><span class="pre">persist._closeable</span></tt> to <tt class="docutils literal"><span class="pre">True</span></tt>.</p>
325+
You can change this behavior be setting the <tt class="docutils literal"><span class="pre">closeable</span></tt> parameter.</p>
319326
</div>
320327
<div class="section">
321328
<h2><a id="id2" name="id2">PooledDB</a></h2>
@@ -340,18 +347,22 @@ <h2><a id="id2" name="id2">PooledDB</a></h2>
340347
<li><p class="first"><tt class="docutils literal"><span class="pre">maxconnections</span></tt>: maximum number of connections generally allowed
341348
(the default value of <tt class="docutils literal"><span class="pre">0</span></tt> means any number of connections)</p>
342349
</li>
343-
<li><p class="first"><tt class="docutils literal"><span class="pre">blocking</span></tt>: determines behavior when exceeding the maximum
344-
(the default of <tt class="docutils literal"><span class="pre">0</span></tt> or <tt class="docutils literal"><span class="pre">False</span></tt> means report an error; otherwise
345-
block and wait until the number of connections decreases)</p>
350+
<li><p class="first"><tt class="docutils literal"><span class="pre">blocking</span></tt>: determines behavior when exceeding the maximum</p>
351+
<p>If this is set to true, block and wait until the number of
352+
connections decreases, but by default an error will be reported.</p>
346353
</li>
347354
<li><p class="first"><tt class="docutils literal"><span class="pre">maxusage</span></tt>: maximum number of reuses of a single connection
348-
(the default of <tt class="docutils literal"><span class="pre">0</span></tt> or <tt class="docutils literal"><span class="pre">False</span></tt> means unlimited reuse)</p>
355+
(the default of <tt class="docutils literal"><span class="pre">0</span></tt> or <tt class="docutils literal"><span class="pre">None</span></tt> means unlimited reuse)</p>
349356
<p>When this maximum usage number of the connection is reached,
350357
the connection is automatically reset (closed and reopened).</p>
351358
</li>
352359
<li><p class="first"><tt class="docutils literal"><span class="pre">setsession</span></tt>: an optional list of SQL commands that may serve to
353360
prepare the session, e.g. <tt class="docutils literal"><span class="pre">[&quot;set</span> <span class="pre">datestyle</span> <span class="pre">to</span> <span class="pre">german&quot;,</span> <span class="pre">...]</span></tt></p>
354361
</li>
362+
<li><p class="first"><tt class="docutils literal"><span class="pre">failures</span></tt>: an optional exception class or a tuple of exception classes
363+
for which the connection failover mechanism shall be applied,
364+
if the default (OperationalError, InternalError) is not adequate</p>
365+
</li>
355366
<li><p class="first">The creator function or the connect function of the DB-API 2 compliant
356367
database module specified as the creator will receive any additional
357368
parameters such as the host, database, user, password etc. You may
@@ -378,7 +389,11 @@ <h2><a id="id2" name="id2">PooledDB</a></h2>
378389
if you set a non-zero <tt class="docutils literal"><span class="pre">maxshared</span></tt> parameter and the DB-API 2 module allows
379390
this. If you want to have a dedicated connection, use:</p>
380391
<pre class="literal-block">
381-
db = pool.connection(0)
392+
db = pool.connection(shareable=False)
393+
</pre>
394+
<p>Instead of this, you can also get a dedicated connection as follows:</p>
395+
<pre class="literal-block">
396+
db = pool.dedicated_connection()
382397
</pre>
383398
<p>If you don't need it any more, you should immediately return it to the
384399
pool with <tt class="docutils literal"><span class="pre">db.close()</span></tt>. You can get another connection in the same way.</p>

DBUtils/Docs/UsersGuide.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,11 @@ Please note that the connection may be shared with other threads by default
353353
if you set a non-zero ``maxshared`` parameter and the DB-API 2 module allows
354354
this. If you want to have a dedicated connection, use::
355355

356-
db = pool.connection(0)
356+
db = pool.connection(shareable=False)
357+
358+
Instead of this, you can also get a dedicated connection as follows::
359+
360+
db = pool.dedicated_connection()
357361

358362
If you don't need it any more, you should immediately return it to the
359363
pool with ``db.close()``. You can get another connection in the same way.

DBUtils/Docs/UsersGuide.zh.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ <h1 class="title">DBUtils 用户指南</h1>
1515
<col class="docinfo-content" />
1616
<tbody valign="top">
1717
<tr><th class="docinfo-name">版本:</th>
18-
<td>0.9.4</td></tr>
19-
<tr class="field"><th class="docinfo-name">发行版:</th><td class="field-body">07/07/07</td>
18+
<td>1.0pre</td></tr>
19+
<tr class="field"><th class="docinfo-name">发行版:</th><td class="field-body">09/25/08</td>
2020
</tr>
2121
<tr class="field"><th class="docinfo-name">翻译:</th><td class="field-body"><a class="reference" href="UsersGuide.html">English</a> <a class="reference" href="UsersGuide.de.html">German</a> Chinese</td>
2222
</tr>
@@ -242,7 +242,7 @@ <h2><a id="id12" name="id12">PooledDB</a></h2>
242242
<p>你可以使用这些连接有如原始的DB-API 2一样。而实际使用的是``SteadyDB``版本的强硬连接。</p>
243243
<p>请注意连接可以与其他线程共享,只要你设置 <tt class="docutils literal"><span class="pre">maxshared</span></tt> 参数为非零,并且DB-API 2模块也允许。如果你想要使用专用连接则使用:</p>
244244
<pre class="literal-block">
245-
db = pool.connection(0)
245+
db = pool.connection(shareable=False)
246246
</pre>
247247
<p>如果你不再需要这个连接了,则可以返回给连接池使用 <tt class="docutils literal"><span class="pre">db.close()</span></tt> 。你也可以使用相同的方法获取另一个连接。</p>
248248
<p><em>警告:</em> 在一个多线程环境,不要使用下面的方法:</p>

DBUtils/Docs/UsersGuide.zh.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ PooledDB
222222

223223
请注意连接可以与其他线程共享,只要你设置 ``maxshared`` 参数为非零,并且DB-API 2模块也允许。如果你想要使用专用连接则使用::
224224

225-
db = pool.connection(0)
225+
db = pool.connection(shareable=False)
226226

227227
如果你不再需要这个连接了,则可以返回给连接池使用 ``db.close()`` 。你也可以使用相同的方法获取另一个连接。
228228

DBUtils/Examples/DBUtilsExample.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,14 +125,17 @@ def connection(self, shareable=True):
125125
error = 'Cannot connect to the database.'
126126
self.outputMsg(error, True)
127127

128+
def dedicated_connection(self):
129+
return self.connection(False)
130+
128131
def sqlEncode(self, s):
129132
if s is None:
130133
return 'null'
131134
s = s.replace('\\', '\\\\').replace('\'', '\\\'')
132135
return "'%s'" % s
133136

134137
def createTables(self):
135-
db = self.connection(False)
138+
db = self.dedicated_connection()
136139
if not db:
137140
return
138141
for table in tables:
@@ -160,7 +163,7 @@ def listSeminars(self):
160163
id = [id]
161164
cmd = ','.join(map(self.sqlEncode, id))
162165
cmd = 'delete from seminars where id in (%s)' % cmd
163-
db = self.connection(False)
166+
db = self.dedicated_connection()
164167
if not db:
165168
return
166169
try:
@@ -236,7 +239,7 @@ def listAttendees(self):
236239
for i, n in places.items():
237240
cmds.append("update seminars set places_left=places_left+%d "
238241
"where id=%s" % (n, self.sqlEncode(i)))
239-
db = self.connection(False)
242+
db = self.dedicated_connection()
240243
if not db:
241244
return
242245
try:
@@ -320,7 +323,7 @@ def newSeminar(self):
320323
values[2] = None
321324
if not values[3]:
322325
values[3] = None
323-
db = self.connection(0)
326+
db = self.dedicated_connection()
324327
if not db:
325328
return
326329
cmd = ('insert into seminars values (%s,%s,%s,%s)'
@@ -388,7 +391,7 @@ def newAttendee(self):
388391
if not values[0] or not values[1]:
389392
self.outputMsg('You must enter a name and a seminar!')
390393
return
391-
db = self.connection(0)
394+
db = self.dedicated_connection()
392395
if not db:
393396
return
394397
try:

DBUtils/PersistentDB.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,10 @@ def connection(self, shareable=False):
176176
self.thread.connection = con
177177
return con
178178

179+
def dedicated_connection(self):
180+
"""Alias for connection(shareable=False)."""
181+
return self.connection()
182+
179183

180184
try: # import a class for representing thread-local objects
181185
from threading import local

0 commit comments

Comments
 (0)