gpt4 book ai didi

sql - 如果没有使用 try/catch 或异常逻辑来回滚,那么在事务中包装存储过程是否有意义?

转载 作者:搜寻专家 更新时间:2023-10-30 23:44:08 24 4
gpt4 key购买 nike

我注意到我的公司有很多类似这样的存储过程脚本:

alter procedure MyProcedure
@ItemID int
as
BEGIN TRANSACTION
-- Do some stuff, maybe only 1 insert/update statement, maybe many
COMMIT TRANSACTION

重点是,没有 try/catch block ,也没有执行回滚的“if”语句。代码中的任何地方都从未使用过回滚。

那么交易的意义何在?包装在我不知道的交易中还有其他好处吗?我一直认为原因是如果出现问题使用回滚。

在这个程序的范围之外是否有逻辑赋予它意义?

鉴于我有限的知识,这不仅看起来毫无意义,而且看起来很危险,因为如果出现问题,提交将永远不会执行(对吗?),然后您有一个打开的事务并且表被无限期锁定。

最佳答案

如果更新中途失败,它将在 session 关闭时回滚(更具体地说,未能提交,但相当于回滚)。详细讨论了here ,但相关部分是:

Transactions ... Has no persistent effect whatever on the database, through: the action of the ROLLBACK Statement, abnormal termination of the client requesting the transaction, or abnormal termination of the transaction by the DBMS. This may be an action by the system (deadlock resolution) or by an administrative agent, or it may be an abnormal termination of the DBMS itself. In the latter case, the DBMS must roll back any active transactions during recovery.

基本上,他们已将其设置为不会部分执行该过程。尽管如此,由于性能和锁定问题,这可能并不总是理想的方式,但如果没有更多细节,我真的不能说。

关于sql - 如果没有使用 try/catch 或异常逻辑来回滚,那么在事务中包装存储过程是否有意义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31120394/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com