如何在psycopg2中回滚?
我在 qry2 中强制出现错误,以便 a 可以运行 qry3,假设它可以回滚到 save_1,但事实并非如此。有什么收获吗?使用 PostgreSql 14.2
qry1 = ('begin;' +
'savepoint save_1;' +
'delete from ntnb_cup;')
qry2 = ...
# Force error
qry3 = 'rollback to save_1;'
try:
cursor = conn.cursor()
cursor.execute(qry1)
conn.commit()
except Exception as err:
cursor.close()
conn.close()
exit()
try:
cursor.execute(qry2)
conn.commit()
except Exception as err:
cursor.execute(qry3)
conn.commit()
finally:
cursor.close()
conn.close()
I´m forcing an error in qry2 so that a can run qry3 suposing it could rollback to save_1 but it doesn´t. Any catch? Using PostgreSql 14.2
qry1 = ('begin;' +
'savepoint save_1;' +
'delete from ntnb_cup;')
qry2 = ...
# Force error
qry3 = 'rollback to save_1;'
try:
cursor = conn.cursor()
cursor.execute(qry1)
conn.commit()
except Exception as err:
cursor.close()
conn.close()
exit()
try:
cursor.execute(qry2)
conn.commit()
except Exception as err:
cursor.execute(qry3)
conn.commit()
finally:
cursor.close()
conn.close()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一旦你承诺了,你就承诺了。要保留稍后回滚到保存点的选项,请不要在此处提交。
Once you commit, you are committed. To preserve the option of rolling back to a savepoint later, don't commit here.