Rollback after error in transaction
This should be an easy one for those familiar with Postgresql:
My application issues a begin_work
, does a number of operations, and then issues a commit
. The operations and the commit are wrapped inside a try-catch block, whose catch statement performs a rollback
. Assumption: if an error occurs during a SQL operation, Postgresql will automatically rollback the transaction, and therefore my rollback will be redundant but harmless. Is this assumption correct?
(The reason why I'm rollbacking anyway: just in case an exception unrelated to a SQL operation ocurs.)
If an error occurs, PostgreSQL does not actually rollback the transaction. It fails every subsequent statements with an error. You can try this out in the client.
You need to execute rollback before any statements can be executed successfully.
In the case that you close the connection and start a new one, this is of little consequence. However, if you retain the connection and run other statements which you expect to execute successfully, it will not work.
链接地址: http://www.djcxy.com/p/70636.html下一篇: 事务中出错后回滚