【发布时间】:2014-01-24 12:43:13
【问题描述】:
我看到一些随机发生的奇怪行为。 这就是我的存储过程的基本作用。
begin try
begin tran
insert into table1
update table2
insert into table3
commit tran
end try
begin catch
rollback tran
end catch
在大多数情况下,上述代码工作正常,除了偶尔(每天或每天一次)发生错误时,事务不会回滚所有 3 个表的更改。
begin try
begin tran
insert into table1----Rollback doesn't happen
update table2--Rollback happens
insert into table3--Rollback happens
commit tran
end try
begin catch
rollback tran
end catch
任何人都可以提出一些我可能错的地方或者我需要以其他方式处理交易吗?
提前致谢。
【问题讨论】:
-
这是不可能的。你的分析一定是错的。部分回滚事务的唯一方法是故意回滚到保存点。
-
您如何确定回滚是否以您描述的方式发生。您认为有哪些信息/日志等表明这是问题所在?
-
您说对于
update table2和insert into table3会发生回滚。这不一定是真的。如果insert into table1发生错误,则立即执行跳转到catch块。所以这不是“回滚发生”,而是“操作永远不会执行”。 -
发生了一个错误,回滚了事务,但批处理继续执行,现在没有事务。我不完全了解发生这种情况的确切条件。这里可能就是这种情况。
-
@frikozoid 在这种情况下,我无法理解的是为什么我仍然看到插入到 table1 而不是其他表中的记录。因此我假设回滚发生在 table2 和 table3 而不是 table1。
标签: sql-server sql-server-2005 transactions rollback