gpt4 book ai didi

c# - 无法提升具有 IsolationLevel 快照的事务

转载 作者:行者123 更新时间:2023-11-30 17:01:52 24 4
gpt4 key购买 nike

我试图通过 Entity v.4.0.30319 围绕我对存储过程的调用包装一个 TransactionScope。我一直遇到以下异常:

Transactions with IsolationLevel Snapshot cannot be promoted.

我该如何解决这个问题?

底层存储过程基本上是一个插入表的大语句。

我的代码如下:

using (TransactionScope scope = new TransactionScope(TransactionScopeOption.RequiresNew, GetTransactionOptions()))
{
int? status;
status = GetStatusIDFromEnum(newMatterCredential);

using (MatterCredentialsEntities db = new MatterCredentialsEntities())
{
DateTime? objDateAnnounced = GenerateNullForDateTime(newMatterCredential.DateAnnounced);
DateTime? objDateClosed = GenerateNullForDateTime(newMatterCredential.DateClosed);
DateTime? objDateFinancialClosed = GenerateNullForDateTime(newMatterCredential.DateFinancialClosed);
db.prcCreateCredential(Common.GetUserProfID(), newMatterCredential.MatterID, status, newMatterCredential.DescriptionSummary, newMatterCredential.DescriptionDetailed, newMatterCredential.BusinessEntitySectorID, newMatterCredential.BusinessEntityRoleID, newMatterCredential.UNGeographyID, newMatterCredential.ProjectName, newMatterCredential.ClientIndustryId, newMatterCredential.TransactionValue, newMatterCredential.TransactionCurrencyID, newMatterCredential.OtherParties, newMatterCredential.LegalAdvisers, newMatterCredential.DateAnnounced, newMatterCredential.DateClosed, newMatterCredential.DateFinancialClosed, newMatterCredential.Award, newMatterCredential.NotifyPartner, newMatterCredential.Notes);
}

scope.Complete();
}

public static TransactionOptions GetTransactionOptions()
{
TransactionOptions tranOpt = new TransactionOptions();
tranOpt.IsolationLevel = IsolationLevel.Snapshot;
return tranOpt;
}

最佳答案

MSDN 说您不能使用快照隔离来提升事务。

MSDN - IsolationLevel Enumeration

快照 - 可以读取 volatile 数据。在事务修改数据之前,它会验证另一个事务是否在最初读取数据后更改了数据。如果数据已更新,则会引发错误。这允许事务获取先前提交的数据值。当您尝试提升使用此隔离级别创建的事务时,会抛出 InvalidOperationException 并显示错误消息:

Transactions with IsolationLevel Snapshot cannot be promoted

如果这是它参与的更大事务的一部分,那么自从您开始事务以来,一定有其他东西在更改数据

我建议将事务更改为可序列化。

public static TransactionOptions GetTransactionOptions()
{
TransactionOptions tranOpt = new TransactionOptions();
tranOpt.IsolationLevel = IsolationLevel.Serializable;
return tranOpt;
}

编辑:请参阅下文,确保您正在运行 MSDTC,因为这要创建分布式事务。

关于c# - 无法提升具有 IsolationLevel 快照的事务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20557098/

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