Revision 3137
Added by Aaron Marcuse-Kubitza over 12 years ago
lib/sql.py | ||
---|---|---|
389 | 389 |
finally: self.db.set_isolation_level(prev_isolation_level) |
390 | 390 |
|
391 | 391 |
def with_savepoint(self, func): |
392 |
top = self._savepoint == 0 |
|
392 | 393 |
savepoint = 'level_'+str(self._savepoint) |
393 |
self.run_query('SAVEPOINT '+savepoint, log_level=4) |
|
394 |
|
|
395 |
if top: query = 'START TRANSACTION ISOLATION LEVEL READ COMMITTED' |
|
396 |
else: query = 'SAVEPOINT '+savepoint |
|
397 |
self.run_query(query, log_level=4) |
|
398 |
|
|
394 | 399 |
self._savepoint += 1 |
395 |
try: return func() |
|
400 |
try: |
|
401 |
return func() |
|
402 |
if top: self.run_query('COMMIT', log_level=4) |
|
396 | 403 |
except: |
397 |
self.run_query('ROLLBACK TO SAVEPOINT '+savepoint, log_level=4) |
|
404 |
if top: query = 'ROLLBACK' |
|
405 |
else: query = 'ROLLBACK TO SAVEPOINT '+savepoint |
|
406 |
self.run_query(query, log_level=4) |
|
407 |
|
|
398 | 408 |
raise |
399 | 409 |
finally: |
400 | 410 |
# Always release savepoint, because after ROLLBACK TO SAVEPOINT, |
401 | 411 |
# "The savepoint remains valid and can be rolled back to again" |
402 | 412 |
# (http://www.postgresql.org/docs/8.3/static/sql-rollback-to.html). |
403 |
self.run_query('RELEASE SAVEPOINT '+savepoint, log_level=4) |
|
413 |
if not top: |
|
414 |
self.run_query('RELEASE SAVEPOINT '+savepoint, log_level=4) |
|
404 | 415 |
|
405 | 416 |
self._savepoint -= 1 |
406 | 417 |
assert self._savepoint >= 0 |
Also available in: Unified diff
sql.py: DbConn.with_savepoint(): Open a new transaction if one is not already open