你可以做一個rollback
的交易或只是引發異常(cr爲光標)的代碼之前回滾到一個保存點:
name = uuid.uuid1().hex
cr.execute('SAVEPOINT "%s"' % name)
try:
# your failing query goes here
except Exception:
cr.execute('ROLLBACK TO SAVEPOINT "%s"' % name)
# your alternative code goes here
else:
cr.execute('RELEASE SAVEPOINT "%s"' % name)
此代碼假定存在正在運行的事務,否則你不會收到該錯誤信息。
Django的PostgreSQL後端creates cursors直接從psycopg。也許在將來他們會爲Django光標創建一個代理類,類似於cursor of odoo。他們延長光標與following code(自我是光標):
@contextmanager
@check
def savepoint(self):
"""context manager entering in a new savepoint"""
name = uuid.uuid1().hex
self.execute('SAVEPOINT "%s"' % name)
try:
yield
except Exception:
self.execute('ROLLBACK TO SAVEPOINT "%s"' % name)
raise
else:
self.execute('RELEASE SAVEPOINT "%s"' % name)
這樣的背景使代碼更容易,這將是:
try:
with cr.savepoint():
# your failing query goes here
except Exception:
# your alternative code goes here
而且代碼的可讀性,因爲交易的東西不存在。
這不會起作用,當發生錯誤時,事務被中止並回滾。數據庫中需要一個異常處理程序。 查詢失敗:錯誤:當前事務中止,忽略命令直到事務塊結束 – 2010-03-03 18:38:11
是的。這就是SAVEPOINTs的重點。爲了給出一個具體的例子,我編輯了我的答案。 – 2010-03-03 19:02:02
----編輯---- 對不起,我錯了...對我感到羞恥;)它工作正常,你說得對。 – 2010-03-03 19:06:07