dotnetcore / dotnetcore/sharding-core

如何使用EnableRetryOnFailure

Open
#258 9 comments 0 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
1.3k
Forks
183
Avg merge
7h 43m
Merged PRs (30d)
1

Description

因数据库采用了AlwaysOn作为高可用方案,主从切换的时候会导致数据库连接不可用。在未使用ShardingCore前,采用了```EnableRetryOnFailure```,允许重试。但是使用以后,执行```SaveChangesAsync()```的时候,会报异常
```
The configured execution strategy 'SqlServerRetryingExecutionStrategy' does not support user-initiated transactions. Use the execution strategy returned by 'DbContext.Database.CreateExecutionStrategy()' to execute all the operations in the transaction as a retriable unit.
```
想问下有合适的解决方案吗?以下是我的配置:
```C#
services.AddShardingDbContext()
.UseRouteConfig(op =>
{
// 此处配置需要分表的表名
op.AddShardingTableRoute();
}).UseConfig((sp, op) =>
{
op.UseShardingQuery((conn, builder) =>
{
builder.UseSqlServer(conn, builder =>
{
builder.EnableRetryOnFailure(5, TimeSpan.FromSeconds(10), null);
});
});
op.UseShardingTransaction((conn, builder) =>
{
builder.UseSqlServer(conn, builder =>
{
builder.EnableRetryOnFailure(5, TimeSpan.FromSeconds(10), null);
});
});
op.AddDefaultDataSource("BoxStickerContext", configuration.GetConnectionString("BoxStickerConnection"));
op.UseShardingMigrationConfigure(op =>
{
op.ReplaceService>();
});
}).AddShardingCore();
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.