- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
为了处理 SQL 超时,我尝试使用 SqlAzureExecutionStrategy ( https://msdn.microsoft.com/en-us/data/dn456835.aspx )
我遇到的问题是它阻止了“用户启动的事务”,这似乎是在 EF 中实现“with (nolock)”的推荐方法(http://www.hanselman.com/blog/GettingLINQToSQLAndLINQToEntitiesToUseNOLOCK.aspx、NOLOCK 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/
我正在尝试合并 SQLAzureConnectionStrategy,因为我有一个 Azure Web 应用程序,在某些情况下会通过混合连接调用本地 SQL 数据库。连接有点不稳定,有时读取数据会出现
我已将 Entity Framework 配置为使用 SqlAzureExecutionStrategy。现在我的 NServiceBus 处理程序失败并出现以下错误: NServiceBus.Uni
当 SqlAzureExecutionStrategy 重试 Entity Framework 操作时如何记录?将 transient 故障处理逻辑与常规 SqlConnection 调用一起使用时,
完全异常: System.Data.Entity.Core.EntityCommandExecutionException: An error occurred while executing the
我是一名优秀的程序员,十分优秀!