我想正确处理提交事务时可能会引发 IntegrityError 异常,乍一看,我看到了两种实现方式,但不确定两者是否正确,有人可以澄清两者是否正确,为什么?
注意:我正在使用 PostgreSQL
# alternative one
try:
with transaction.commit_on_success():
# db operation 1
# db operation 2
except IntegrityError:
transaction.rollback()
#alternative two
with transaction.commit_manually():
sid = transaction.savepoint()
# db operation 1
# db operation 2
try:
transaction.savepoint_commit(sid)
except IntegrityError:
transaction.savepoint_rollback(sid)