交易错误后回滚 [英] Rollback after error in transaction

查看:113
本文介绍了交易错误后回滚的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于熟悉Postgresql的人来说,这应该很容易:

This should be an easy one for those familiar with Postgresql:

我的应用发布了 begin_work ,许多操作,然后发出 commit 。操作和提交包装在try-catch块中,该块的catch语句执行 rollback 。假设:如果在SQL操作过程中发生错误,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?

(无论如何,我回滚的原因:以防万一发生与SQL操作无关的异常。)

(The reason why I'm rollbacking anyway: just in case an exception unrelated to a SQL operation ocurs.)

推荐答案

如果发生错误,PostgreSQL实际上不会回滚事务。它使所有后续语句失败并出现错误。您可以在客户端中进行尝试。

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.

这篇关于交易错误后回滚的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆