简体   繁体   English

数据库事务仅部分提交

[英]Database transaction only partially committing

I've got a T-SQL stored procedure running on a Sybase ASE database server that is sometimes failing to commit all of its operations, even though it completes without exception. 我在Sybase ASE数据库服务器上运行了一个T-SQL存储过程,尽管它无例外地完成了,但有时无法提交其所有操作。 Here's a rough example of what it does. 这是一个大致的示例。

BEGIN TRANSACTION

UPDATE TABLE1
SET FIELD1 = @NEW_VALUE1
WHERE KEY1 = @KEY_VALUE1

IF @@error <> 0 OR @@rowcount <> 1 BEGIN
    ROLLBACK
    RETURN 1
END

UPDATE TABLE2
SET FIELD2 = @NEW_VALUE2
WHERE KEY2 = @KEY_VALUE2

IF @@error <> 0 OR @@rowcount <> 1 BEGIN
    ROLLBACK
    RETURN 2
END

INSERT TABLE2 (FIELD2, FIELD3)
VALUES (@NEW_VALUE3a, @NEW_VALUE3b)

IF @@error <> 0 OR @@rowcount <> 1 BEGIN
    ROLLBACK
    RETURN 3
END

COMMIT TRANSACTION
RETURN 0

The procedure is called at least hundreds of times a day. 该程序每天至少调用数百次。 In a small percentage of those cases (probably < 3%), only the INSERT statement commits. 在少数情况下(可能<3%),仅INSERT语句提交。 The proc completes and returns 0, but the two UPDATE s don't take. proc完成并返回0,但是两个UPDATE不使用。 Originally we thought it might be that the WHERE clauses on the UPDATE s weren't matching anything, so we added the IF @@rowcount logic. 最初,我们认为UPDATE上的WHERE子句可能与任何内容都不匹配,因此我们添加了IF @@rowcount逻辑。 But even with those checks in there, the INSERT is still happening and the procedure is still completing and returning 0. 但是即使进行了这些检查, INSERT仍在发生,并且该过程仍在完成并返回0。

I'm looking for ideas about what might cause this type of problem. 我正在寻找有关可能导致此类问题的想法。 Is there anything about the way SQL transactions work, or the way Sybase works specifically, that could be causing the COMMIT not to commit everything? 关于SQL事务的工作方式或Sybase的具体工作方式,是否可能导致COMMIT不提交所有内容? Is there something about my IF blocks that could allow the UPDATE to not match anything but the procedure to continue? 关于我的IF块,是否有某些东西可以使UPDATE除了继续执行过程之外不匹配任何东西? Any other ideas? 还有其他想法吗?

is is possible that they are updating, but something is changing the values back? 它们是否有可能正在更新,但是某些值又将其更改回? try adding a update trigger on those tables and within that trigger insert into a log table. 请尝试在这些表上添加更新触发器,然后在该触发器内插入日志表。 for rows that appear to have not been updated look in the log, is there a row or not? 对于似乎尚未更新的行,请查看日志,是否有行?

Not knowing how you set the values for your variables, it occurs to me that if the value of @NEW_VALUE1 is the same as the previous value in FIELD1 , the update would succeed and yet appear to have not changed anything making you think the transaction had not happened. 不知道如何设置变量的值,我想到如果@ NEW_VALUE1的值与FIELD1中的上一个值相同,则更新将成功,但似乎没有进行任何更改,使您认为事务已没有发生。

You also could have a trigger that is affecting the update. 您也可能有一个影响更新的触发器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM