- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在处理某个特定实现时遇到了问题。
我有一个基本的方法来创建一个新的上下文,查询一个表并从表中获取“LastNumberUsed”,在最终增加和写回之前对这个数字执行一些基本检查——所有这些都在一个事务中。
我已经编写了一个基本的测试应用程序,它使用 Parallel.For 来执行这个方法 5 次。
使用 Isolation.Serialization 我发现在运行此代码时出现了很多死锁错误。
我已经阅读了一些关于这个主题的内容,并尝试将隔离级别更改为快照。我不再遇到死锁,而是发现我遇到了隔离更新冲突错误。
我真的不知道该怎么办。每个事务大约需要 0.009 秒才能完成,所以我一直在考虑将代码包装在 try..catch 中,检查死锁错误并再次运行,但这感觉像是一个困惑的解决方案。
有没有人对如何处理这个问题有任何想法(或最好的经验)?
我创建了一个控制台应用程序来演示这一点。
在程序 main 中,我运行以下代码:
Parallel.For(0, totalRequests,
x => TestContract(x, contractId, incrementBy, maxRetries));
//Define the context
using (var context = new Entities())
{
//Define a new transaction
var options = new TransactionOptions {IsolationLevel = IsolationLevel.Serializable};
using (var scope = new TransactionScope(TransactionScopeOption.Required, options))
{
//Get the contract details
var contract = (
from c in context.ContractRanges
where c.ContractId == contractId
select c).FirstOrDefault();
//Simulate activity
Threading.Thread.sleep(50);
//Increment the contract number
contract.Number++;
//Save the changes made to the context
context.SaveChanges();
//Complete the scope
scope.Complete();
}
}
}
最佳答案
暂时将隔离级别放在一边,让我们专注于您的代码正在做什么:
您正在并行运行 5 个调用 TestContract
的任务。路过一样contractId
对于他们所有人,对吧?
在 TestContract
您获取 contract
由其 id
,用它做一些工作,然后增加 Number
契约(Contract)的属性(property)。
所有这一切都在交易边界内。
为什么会出现死锁?
为了理解为什么会陷入僵局,了解 Serializable
是什么很重要。隔离级别是指。
documentation of SQL Server Isolation Levels以下是关于 Serializable
的内容(强调我的):
- Statements cannot read data that has been modified but not yet committed by other transactions.
- No other transactions can modify data that has been read by the current transaction until the current transaction completes.
- Other transactions cannot insert new rows with key values that would fall in the range of keys read by any statements in the current transaction until the current transaction completes.
Range locks are placed in the range of key values that match the search conditions of each statement executed in a transaction. This blocks other transactions from updating or inserting any rows that would qualify for any of the statements executed by the current transaction. This means that if any of the statements in a transaction are executed a second time, they will read the same set of rows. The range locks are held until the transaction completes. This is the most restrictive of the isolation levels because it locks entire ranges of keys and holds the locks until the transaction completes. Because concurrency is lower, use this option only when necessary. This option has the same effect as setting HOLDLOCK on all tables in all SELECT statements in a transaction.
TaskA
和
TaskB
与
contractId=123
, 全部在与
Serializable
的交易下隔离级别。
Transaction 1234
具有可序列化隔离级别 Transaction 5678
具有可序列化隔离级别 SELECT * FROM ContractRanges WHERE ContractId = 123
.ContractRanges
中放置了一个锁表,在 ContractId = 123
所在的行中以防止其他事务改变该数据。 SELECT
声明并放了一个 lock
在 ContractId = 123
排的ContractRanges
table 。 Number
契约(Contract)Number
契约(Contract)属性(property)SaveChanges
反过来,它会尝试提交事务。 1234
,我们正在尝试修改
Number
具有事务创建的锁的行中的值
5678
所以,SQL Servers 开始等待
lock
被释放以便按照您的要求提交事务。
TaskB
,然后,也调用 SaveChanges
,就像发生在 TaskA
上一样,它试图增加 Number
契约(Contract)123
.在这种情况下,它会找到一个 lock
在事务创建的那一行 1234
来自 TaskA
. 1234
来自
TaskA
等待来自事务的锁
5678
待发布
和 交易
5678
等待来自事务的锁
1234
即将面世。这意味着我们处于死锁状态,因为两个事务都无法完成,因为它们相互阻塞。
victim
,杀死它并允许另一个继续。
Serializable
,但很有可能您不需要它。
Serializable
是最安全和最严格的隔离级别,它通过牺牲并发性来实现,就像我们看到的那样。
Serializable
保证您真的不应该尝试更新
Number
同时签同一份契约(Contract)。
Snapshot Isolation
选择
I have read a bit on this subject and tried changing the isolation level to snapshot. I no longer get deadlocks but instead find I get isolation update conflict errors.
Snapshot
使用乐观并发模型。
Specifies that data read by any statement in a transaction will be the transactionally consistent version of the data that existed at the start of the transaction. The transaction can only recognize data modifications that were committed before the start of the transaction. Data modifications made by other transactions after the start of the current transaction are not visible to statements executing in the current transaction. The effect is as if the statements in a transaction get a snapshot of the committed data as it existed at the start of the transaction.
Except when a database is being recovered, SNAPSHOT transactions do not request locks when reading data. SNAPSHOT transactions reading data do not block other transactions from writing data. Transactions writing data do not block SNAPSHOT transactions from reading data.
During the roll-back phase of a database recovery, SNAPSHOT transactions will request a lock if an attempt is made to read data that is locked by another transaction that is being rolled back. The SNAPSHOT transaction is blocked until that transaction has been rolled back. The lock is released immediately after it has been granted.
The ALLOW_SNAPSHOT_ISOLATION database option must be set to ON before you can start a transaction that uses the SNAPSHOT isolation level. If a transaction using the SNAPSHOT isolation level accesses data in multiple databases, ALLOW_SNAPSHOT_ISOLATION must be set to ON in each database.
A transaction cannot be set to SNAPSHOT isolation level that started with another isolation level; doing so will cause the transaction to abort. If a transaction starts in the SNAPSHOT isolation level, you can change it to another isolation level and then back to SNAPSHOT. A transaction starts the first time it accesses data.
A transaction running under SNAPSHOT isolation level can view changes made by that transaction. For example, if the transaction performs an UPDATE on a table and then issues a SELECT statement against the same table, the modified data will be included in the result set.
Number
的初始值是 2
契约(Contract)123
Transaction 1234
具有快照隔离级别 Transaction 5678
具有快照隔离级别 Number = 2
契约(Contract)
123
.
SELECT * FROM ContractRanges WHERE ContractId = 123
.因为我们在 Snapshot
下运行隔离,没有locks
. SELECT
声明,也做 不是 放任何 locks
. Number
契约(Contract)到3
Number
契约(Contract)属性(property)到3
SaveChanges
反过来,这会导致 SQL Server 将创建事务时创建的快照与数据库的当前状态以及在此事务下所做的未提交更改进行比较。由于没有发现任何冲突,它提交了事务,现在 Number
值为 3
在数据库中。 TaskB
,然后,也调用 SaveChanges
,并尝试提交其事务。当 SQL Server 将事务快照值与数据库中当前的值进行比较时,它会发现冲突。在快照中,Number
有一个值 2
现在它的值为 3
.然后,它抛出 Update Exception
. TaskB
这次失败了,因为
TaskA
变异了也在
TaskB
中使用的数据.
Serializable
下运行代码时发生的情况。和
Snapshot
隔离级别,您可以对
fix
做些什么它。
Contract
真的有意义吗?记录。这是我在您的代码中看到的第一个大气味,我会先尝试理解这一点。您可能需要与您的企业讨论这个问题,以了解他们是否真的需要契约(Contract)中的这种并发性。
Serializable
因为这会导致
deadlocks
就像你看到的那样。所以,我们只剩下
Snapshot
隔离。
OptmisticConcurrencyException
这真的由你来处理取决于你和你的企业来决定。
OptmitisticConcurrencyException
出现时重试执行操作。被抛出。这是基于这样的假设:在第二次时,不会有并发事务改变相同的数据并且操作现在将成功。
关于c# - Entity Framework 死锁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26102309/
这个问题在这里已经有了答案: 10年前关闭。 Possible Duplicates: What is a framework? What does it do? Why do we need a f
我在按照 http://msdn.microsoft.com/en-us/data/jj591621.aspx 处的 Microsoft Data Developer 过程启用代码优先迁移时遇到了一些
我正在从 迁移项目 Entity Framework 4.3 在 .net 4 上运行到 Entity Framework 5 在 .net 4.5 上运行。在不做任何更改的情况下,当我尝试运行该项目
我正在使用 Entity Framework 6 并使用 EntityFramework Extended 来执行一些批量更新和批量删除。批量更新和批量删除工作正常,但我还需要知道更新/删除的实体(即
我在实体上添加了一个列,然后从模型中生成数据库或构建解决方案,然后收到一条消息,提示我刚添加的新列未映射。该数据库以前是从模型创建的,没有错误。 当我右键单击Entity并选择Table Mappin
每次我尝试运行我的代码时都会崩溃,因为我尝试启动函数以调用 SDK 的任何部分。 我在构建过程中包含了 FoundationSDK: 并且我在头文件中包含了对 SDK 的引用: 但是每次我运行这个,我
我以前能够毫无问题地提交我的申请。我的工作流程中唯一改变的部分是使用 Sourcetree。在对以下框架进行更新后,我在提交到 iOS App Store 时收到此警告。我还收到一封电子邮件,其中包含
假设我为 Asp.NET Web 应用程序安装了 .NET Framework 2.0、3.0、3.5。 我意识到 Framework 3.0 和 3.5 只是 Framework 2 的扩展,不太清
是否有 SaveChanges 事件在保存更改后但在更新更改跟踪器之前触发? 我正在使用 EF 6。 我需要在某个实体的状态发生变化时执行任务。 我已经覆盖了 SaveChanges 来设置它。我可以
我正在使用一个现有的数据库,并且我已经将其中一个表映射为一个实体(因为我需要映射一个外键)。 因此,在初始化此数据库时,我希望 EF 忽略此实体,因为它已经存在。 我该怎么做? 最佳答案 您应该使用
我有 3 个表需要与 Entity Framework 进行映射,但我不确定解决此问题的正确方法。这是我的 3 个实体: public class User { [Key] public
我首先使用 VS 2010 和 Entity Framework 代码(版本 6)。我有两个实体,每个实体都在自己的上下文中,我想在它们之间创建一对多关系。 上下文 1 具有以下实体: public
我知道 EF 在 CodePlex 上是开源的,但我没有看到当前发布的 5.0 版本的分支。我在哪里可以得到这个源代码? 最佳答案 没有。他们只开源了 post 5 版本。第一次签到可能足够接近,但再
我们目前有一个数据库很大的系统,存储过程既用于CUD又用于查询。数据集用于从 SP 查询中检索结果。 现在我们正在研究使用 Entity Framework 针对同一个数据库开发另一个项目。在查询数据
我有一个每 10 秒运行一次的 Windows 服务......每次运行时,它都会获取一些测试数据,对其进行修改并使用 EntityFramework 将其保存到数据库中。但是,在每一秒运行时,当我尝
我对在我们的场景中仅将 Entity Framework 与存储过程一起使用的合理性有疑问。 我们计划拥有一个 N 层架构,包括 UI、BusinessLayer (BLL)、DataAccessLa
当我使用 Entity Framework 时,我想在上下文中查询出一条记录并将其添加到具有相同架构的另一个上下文中,在查询出记录后,我将其从上下文中分离出来,但是相关实体都没有了,是吗?有什么办法解
我正在使用 Entity Framework 5 构建 ASP.Net MVC4 Web 应用程序。我必须使用现有的 sql server 数据库,但也想使用 Code First,所以我遵循了本教程
在 Entity Framework 4.0 中使用 T4 模板创建 POCO 会丢失什么?为什么使用 Entity Framework 4.0 时的默认行为不创建 POCO? 最佳答案 你会失去很多
我在网上使用 Repository Pattern 和 EF 看了很多例子。但他们都没有真正谈到与相关实体的合作。 就像说用户可以有多个地址。 IUserRepository User CreateU
我是一名优秀的程序员,十分优秀!