Skip to content

如何使用EnableRetryOnFailure #258

@NuxYoung

Description

@NuxYoung

因数据库采用了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.

想问下有合适的解决方案吗?以下是我的配置:

services.AddShardingDbContext<BoxStickerContext>()
    .UseRouteConfig(op =>
    {
        // 此处配置需要分表的表名
        op.AddShardingTableRoute<BoxCodeVirtualTableRoute>();
    }).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<IMigrationsSqlGenerator, ShardingMigrationsSqlGenerator<BoxStickerContext>>();
        });
    }).AddShardingCore();

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions