try: a_session.commit() b_sessiom.commit() except Exception: a_session.rollback() b_session.rollback()
在处理两个 db 的时候,如果执行 a_session.commit() 就报错了,因为 b_session.commit()没有执行,那么 b_session.rollback() 会报错 或者会有什么不好的影响吗。
本意就是 a 或 b 其中一个执行错了 都回滚的。
求教大佬们。。。
1
Wichna 2017 年 11 月 8 日 session.rollback() 的执行应该是具有幂等性的吧,没有任何 commit 执行 rollback 应该不会有什么问题
|
2
fox0001 2017 年 11 月 8 日
事务没学好…
|
4
ryd994 2017 年 11 月 8 日 via Android
我觉得你这里设计就错了。看你的意思,a 和 b 要求同时成功或失败,那他们就应该合并为一个事务啊。
|
7
ryd994 2017 年 11 月 9 日 via Android
等等,你 commit 过的怎么还能 rollback ?
|
8
siteshen 2017 年 11 月 9 日
这个能解决你的问题,可以去研究下。
http://docs.sqlalchemy.org/en/latest/orm/session_transaction.html#enabling-two-phase-commit For backends which support two-phase operation (currently MySQL and PostgreSQL), the session can be instructed to use two-phase commit semantics. This will coordinate the committing of transactions across databases so that the transaction is either committed or rolled back in all databases. |