如何修复“快照隔离事务由于更新冲突而中止”?
我看到一条与事务隔离级别相关的错误消息。涉及两个表,第一个一个经常更新,事务隔离级别设置为SERIALIZABLE,第二个一个在上有一个外键>第一个。
插入或更新第二表时出现问题。几个小时后我就会收到以下错误消息:
由于更新冲突,快照隔离事务中止。您不能使用快照隔离直接或间接访问数据库“DB”中的表“dbo.first”来更新、删除或插入已被另一个事务修改或删除的行。重试事务或更改更新/删除语句的隔离级别。
在插入或更新第二表时,我没有设置事务隔离级别,而且我运行了命令DBCC USEROPTIONS,它返回read_commissed。
I see an error message related to transaction isolation levels. There are two tables involved, first one is updated frequently with transaction isolation level set to SERIALIZABLE, the second one has a foreign key on first one.
Problem occurs when doing insert or update of the second table. Once in few hours I get the following error message:
Snapshot isolation transaction aborted due to update conflict. You cannot use snapshot isolation to access table 'dbo.first' directly or indirectly in database 'DB' to update, delete, or insert the row that has been modified or deleted by another transaction. Retry the transaction or change the isolation level for the update/delete statement.
I don't set transaction isolation level when inserting or updating second table, also I ran command DBCC USEROPTIONS and it returns read_committed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
第一:
看起来,您没有使用
SERIALIZABLE
,而是使用 MSSQL 2005 中引入的快照隔离。这是一篇文章来了解其中的差异:http:// blogs.msdn.com/b/craigfr/archive/2007/05/16/serialized-vs-snapshot-isolation-level.aspx
=>这是基于错误消息,但正如您在评论中再次解释的那样,编辑第二个表时会出现错误。
第二:
对于修改,MSSQL Server 总是尝试获取锁,并且由于第一个表上有锁(通过使用事务),因此升级到第二个表上的锁,因为(外键)操作失败。因此,每次修改实际上都会导致一次小型交易。
MSSQL 上的默认事务级别是 READ COMMITTED,但如果您打开选项 READ_COMMITTED_SNAPSHOT,它会将 READ COMMITTED 转换为 SNAPSHOT 就像每次使用
READ COMMITTED
时的事务一样。这会导致您收到错误消息。准确地说,正如 VladV 指出的那样,它并不是真正使用
SNAPSHOT
隔离级别,而是使用READ COMMITTED
行版本控制 而不是锁定,但仅基于语句,其中SNAPSHOT
在事务基础上使用行版本控制。要了解差异,请查看以下内容:
http://msdn.microsoft.com/en-us /library/ms345124(SQL.90).aspx
要了解有关
READ_COMMITTED_SNAPSHOT
的更多信息,请在此处详细说明:http://msdn.microsoft.com/en-us /library/tcbchxcb(VS.80).aspx
在这里:
默认 SQL Server 隔离级别更改
您查看
快照 如果您没有指定隔离,则使用隐式事务。打开此选项后,并且您实际上没有在修改语句上指定隔离级别(实际上您没有指定),MS SQL Server 将选择他认为正确的隔离级别。详细信息如下:
http://msdn.microsoft.com/en-us /library/ms188317(SQL.90).aspx
对于所有这些场景,解决方案都是相同的。
解决方案:
您需要按顺序执行操作,您可以通过专门在两个操作上使用具有
SERIALIZABLE
隔离级别的事务来实现这一点:插入/更新第一个操作时和插入时/更新第二个。通过这种方式,您可以阻止相应的另一个,直到完成为止。
First:
It seems, you're not using
SERIALIZABLE
, but snapshot isolation which was introduced with MSSQL 2005. Here is an article to understand the difference:http://blogs.msdn.com/b/craigfr/archive/2007/05/16/serializable-vs-snapshot-isolation-level.aspx
=> This was based on the error, message, but as you have explained again in the comments the error comes when editing the second table.
Second:
For modifications MSSQL Server always tries to acquire locks, and since there are locks (by using a transaction) on the first table which escalate to locks on the second table because of the (foreign key) the operation fails. So every modification causes in fact a mini transaction.
The default transaction level on MSSQL is
READ COMMITTED
, but if you turn on the optionREAD_COMMITTED_SNAPSHOT
it will convertREAD COMMITTED
to aSNAPSHOT
like transaction every time you useREAD COMMITTED
. Which then leads to the error message you get.To be precise as VladV pointed out, it's not really using the
SNAPSHOT
isolation level, butREAD COMMITTED
with row versioning rather than locking, but only on a statement basis, whereSNAPSHOT
is using row versioning on a transaction basis.To understand the difference check out this:
http://msdn.microsoft.com/en-us/library/ms345124(SQL.90).aspx
To find out more about the
READ_COMMITTED_SNAPSHOT
, its explained in detail here:http://msdn.microsoft.com/en-us/library/tcbchxcb(VS.80).aspx
and here:
Default SQL Server IsolationLevel Changes
Another reason for you to see
SNAPSHOT
isolation if you have not specified it, is by using implicit transaction. After turing this option on and you don't actually specify the isolation level on a modifying statement (which you don't), MS SQL server will choose whatever he believes is the right isolation level. Here are the details:http://msdn.microsoft.com/en-us/library/ms188317(SQL.90).aspx
For all theses scenarios the solution is the same though.
Solution:
You need to execute the operations in sequence, and you can do this by specifically using a transaction with
SERIALIZABLE
isolation level on both operations: when inserting/updating the first and when inserting/updating the second.This way you block the respective other until it is completed.
我们遇到了类似的问题 - 您会很高兴知道您应该能够在不删除 FK 约束的情况下解决该问题。
具体来说,在我们的场景中,我们在 READ COMMITTED 事务中频繁更新父表。我们还经常发生并发(长时间运行)快照事务,需要将行插入到具有父表 FK 的子表中 - 因此本质上它与您的情况相同,只是我们使用 READ COMMITTED 而不是 SEREALIZABLE 事务。
要解决该问题,请在主表上的 FK 列上创建一个新的 UNIQUE NONCLUSTERED 约束。此外,您还必须在创建唯一约束后重新创建 FK,因为这将确保 FK 现在引用该约束(而不是聚集键)。
不幸的是我找不到网上有一个关于为什么创建唯一约束可以解决问题的很好的解释。我可以解释为什么它有效的最简单方法是因为 FK 现在仅引用唯一约束 - 并且对父表(即非 FK 引用的列)的修改不会导致快照事务中的更新冲突,因为 FK现在引用未更改唯一约束条目。将此与聚簇键进行对比,其中对父表中任何列的更改都会影响该表中的行版本 - 并且由于 FK 看到更新的版本号,因此快照事务需要中止。
此外,如果在非快照事务中删除父行,那么聚集约束和唯一约束都会受到影响,并且如预期的那样,快照事务将回滚(因此可以保持 FK 完整性)。
我已经能够使用上面的示例代码重现此问题,该代码改编自 此博客条目
要修复上述情况,请重新设置测试数据库。然后在运行测试 1 和 2 之前运行以下脚本。
We had a similar issue - and you'd be glad to know that you should be able to solve the problem without removing the FK constraint.
Specifically, in our scenario, we had frequent updates to the parent table in a READ COMMITTED transaction. We also had frequent concurrent (long running) snapshot transactions occurring that needed to insert rows into a child table with a FK to parent table - so essentially it's the same scenario as yours, except we used a READ COMMITTED instead of SEREALIZABLE transaction.
To solve the problem, create a new UNIQUE NONCLUSTERED constraint on the primary table over the FK column. In addition you must also re-create the FK after you've created the unique constraint as this will ensure that the FK now references the constraint (not the clustered key).
Unfortunately I can't find a good explanation on the web on why creating a unique constraint solves the problem. The easiest way I can explain why this works is because the FK now only references the unique constraint - and a modification to the parent table (i.e. to the non-FK referenced columns) does not cause an update conflict in the snapshot transaction as the FK now references an unchanged unique constraint entry. Contrast this with the clustered key where a change to any column in parent table would affect the row version in this table - and since the FK sees an updated version number, the snapshot transaction needs to abort.
Furthermore, if the parent row is deleted in the non-snapshot transaction, then both the clustered and unique constraints would be affected and, as expected, the snapshot transaction will roll back (so FK integrity is maintained).
I've been able to reproduce this problem using the above sample code that I have adapted from this blog entry
And to fix the above scenario, re-setup the test database. Then run the following script before running Test 1 and 2.
根据我对“SNAPSHOT”、“SERIALIZABLE”和“READ COMMITTED SNAPSHOT”隔离级别的3次实验,当我仅使用“SNAPSHOT”隔离级别时,我得到了以下相同的错误更新已由其他事务更新的行,而我在“SERIALIZABLE”和“READ COMMITTED SNAPSHOT”隔离级别下没有收到以下相同的错误:
并且,如 文档如下所述,使用“SNAPSHOT”隔离级别,我们会得到与上面相同的错误更新已被其他事务更新的行。而且,我认为我们无法解决或避免上述错误,因此我们可以做的是再次重试交易,如上面的错误所述。因此,如果我们的应用程序收到上述错误,我们将能够将上述错误作为异常处理来再次重试事务:
对于“SNAPSHOT”隔离级别的实验,我使用“id”和“name”创建了“person”表 “test”数据库中如下所示:
,我使用 SQL 查询执行了这些步骤,如下所示:
开始;
BEGIN;
UPDATE person SET name = 'Tom' WHERE id = 2;
UPDATE person SET name = 'Lisa' WHERE id = 2;
COMMIT;
ROLLBACK;
快照隔离事务因更新冲突而中止。您不能使用快照隔离直接或间接访问数据库“test”中的表“dbo.person”来更新、删除或插入已被另一个事务修改或删除的行。重试事务或更改更新/删除语句的隔离级别。
According to my 3 experiments with "SNAPSHOT", "SERIALIZABLE" and "READ COMMITTED SNAPSHOT" isolation levels, I got the same error below with only "SNAPSHOT" isolation level when updating the row which is already updated by other transaction while I did not get the same error below with "SERIALIZABLE" and "READ COMMITTED SNAPSHOT" isolation levels:
And, as the documentation says below, with "SNAPSHOT" isolation level, we get the same error above when updating the row which is already updated by other transaction. And, I do not think we can solve or avoid the error above so what we can do is retry the transaction again as the error above says. So if our applications get the error above, we will be able to handle the error above as an exception handling to retry the transaction again:
For my experiment with "SNAPSHOT" isolation level, I created "person" table with "id" and "name" in "test" database as shown below:
Now, I did these steps with SQL queries as shown below:
BEGIN;
BEGIN;
UPDATE person SET name = 'Tom' WHERE id = 2;
UPDATE person SET name = 'Lisa' WHERE id = 2;
COMMIT;
ROLLBACK;
Snapshot isolation transaction aborted due to update conflict. You cannot use snapshot isolation to access table 'dbo.person' directly or indirectly in database 'test' to update, delete, or insert the row that has been modified or deleted by another transaction. Retry the transaction or change the isolation level for the update/delete statement.