交易错误后回滚

编程入门 行业动态 更新时间:2024-10-28 09:26:25
本文介绍了交易错误后回滚的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

对于熟悉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.

更多推荐

交易错误后回滚

本文发布于:2023-10-19 19:32:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1508548.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误   后回滚

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!