gpt4 book ai didi

linq - 如何使用SqlAzureExecutionStrategy和 "Nolock"

转载 作者:行者123 更新时间:2023-12-04 05:48:20 31 4
gpt4 key购买 nike

为了处理 SQL 超时,我尝试使用 SqlAzureExecutionStrategy ( https://msdn.microsoft.com/en-us/data/dn456835.aspx )

我遇到的问题是它阻止了“用户启动的事务”,这似乎是在 EF 中实现“with (nolock)”的推荐方法(http://www.hanselman.com/blog/GettingLINQToSQLAndLINQToEntitiesToUseNOLOCK.aspxNOLOCK with Linq to SQL)。

示例代码

    public AspnetUser GetAspnetUserByUserName(string userName)
{
using (var tx = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions() { IsolationLevel = IsolationLevel.ReadUncommitted }))
{
return context.AspnetUsers.Where(x => x.UserName == userName).FirstOrDefault();
}
}

抛出错误

The configured execution strategy 'SqlAzureExecutionStrategy' does not support user initiated transactions. See http://go.microsoft.com/fwlink/?LinkId=309381 for additional information.

我看到的答案说要在每次调用的基础上关闭 SqlAzureExecutionStrategy,但是如果我所有的读取都忽略了该策略,那么这将违背使用它的目的。可以同时拥有“NoLock”和 SqlAzureExecutionStrategy

最佳答案

SqlAzureExecutionStrategy 不支持在要重试的操作之外启动的事务。要解决此限制,您需要暂停策略,创建事务范围并将工作作为手动传递给执行策略以重试的操作来执行:

public AspnetUser GetAspnetUserByUserName(string userName)
{
new SuspendableSqlAzureExecutionStrategy().Execute(() =>
{
using (var tx = new TransactionScope(
TransactionScopeOption.Required,
new TransactionOptions() { IsolationLevel = IsolationLevel.ReadUncommitted }))
{
return context.AspnetUsers.Where(x => x.UserName == userName).FirstOrDefault();
}
});
}

在这里,我使用 https://msdn.microsoft.com/en-us/data/dn307226 中的可暂停策略的替代方案。这将自动暂停任何嵌套调用:

using System.Data.Entity.Infrastructure;
using System.Data.Entity.SqlServer;
using System.Data.Entity.Utilities;
using System.Runtime.Remoting.Messaging;
using System.Threading;
using System.Threading.Tasks;

public class SuspendableSqlAzureExecutionStrategy : IDbExecutionStrategy
{
private readonly IDbExecutionStrategy _azureExecutionStrategy;

public SuspendableSqlAzureExecutionStrategy()
{
_azureExecutionStrategy = new SqlAzureExecutionStrategy();
}

private static bool Suspend
{
get { return (bool?)CallContext.LogicalGetData("SuspendExecutionStrategy") ?? false; }
set { CallContext.LogicalSetData("SuspendExecutionStrategy", value); }
}

public bool RetriesOnFailure
{
get { return !Suspend; }
}

public virtual void Execute(Action operation)
{
if (!RetriesOnFailure)
{
operation();
return;
}

try
{
Suspend = true;
_azureExecutionStrategy.Execute(operation);
}
finally
{
Suspend = false;
}
}

public virtual TResult Execute<TResult>(Func<TResult> operation)
{
if (!RetriesOnFailure)
{
return operation();
}

try
{
Suspend = true;
return _azureExecutionStrategy.Execute(operation);
}
finally
{
Suspend = false;
}
}

public virtual async Task ExecuteAsync(Func<Task> operation, CancellationToken cancellationToken)
{
if (!RetriesOnFailure)
{
await operation();
return;
}

try
{
Suspend = true;
await _azureExecutionStrategy.ExecuteAsync(operation, cancellationToken);
}
finally
{
Suspend = false;
}
}

public virtual async Task<TResult> ExecuteAsync<TResult>(Func<Task<TResult>> operation, CancellationToken cancellationToken)
{
if (!RetriesOnFailure)
{
return await operation();
}

try
{
Suspend = true;
return await _azureExecutionStrategy.ExecuteAsync(operation, cancellationToken);
}
finally
{
Suspend = false;
}
}
}

public class MyConfiguration : DbConfiguration
{
public MyConfiguration()
{
SetExecutionStrategy("System.Data.SqlClient", () => new SuspendableSqlAzureExecutionStrategy());
}
}

关于linq - 如何使用SqlAzureExecutionStrategy和 "Nolock",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31899382/

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