当我执行一个简单的查询(我正在使用tornado.database模块)时,我会处理这样的异常:
try:
self.application.db.execute('DELETE FROM table_one WHERE a = 1')
except Exception, e:
logging.warning('DB exception: %s' % e)
self.set_status(500)
return
如果我想做一个事务,在出现异常的情况下回滚它会很聪明:
try:
self.application.db.execute('START TRANSACTION')
self.application.db.execute('DELETE FROM table_one WHERE a = 1')
self.application.db.execute('DELETE FROM table_two WHERE b = 2')
self.application.db.execute('COMMIT')
except Exception, e:
logging.warning('DB exception: %s' % e)
self.set_status(500)
self.application.db.execute('ROLLBACK')
return
但是如果回滚也会导致异常(例如,如果连接失败)怎么办?我是否需要在 except 块内放置一个嵌套的 try-except 块?