gpt4 book ai didi

sql-server - 如果两个事务都没有回滚,则已提交读与未提交读

转载 作者:行者123 更新时间:2023-12-03 16:36:23 25 4
gpt4 key购买 nike

我试图了解已提交读和未提交读隔离级别。我知道理论上读未提交允许脏读而提交读不允许,但我仍然无法真正理解。

this example

考虑上图,如果没有任何事务被中止,那么对于已提交读和未提交读的最终结果是否相同?

最佳答案

您的示例与 Isolation Levels 无关.这是因为它们会影响 readers行为,而不是 writers ,在你的例子中只有 writers .

您应该引用这篇 BOL 文章:Understanding Isolation Levels
说的是

Choosing a transaction isolation level doesn't affect the locks that are acquired to protect data modifications. A transaction always gets an exclusive lock on any data it modifies and holds that lock until the transaction completes, regardless of the isolation level set for that transaction. For read operations, transaction isolation levels primarily define the level of protection from the effects of modifications made by other transactions.



在您的示例中,没有交易 read ,他们都 modify .第一个交易会获取 X对感兴趣 RIDkey (取决于表结构,如果是堆表或聚簇表)--我将其称为 res_1 将来 - 用于插入并将在整个事务期间保持它(它也会在相应的 IXpage 上有 object ),第二个事务的第一个语句也是如此:将收购 X res_2 插入时。

关于 DELETE尝试第二笔交易将被阻止,因为它无法获得 X (或 U 如果 where 条件没有索引),这是因为已经存在 X在第一个事务持有的同一资源 ( res_1 ) 上。并且不会有第二个 INSERT在第二笔交易中,因为之前的 DELETE被阻止。

最后,当第一笔交易尝试其 DELETE 时,需要 XU (取决于索引存在)在 res_2 ,但它已经被 X 屏蔽了通过tran2,所以它也被阻塞,这种情况没有退出,每个 session 都等待另一个 session 完成,没有 session 可以完成,此时是 deadlock发生,服务器将通过 rolling back 解决它交易之一。

关于sql-server - 如果两个事务都没有回滚,则已提交读与未提交读,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54063118/

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