From 13c76f4be3ea34a9ca8613d97b3e36c061c11dc1 Mon Sep 17 00:00:00 2001 From: xuejiaming <326308290@qq.com> Date: Thu, 11 May 2023 12:25:46 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AF=BB=E5=86=99=E5=88=86=E7=A6=BB=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E9=BB=98=E8=AE=A4=E8=A1=8C=E4=B8=BA=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/WeatherForecastController.cs | 36 +++++++++++ samples/Sample.MySql/Startup.cs | 6 +- .../ShardingConfigOptions.cs | 63 ++++++++++++++++++- .../ShardingReadWriteSeparationOptions.cs | 20 ++++-- ...actVirtualDataSourceConfigurationParams.cs | 2 +- .../IVirtualDataSourceConfigurationParams.cs | 2 +- ...pleVirtualDataSourceConfigurationParams.cs | 4 +- .../VirtualDataSources/VirtualDataSource.cs | 4 +- .../Extensions/ShardingReadWriteExtension.cs | 28 ++++++++- .../Sharding/Abstractions/IReadWriteSwitch.cs | 1 + .../Sharding/ActualConnectionStringManager.cs | 30 +++++++-- .../ReadWriteDefaultEnableBehavior.cs | 15 +++++ .../ShardingReadWriteContext.cs | 23 ++++++- .../ShardingDbContextExecutor.cs | 9 ++- test/ShardingCore.Test/ShardingTest.cs | 2 +- test/ShardingCore.Test/ShardingTestSync.cs | 2 +- test/ShardingCore.Test2x/ShardingTest.cs | 2 +- test/ShardingCore.Test2x/ShardingTestSync.cs | 2 +- test/ShardingCore.Test3x/ShardingTest.cs | 2 +- test/ShardingCore.Test3x/ShardingTestSync.cs | 2 +- test/ShardingCore.Test5x/ShardingTest.cs | 2 +- test/ShardingCore.Test5x/ShardingTestSync.cs | 2 +- test/ShardingCore.Test6x/ShardingTest.cs | 2 +- test/ShardingCore.Test6x/ShardingTestSync.cs | 2 +- 24 files changed, 229 insertions(+), 34 deletions(-) create mode 100644 src/ShardingCore/Sharding/ReadWriteConfigurations/ReadWriteDefaultEnableBehavior.cs diff --git a/samples/Sample.MySql/Controllers/WeatherForecastController.cs b/samples/Sample.MySql/Controllers/WeatherForecastController.cs index 6e571c6c..e3a76af8 100644 --- a/samples/Sample.MySql/Controllers/WeatherForecastController.cs +++ b/samples/Sample.MySql/Controllers/WeatherForecastController.cs @@ -318,5 +318,41 @@ namespace Sample.MySql.Controllers // var sysUserMods2 = await _defaultTableDbContext.Set().FromSqlRaw("select * from SysTest where id='2'").ToListAsync(); return Ok(); } + [HttpGet] + public async Task Get9() + { + var sysUserMod = await _defaultTableDbContext.Set().Where(o=>o.Name=="11231").AllAsync(o=>o.Id=="1123"); + + // var sysUserMods1 = await _defaultTableDbContext.Set().FromSqlRaw("select * from SysUserMod where id='2'").ToListAsync(); + // var sysUserMods2 = await _defaultTableDbContext.Set().FromSqlRaw("select * from SysTest where id='2'").ToListAsync(); + return Ok(); + } + [HttpGet] + public async Task Get10() + { + var sysUserMod1 = await _defaultTableDbContext.Set().Where(o=>o.UserId=="11231").AllAsync(o=>o.Id=="1123"&&string.Compare(o.UserId,"123")>0); + var sysUserMod2 = await _defaultTableDbContext.Set().AllAsync(o=>o.Id=="1123"); + var dateTime = new DateTime(2020,1,1); + var sysUserMod3 = await _defaultTableDbContext.Set().Where(o => o.Time > dateTime) + .ToListAsync(); + + // var sysUserMods1 = await _defaultTableDbContext.Set().FromSqlRaw("select * from SysUserMod where id='2'").ToListAsync(); + // var sysUserMods2 = await _defaultTableDbContext.Set().FromSqlRaw("select * from SysTest where id='2'").ToListAsync(); + return Ok(); + } + [HttpGet] + public async Task Get11() + { + using(var tran = await _defaultTableDbContext.Database.BeginTransactionAsync()) + { + var newGuid = Guid.NewGuid().ToString("n"); + await _defaultTableDbContext.Set().Where(o => o.Id == "11") + .ExecuteUpdateAsync(o => o.SetProperty(x => x.UserId, x => newGuid)); + throw new Exception("123"); + await tran.CommitAsync(); + } + + return Ok(); + } } } diff --git a/samples/Sample.MySql/Startup.cs b/samples/Sample.MySql/Startup.cs index 8a2d1f47..43cb06b3 100644 --- a/samples/Sample.MySql/Startup.cs +++ b/samples/Sample.MySql/Startup.cs @@ -197,7 +197,7 @@ namespace Sample.MySql // app.ApplicationServices.UseAutoTryCompensateTable(); // using (var scope = app.ApplicationServices.CreateScope()) // { - // var defaultShardingDbContext = scope.ServiceProvider.GetService(); + // var defaultShardingDbContext = scope.ServiceProvider.GetService(); // // if (defaultShardingDbContext.Database.GetPendingMigrations().Any()) // { // try @@ -209,6 +209,10 @@ namespace Sample.MySql // { // } // } + // // var shardingRuntimeContext = defaultShardingDbContext.GetShardingRuntimeContext(); + // // var tableRouteManager = shardingRuntimeContext.GetTableRouteManager(); + // // var virtualTableRoute = (SysUserLogByMonthRoute)tableRouteManager.GetRoute(typeof(SysUserLogByMonth)); + // // virtualTableRoute.Append("2021"); // } // // app.ApplicationServices.UseAutoTryCompensateTable(12); diff --git a/src/ShardingCore/Core/ShardingConfigurations/ShardingConfigOptions.cs b/src/ShardingCore/Core/ShardingConfigurations/ShardingConfigOptions.cs index e2c87383..d9287f67 100644 --- a/src/ShardingCore/Core/ShardingConfigurations/ShardingConfigOptions.cs +++ b/src/ShardingCore/Core/ShardingConfigurations/ShardingConfigOptions.cs @@ -115,6 +115,7 @@ namespace ShardingCore.Core.ShardingConfigurations /// 默认优先级建议大于0 /// LatestFirstTime:DbContext缓存,LatestEveryTime:每次都是最新 /// + [Obsolete("plz use AddReadWriteSeparation param use ReadWriteDefaultEnableBehavior")] public void AddReadWriteSeparation( Func>> readWriteSeparationConfigure, ReadStrategyEnum readStrategyEnum, @@ -125,7 +126,31 @@ namespace ShardingCore.Core.ShardingConfigurations ShardingReadWriteSeparationOptions = new ShardingReadWriteSeparationOptions(); ShardingReadWriteSeparationOptions.ReadWriteSeparationConfigure= readWriteSeparationConfigure ?? throw new ArgumentNullException(nameof(readWriteSeparationConfigure)); ShardingReadWriteSeparationOptions.ReadStrategy = readStrategyEnum; - ShardingReadWriteSeparationOptions.DefaultEnable=defaultEnable; + ShardingReadWriteSeparationOptions.DefaultEnableBehavior=defaultEnable?ReadWriteDefaultEnableBehavior.DefaultEnable:ReadWriteDefaultEnableBehavior.DefaultDisable; + ShardingReadWriteSeparationOptions.DefaultPriority= defaultPriority; + ShardingReadWriteSeparationOptions.ReadConnStringGetStrategy= readConnStringGetStrategy; + } + /// + /// 添加读写分离配置 + /// + /// + /// 随机或者轮询 + /// + /// DefaultDisable表示哪怕您添加了读写分离也不会进行读写分离查询,只有需要的时候自行开启,DefaultEnable表示默认查询就是走的读写分离,InTransactionEnable在事务中的查询使用读写分离,InTransactionDisbale在事务中不使用读写分离 + /// 默认优先级建议大于0 + /// LatestFirstTime:DbContext缓存,LatestEveryTime:每次都是最新 + /// + public void AddReadWriteSeparation( + Func>> readWriteSeparationConfigure, + ReadStrategyEnum readStrategyEnum, + ReadWriteDefaultEnableBehavior defaultEnableBehavior = ReadWriteDefaultEnableBehavior.DefaultDisable, + int defaultPriority = 10, + ReadConnStringGetStrategyEnum readConnStringGetStrategy = ReadConnStringGetStrategyEnum.LatestFirstTime) + { + ShardingReadWriteSeparationOptions = new ShardingReadWriteSeparationOptions(); + ShardingReadWriteSeparationOptions.ReadWriteSeparationConfigure= readWriteSeparationConfigure ?? throw new ArgumentNullException(nameof(readWriteSeparationConfigure)); + ShardingReadWriteSeparationOptions.ReadStrategy = readStrategyEnum; + ShardingReadWriteSeparationOptions.DefaultEnableBehavior=defaultEnableBehavior; ShardingReadWriteSeparationOptions.DefaultPriority= defaultPriority; ShardingReadWriteSeparationOptions.ReadConnStringGetStrategy= readConnStringGetStrategy; } @@ -148,6 +173,7 @@ namespace ShardingCore.Core.ShardingConfigurations /// /// /// + [Obsolete("plz use AddReadWriteNodeSeparation param use ReadWriteDefaultEnableBehavior")] public void AddReadWriteNodeSeparation( Func>> readWriteNodeSeparationConfigure, ReadStrategyEnum readStrategyEnum, @@ -158,7 +184,40 @@ namespace ShardingCore.Core.ShardingConfigurations ShardingReadWriteSeparationOptions = new ShardingReadWriteSeparationOptions(); ShardingReadWriteSeparationOptions.ReadWriteNodeSeparationConfigure= readWriteNodeSeparationConfigure ?? throw new ArgumentNullException(nameof(readWriteNodeSeparationConfigure)); ShardingReadWriteSeparationOptions.ReadStrategy = readStrategyEnum; - ShardingReadWriteSeparationOptions.DefaultEnable=defaultEnable; + ShardingReadWriteSeparationOptions.DefaultEnableBehavior=defaultEnable?ReadWriteDefaultEnableBehavior.DefaultEnable:ReadWriteDefaultEnableBehavior.DefaultDisable; + ShardingReadWriteSeparationOptions.DefaultPriority= defaultPriority; + ShardingReadWriteSeparationOptions.ReadConnStringGetStrategy= readConnStringGetStrategy; + } + /// + /// 读写分离配置 和 AddReadWriteSeparation不同的是 + /// 当前配置支持自定义读链接节点命名,命名的好处在于当使用读库链接的时候由于服务器性能的差异 + /// 可以将部分吃性能的查询通过节点名称切换到对应的性能相对较好或者较空闲的读库服务器 + /// ().FirstOrDefaultAsync(); + /// }]]> + /// + /// + /// + /// + /// + /// + /// + public void AddReadWriteNodeSeparation( + Func>> readWriteNodeSeparationConfigure, + ReadStrategyEnum readStrategyEnum, + ReadWriteDefaultEnableBehavior defaultEnableBehavior = ReadWriteDefaultEnableBehavior.DefaultDisable, + int defaultPriority = 10, + ReadConnStringGetStrategyEnum readConnStringGetStrategy = ReadConnStringGetStrategyEnum.LatestFirstTime) + { + ShardingReadWriteSeparationOptions = new ShardingReadWriteSeparationOptions(); + ShardingReadWriteSeparationOptions.ReadWriteNodeSeparationConfigure= readWriteNodeSeparationConfigure ?? throw new ArgumentNullException(nameof(readWriteNodeSeparationConfigure)); + ShardingReadWriteSeparationOptions.ReadStrategy = readStrategyEnum; + ShardingReadWriteSeparationOptions.DefaultEnableBehavior=defaultEnableBehavior; ShardingReadWriteSeparationOptions.DefaultPriority= defaultPriority; ShardingReadWriteSeparationOptions.ReadConnStringGetStrategy= readConnStringGetStrategy; } diff --git a/src/ShardingCore/Core/ShardingConfigurations/ShardingReadWriteSeparationOptions.cs b/src/ShardingCore/Core/ShardingConfigurations/ShardingReadWriteSeparationOptions.cs index c132bc9a..b41af557 100644 --- a/src/ShardingCore/Core/ShardingConfigurations/ShardingReadWriteSeparationOptions.cs +++ b/src/ShardingCore/Core/ShardingConfigurations/ShardingReadWriteSeparationOptions.cs @@ -7,14 +7,26 @@ namespace ShardingCore.Core.ShardingConfigurations { public class ShardingReadWriteSeparationOptions { - public Func>> ReadWriteSeparationConfigure { get; set; } - public Func>> ReadWriteNodeSeparationConfigure { get; set; } + public Func>> ReadWriteSeparationConfigure + { + get; + set; + } + + public Func>> ReadWriteNodeSeparationConfigure + { + get; + set; + } public ReadStrategyEnum ReadStrategy { get; set; } = ReadStrategyEnum.Loop; - public bool DefaultEnable { get; set; } = false; + // public bool DefaultEnable { get; set; } = false; public int DefaultPriority { get; set; } = 10; + public ReadWriteDefaultEnableBehavior DefaultEnableBehavior { get; set; } = + ReadWriteDefaultEnableBehavior.DefaultDisable; + public ReadConnStringGetStrategyEnum ReadConnStringGetStrategy { get; set; } = ReadConnStringGetStrategyEnum.LatestFirstTime; } -} +} \ No newline at end of file diff --git a/src/ShardingCore/Core/VirtualDatabase/VirtualDataSources/Abstractions/AbstractVirtualDataSourceConfigurationParams.cs b/src/ShardingCore/Core/VirtualDatabase/VirtualDataSources/Abstractions/AbstractVirtualDataSourceConfigurationParams.cs index a3a81917..0c583707 100644 --- a/src/ShardingCore/Core/VirtualDatabase/VirtualDataSources/Abstractions/AbstractVirtualDataSourceConfigurationParams.cs +++ b/src/ShardingCore/Core/VirtualDatabase/VirtualDataSources/Abstractions/AbstractVirtualDataSourceConfigurationParams.cs @@ -21,7 +21,7 @@ namespace ShardingCore.Core.VirtualDatabase.VirtualDataSources.Abstractions public virtual IDictionary ExtraDataSources { get; }=new ConcurrentDictionary(); public virtual IDictionary ReadWriteNodeSeparationConfigs { get; } public virtual ReadStrategyEnum? ReadStrategy { get; } - public virtual bool? ReadWriteDefaultEnable { get; } + public virtual ReadWriteDefaultEnableBehavior? ReadWriteDefaultEnableBehavior { get; } public virtual int? ReadWriteDefaultPriority { get; } public virtual ReadConnStringGetStrategyEnum? ReadConnStringGetStrategy { get; } diff --git a/src/ShardingCore/Core/VirtualDatabase/VirtualDataSources/Abstractions/IVirtualDataSourceConfigurationParams.cs b/src/ShardingCore/Core/VirtualDatabase/VirtualDataSources/Abstractions/IVirtualDataSourceConfigurationParams.cs index d1e8903d..7d531414 100644 --- a/src/ShardingCore/Core/VirtualDatabase/VirtualDataSources/Abstractions/IVirtualDataSourceConfigurationParams.cs +++ b/src/ShardingCore/Core/VirtualDatabase/VirtualDataSources/Abstractions/IVirtualDataSourceConfigurationParams.cs @@ -36,7 +36,7 @@ namespace ShardingCore.Core.VirtualDatabase.VirtualDataSources.Abstractions IDictionary ReadWriteNodeSeparationConfigs { get; } ReadStrategyEnum? ReadStrategy { get; } - bool? ReadWriteDefaultEnable { get; } + ReadWriteDefaultEnableBehavior? ReadWriteDefaultEnableBehavior { get; } int? ReadWriteDefaultPriority { get; } /// /// 读写分离链接字符串获取 diff --git a/src/ShardingCore/Core/VirtualDatabase/VirtualDataSources/SimpleVirtualDataSourceConfigurationParams.cs b/src/ShardingCore/Core/VirtualDatabase/VirtualDataSources/SimpleVirtualDataSourceConfigurationParams.cs index ebcfacea..90fa3dac 100644 --- a/src/ShardingCore/Core/VirtualDatabase/VirtualDataSources/SimpleVirtualDataSourceConfigurationParams.cs +++ b/src/ShardingCore/Core/VirtualDatabase/VirtualDataSources/SimpleVirtualDataSourceConfigurationParams.cs @@ -23,7 +23,7 @@ namespace ShardingCore.Core.VirtualDatabase.VirtualDataSources public override IDictionary ExtraDataSources { get; } public override IDictionary ReadWriteNodeSeparationConfigs { get; } public override ReadStrategyEnum? ReadStrategy { get; } - public override bool? ReadWriteDefaultEnable { get; } + public override ReadWriteDefaultEnableBehavior? ReadWriteDefaultEnableBehavior { get; } public override int? ReadWriteDefaultPriority { get; } public override ReadConnStringGetStrategyEnum? ReadConnStringGetStrategy { get; } @@ -57,7 +57,7 @@ namespace ShardingCore.Core.VirtualDatabase.VirtualDataSources } } ReadStrategy = options.ShardingReadWriteSeparationOptions.ReadStrategy; - ReadWriteDefaultEnable = options.ShardingReadWriteSeparationOptions.DefaultEnable; + ReadWriteDefaultEnableBehavior = options.ShardingReadWriteSeparationOptions.DefaultEnableBehavior; ReadWriteDefaultPriority = options.ShardingReadWriteSeparationOptions.DefaultPriority; ReadConnStringGetStrategy = options.ShardingReadWriteSeparationOptions.ReadConnStringGetStrategy; } diff --git a/src/ShardingCore/Core/VirtualDatabase/VirtualDataSources/VirtualDataSource.cs b/src/ShardingCore/Core/VirtualDatabase/VirtualDataSources/VirtualDataSource.cs index 27e11e44..cfc287c4 100644 --- a/src/ShardingCore/Core/VirtualDatabase/VirtualDataSources/VirtualDataSource.cs +++ b/src/ShardingCore/Core/VirtualDatabase/VirtualDataSources/VirtualDataSource.cs @@ -77,9 +77,9 @@ namespace ShardingCore.Core.VirtualDatabase.VirtualDataSources { throw new ArgumentException(nameof(ConfigurationParams.ReadConnStringGetStrategy)); } - if (!ConfigurationParams.ReadWriteDefaultEnable.HasValue) + if (!ConfigurationParams.ReadWriteDefaultEnableBehavior.HasValue) { - throw new ArgumentException(nameof(ConfigurationParams.ReadWriteDefaultEnable)); + throw new ArgumentException(nameof(ConfigurationParams.ReadWriteDefaultEnableBehavior)); } if (!ConfigurationParams.ReadWriteDefaultPriority.HasValue) { diff --git a/src/ShardingCore/Extensions/ShardingReadWriteExtension.cs b/src/ShardingCore/Extensions/ShardingReadWriteExtension.cs index 05de2635..e5464a87 100644 --- a/src/ShardingCore/Extensions/ShardingReadWriteExtension.cs +++ b/src/ShardingCore/Extensions/ShardingReadWriteExtension.cs @@ -61,7 +61,7 @@ namespace ShardingCore.Extensions } } - shardingDbContextExecutor.ReadWriteSeparation = readOnly; + shardingDbContextExecutor.ReadWriteSeparationBehavior = ReadWriteDefaultEnableBehavior.DefaultEnable; } public static void SetReadWriteSeparation(this ShardingReadWriteContext shardingReadWriteContext, int priority, @@ -76,6 +76,7 @@ namespace ShardingCore.Extensions /// /// /// + [Obsolete(" plz use CurrentIsReadWriteSeparationBehavior")] public static bool CurrentIsReadWriteSeparation(this IShardingDbContext shardingDbContext) { if (shardingDbContext.IsUseReadWriteSeparation()) @@ -101,5 +102,30 @@ namespace ShardingCore.Extensions return false; } + public static ReadWriteDefaultEnableBehavior CurrentIsReadWriteSeparationBehavior(this IShardingDbContext shardingDbContext) + { + if (shardingDbContext.IsUseReadWriteSeparation()) + { + var shardingRuntimeContext = ((DbContext)shardingDbContext).GetShardingRuntimeContext(); + var shardingDbContextExecutor = shardingDbContext.GetShardingExecutor(); + var shardingReadWriteManager = shardingRuntimeContext.GetService(); + var shardingReadWriteContext = shardingReadWriteManager.GetCurrent(); + if (shardingReadWriteContext != null) + { + if (shardingReadWriteContext.DefaultPriority > shardingDbContextExecutor.ReadWriteSeparationPriority) + { + return shardingReadWriteContext.DefaultEnableBehavior; + } + else + { + return shardingDbContextExecutor.ReadWriteSeparationBehavior; + } + } + + return shardingDbContextExecutor.ReadWriteSeparationBehavior; + } + + return ReadWriteDefaultEnableBehavior.DefaultDisable; + } } } \ No newline at end of file diff --git a/src/ShardingCore/Sharding/Abstractions/IReadWriteSwitch.cs b/src/ShardingCore/Sharding/Abstractions/IReadWriteSwitch.cs index d27ec6de..d6216c19 100644 --- a/src/ShardingCore/Sharding/Abstractions/IReadWriteSwitch.cs +++ b/src/ShardingCore/Sharding/Abstractions/IReadWriteSwitch.cs @@ -17,5 +17,6 @@ namespace ShardingCore.Sharding.Abstractions { int ReadWriteSeparationPriority { get; set; } bool ReadWriteSeparation { get; set; } + ReadWriteDefaultEnableBehavior ReadWriteSeparationBehavior { get; set; } } } diff --git a/src/ShardingCore/Sharding/ActualConnectionStringManager.cs b/src/ShardingCore/Sharding/ActualConnectionStringManager.cs index 829505a1..3af472a2 100644 --- a/src/ShardingCore/Sharding/ActualConnectionStringManager.cs +++ b/src/ShardingCore/Sharding/ActualConnectionStringManager.cs @@ -19,23 +19,25 @@ namespace ShardingCore.Sharding */ public class ActualConnectionStringManager { + public DbContext ShellDbContext { get; } private readonly bool _useReadWriteSeparation; private readonly IShardingReadWriteManager _shardingReadWriteManager; private readonly IVirtualDataSource _virtualDataSource; public int ReadWriteSeparationPriority { get; set; } - public bool ReadWriteSeparation { get; set; } + public ReadWriteDefaultEnableBehavior ReadWriteSeparation { get; set; } public ReadStrategyEnum ReadStrategy { get; set; } public ReadConnStringGetStrategyEnum ReadConnStringGetStrategy { get; set; } private string _cacheConnectionString; - public ActualConnectionStringManager(IShardingReadWriteManager shardingReadWriteManager,IVirtualDataSource virtualDataSource) + public ActualConnectionStringManager(IShardingReadWriteManager shardingReadWriteManager,IVirtualDataSource virtualDataSource,DbContext shellDbContext) { + ShellDbContext = shellDbContext; _shardingReadWriteManager = shardingReadWriteManager; _virtualDataSource=virtualDataSource; _useReadWriteSeparation = virtualDataSource.ConnectionStringManager is ReadWriteConnectionStringManager; if (_useReadWriteSeparation) { ReadWriteSeparationPriority = virtualDataSource.ConfigurationParams.ReadWriteDefaultPriority.GetValueOrDefault(); - ReadWriteSeparation = virtualDataSource.ConfigurationParams.ReadWriteDefaultEnable.GetValueOrDefault(); + ReadWriteSeparation = virtualDataSource.ConfigurationParams.ReadWriteDefaultEnableBehavior.GetValueOrDefault(ReadWriteDefaultEnableBehavior.DefaultDisable); ReadStrategy = virtualDataSource.ConfigurationParams.ReadStrategy.GetValueOrDefault(); ReadConnStringGetStrategy = virtualDataSource.ConfigurationParams.ReadConnStringGetStrategy.GetValueOrDefault(); } @@ -62,9 +64,25 @@ namespace ShardingCore.Sharding return _virtualDataSource.GetConnectionString(dataSourceName); } + private bool useReadWriteSeparation(ReadWriteDefaultEnableBehavior behavior,bool inTransaction) + { + if (behavior == ReadWriteDefaultEnableBehavior.DefaultEnable) + { + return true; + } + + if (behavior == ReadWriteDefaultEnableBehavior.OutTransactionEnable) + { + return !inTransaction; + } + + return false; + } + private string GetReadWriteSeparationConnectString(string dataSourceName) { - var support = ReadWriteSeparation; + bool inTransaction=ShellDbContext.Database.CurrentTransaction != null; + var support = useReadWriteSeparation(ReadWriteSeparation,inTransaction); string readNodeName = null; var hasConfig = false; var shardingReadWriteContext = _shardingReadWriteManager.GetCurrent(); @@ -72,8 +90,8 @@ namespace ShardingCore.Sharding { var dbFirst = ReadWriteSeparationPriority >= shardingReadWriteContext.DefaultPriority; support = dbFirst - ? ReadWriteSeparation - : shardingReadWriteContext.DefaultReadEnable; + ? useReadWriteSeparation(ReadWriteSeparation,inTransaction) + : useReadWriteSeparation(shardingReadWriteContext.DefaultEnableBehavior,inTransaction); if (!dbFirst&& support) { hasConfig = shardingReadWriteContext.TryGetDataSourceReadNode(dataSourceName, out readNodeName); diff --git a/src/ShardingCore/Sharding/ReadWriteConfigurations/ReadWriteDefaultEnableBehavior.cs b/src/ShardingCore/Sharding/ReadWriteConfigurations/ReadWriteDefaultEnableBehavior.cs new file mode 100644 index 00000000..31b9ddaa --- /dev/null +++ b/src/ShardingCore/Sharding/ReadWriteConfigurations/ReadWriteDefaultEnableBehavior.cs @@ -0,0 +1,15 @@ +namespace ShardingCore.Sharding.ReadWriteConfigurations +{ + /** + * 读写分离默认行为 + */ + public enum ReadWriteDefaultEnableBehavior + { + //默认不启用 + DefaultDisable, + //默认启用 + DefaultEnable, + //不在事务中启用 + OutTransactionEnable + } +} \ No newline at end of file diff --git a/src/ShardingCore/Sharding/ReadWriteConfigurations/ShardingReadWriteContext.cs b/src/ShardingCore/Sharding/ReadWriteConfigurations/ShardingReadWriteContext.cs index 32170aff..2b5be95a 100644 --- a/src/ShardingCore/Sharding/ReadWriteConfigurations/ShardingReadWriteContext.cs +++ b/src/ShardingCore/Sharding/ReadWriteConfigurations/ShardingReadWriteContext.cs @@ -13,13 +13,27 @@ namespace ShardingCore.Sharding.ReadWriteConfigurations */ public class ShardingReadWriteContext { - public bool DefaultReadEnable { get; set; } + [Obsolete("use DefaultEnableBehavior")] + public bool DefaultReadEnable + { + get { return DefaultEnableBehavior == ReadWriteDefaultEnableBehavior.DefaultEnable; } + set + { + DefaultEnableBehavior = + value + ? ReadWriteDefaultEnableBehavior.DefaultEnable + : ReadWriteDefaultEnableBehavior.DefaultDisable; + } + } + + public ReadWriteDefaultEnableBehavior DefaultEnableBehavior { get; set; } public int DefaultPriority { get; set; } private readonly Dictionary _dataSourceReadNode; private ShardingReadWriteContext() { DefaultReadEnable = false; + DefaultEnableBehavior = ReadWriteDefaultEnableBehavior.DefaultDisable; DefaultPriority = 0; _dataSourceReadNode = new Dictionary(); } @@ -28,6 +42,7 @@ namespace ShardingCore.Sharding.ReadWriteConfigurations { return new ShardingReadWriteContext(); } + /// /// 添加数据源对应读节点获取名称 /// @@ -40,18 +55,20 @@ namespace ShardingCore.Sharding.ReadWriteConfigurations { return false; } + _dataSourceReadNode.Add(dataSource, readNodeName); return true; } + /// /// 尝试获取对应数据源的读节点名称 /// /// /// /// - public bool TryGetDataSourceReadNode(string dataSource,out string readNodeName) + public bool TryGetDataSourceReadNode(string dataSource, out string readNodeName) { return _dataSourceReadNode.TryGetValue(dataSource, out readNodeName); } } -} +} \ No newline at end of file diff --git a/src/ShardingCore/Sharding/ShardingDbContextExecutors/ShardingDbContextExecutor.cs b/src/ShardingCore/Sharding/ShardingDbContextExecutors/ShardingDbContextExecutor.cs index 987b9c1c..2aaff47e 100644 --- a/src/ShardingCore/Sharding/ShardingDbContextExecutors/ShardingDbContextExecutor.cs +++ b/src/ShardingCore/Sharding/ShardingDbContextExecutors/ShardingDbContextExecutor.cs @@ -66,7 +66,14 @@ namespace ShardingCore.Sharding.ShardingDbContextExecutors set => _actualConnectionStringManager.ReadWriteSeparationPriority = value; } + [Obsolete("use ReadWriteSeparationBehavior")] public bool ReadWriteSeparation + { + get => _actualConnectionStringManager.ReadWriteSeparation==ReadWriteDefaultEnableBehavior.DefaultEnable; + set => _actualConnectionStringManager.ReadWriteSeparation = value?ReadWriteDefaultEnableBehavior.DefaultEnable:ReadWriteDefaultEnableBehavior.DefaultDisable; + } + + public ReadWriteDefaultEnableBehavior ReadWriteSeparationBehavior { get => _actualConnectionStringManager.ReadWriteSeparation; set => _actualConnectionStringManager.ReadWriteSeparation = value; @@ -92,7 +99,7 @@ namespace ShardingCore.Sharding.ShardingDbContextExecutors var loggerFactory = shardingProvider.GetRequiredService(); _logger = loggerFactory.CreateLogger(); _actualConnectionStringManager = - new ActualConnectionStringManager(shardingReadWriteManager, _virtualDataSource); + new ActualConnectionStringManager(shardingReadWriteManager, _virtualDataSource,_shardingDbContext); } #region create db context diff --git a/test/ShardingCore.Test/ShardingTest.cs b/test/ShardingCore.Test/ShardingTest.cs index 247f2279..9b545234 100644 --- a/test/ShardingCore.Test/ShardingTest.cs +++ b/test/ShardingCore.Test/ShardingTest.cs @@ -58,7 +58,7 @@ namespace ShardingCore.Test _shardingRouteManager = shardingRuntimeContext.GetShardingRouteManager(); _shardingReadWriteManager = shardingRuntimeContext.GetShardingReadWriteManager(); _virtualDataSource = shardingRuntimeContext.GetVirtualDataSource(); - _connectionStringManager = new ActualConnectionStringManager(_shardingReadWriteManager, _virtualDataSource); + _connectionStringManager = new ActualConnectionStringManager(_shardingReadWriteManager, _virtualDataSource,virtualDbContext); _configuration = configuration; _entityMetadataManager = shardingRuntimeContext.GetEntityMetadataManager(); _tableRouteManager = shardingRuntimeContext.GetTableRouteManager(); diff --git a/test/ShardingCore.Test/ShardingTestSync.cs b/test/ShardingCore.Test/ShardingTestSync.cs index 75fde0d8..3532f625 100644 --- a/test/ShardingCore.Test/ShardingTestSync.cs +++ b/test/ShardingCore.Test/ShardingTestSync.cs @@ -52,7 +52,7 @@ namespace ShardingCore.Test _shardingRouteManager = shardingRuntimeContext.GetShardingRouteManager(); _shardingReadWriteManager = shardingRuntimeContext.GetShardingReadWriteManager(); _virtualDataSource = shardingRuntimeContext.GetVirtualDataSource(); - _connectionStringManager = new ActualConnectionStringManager(_shardingReadWriteManager,_virtualDataSource); + _connectionStringManager = new ActualConnectionStringManager(_shardingReadWriteManager,_virtualDataSource,virtualDbContext); _configuration = configuration; _entityMetadataManager = shardingRuntimeContext.GetEntityMetadataManager(); _tableRouteManager = shardingRuntimeContext.GetTableRouteManager(); diff --git a/test/ShardingCore.Test2x/ShardingTest.cs b/test/ShardingCore.Test2x/ShardingTest.cs index 0695d151..e1ffc3e2 100644 --- a/test/ShardingCore.Test2x/ShardingTest.cs +++ b/test/ShardingCore.Test2x/ShardingTest.cs @@ -55,7 +55,7 @@ namespace ShardingCore.Test2x _shardingRouteManager = shardingRuntimeContext.GetShardingRouteManager(); _shardingReadWriteManager = shardingRuntimeContext.GetShardingReadWriteManager(); _virtualDataSource = shardingRuntimeContext.GetVirtualDataSource(); - _connectionStringManager = new ActualConnectionStringManager(_shardingReadWriteManager,_virtualDataSource); + _connectionStringManager = new ActualConnectionStringManager(_shardingReadWriteManager,_virtualDataSource,virtualDbContext); _configuration = configuration; _entityMetadataManager = shardingRuntimeContext.GetEntityMetadataManager(); _tableRouteManager = shardingRuntimeContext.GetTableRouteManager(); diff --git a/test/ShardingCore.Test2x/ShardingTestSync.cs b/test/ShardingCore.Test2x/ShardingTestSync.cs index d3a760ad..c200af2b 100644 --- a/test/ShardingCore.Test2x/ShardingTestSync.cs +++ b/test/ShardingCore.Test2x/ShardingTestSync.cs @@ -52,7 +52,7 @@ namespace ShardingCore.Test2x _shardingRouteManager = shardingRuntimeContext.GetShardingRouteManager(); _shardingReadWriteManager = shardingRuntimeContext.GetShardingReadWriteManager(); _virtualDataSource = shardingRuntimeContext.GetVirtualDataSource(); - _connectionStringManager = new ActualConnectionStringManager(_shardingReadWriteManager,_virtualDataSource); + _connectionStringManager = new ActualConnectionStringManager(_shardingReadWriteManager,_virtualDataSource,virtualDbContext); _configuration = configuration; _entityMetadataManager = shardingRuntimeContext.GetEntityMetadataManager(); _tableRouteManager = shardingRuntimeContext.GetTableRouteManager(); diff --git a/test/ShardingCore.Test3x/ShardingTest.cs b/test/ShardingCore.Test3x/ShardingTest.cs index 6d6b2425..a270f661 100644 --- a/test/ShardingCore.Test3x/ShardingTest.cs +++ b/test/ShardingCore.Test3x/ShardingTest.cs @@ -55,7 +55,7 @@ namespace ShardingCore.Test3x _shardingRouteManager = shardingRuntimeContext.GetShardingRouteManager(); _shardingReadWriteManager = shardingRuntimeContext.GetShardingReadWriteManager(); _virtualDataSource = shardingRuntimeContext.GetVirtualDataSource(); - _connectionStringManager = new ActualConnectionStringManager(_shardingReadWriteManager,_virtualDataSource); + _connectionStringManager = new ActualConnectionStringManager(_shardingReadWriteManager,_virtualDataSource,virtualDbContext); _configuration = configuration; _entityMetadataManager = shardingRuntimeContext.GetEntityMetadataManager(); _tableRouteManager = shardingRuntimeContext.GetTableRouteManager(); diff --git a/test/ShardingCore.Test3x/ShardingTestSync.cs b/test/ShardingCore.Test3x/ShardingTestSync.cs index b018b40e..35508ea8 100644 --- a/test/ShardingCore.Test3x/ShardingTestSync.cs +++ b/test/ShardingCore.Test3x/ShardingTestSync.cs @@ -52,7 +52,7 @@ namespace ShardingCore.Test3x _shardingRouteManager = shardingRuntimeContext.GetShardingRouteManager(); _shardingReadWriteManager = shardingRuntimeContext.GetShardingReadWriteManager(); _virtualDataSource = shardingRuntimeContext.GetVirtualDataSource(); - _connectionStringManager = new ActualConnectionStringManager(_shardingReadWriteManager,_virtualDataSource); + _connectionStringManager = new ActualConnectionStringManager(_shardingReadWriteManager,_virtualDataSource,virtualDbContext); _configuration = configuration; _entityMetadataManager = shardingRuntimeContext.GetEntityMetadataManager(); _tableRouteManager = shardingRuntimeContext.GetTableRouteManager(); diff --git a/test/ShardingCore.Test5x/ShardingTest.cs b/test/ShardingCore.Test5x/ShardingTest.cs index 36d3d1eb..5b8cb19b 100644 --- a/test/ShardingCore.Test5x/ShardingTest.cs +++ b/test/ShardingCore.Test5x/ShardingTest.cs @@ -55,7 +55,7 @@ namespace ShardingCore.Test5x _shardingRouteManager = shardingRuntimeContext.GetShardingRouteManager(); _shardingReadWriteManager = shardingRuntimeContext.GetShardingReadWriteManager(); _virtualDataSource = shardingRuntimeContext.GetVirtualDataSource(); - _connectionStringManager = new ActualConnectionStringManager(_shardingReadWriteManager,_virtualDataSource); + _connectionStringManager = new ActualConnectionStringManager(_shardingReadWriteManager,_virtualDataSource,virtualDbContext); _configuration = configuration; _entityMetadataManager = shardingRuntimeContext.GetEntityMetadataManager(); _tableRouteManager = shardingRuntimeContext.GetTableRouteManager(); diff --git a/test/ShardingCore.Test5x/ShardingTestSync.cs b/test/ShardingCore.Test5x/ShardingTestSync.cs index 5e14e836..44fa6b2d 100644 --- a/test/ShardingCore.Test5x/ShardingTestSync.cs +++ b/test/ShardingCore.Test5x/ShardingTestSync.cs @@ -52,7 +52,7 @@ namespace ShardingCore.Test5x _shardingRouteManager = shardingRuntimeContext.GetShardingRouteManager(); _shardingReadWriteManager = shardingRuntimeContext.GetShardingReadWriteManager(); _virtualDataSource = shardingRuntimeContext.GetVirtualDataSource(); - _connectionStringManager = new ActualConnectionStringManager(_shardingReadWriteManager,_virtualDataSource); + _connectionStringManager = new ActualConnectionStringManager(_shardingReadWriteManager,_virtualDataSource,virtualDbContext); _configuration = configuration; _entityMetadataManager = shardingRuntimeContext.GetEntityMetadataManager(); _tableRouteManager = shardingRuntimeContext.GetTableRouteManager(); diff --git a/test/ShardingCore.Test6x/ShardingTest.cs b/test/ShardingCore.Test6x/ShardingTest.cs index 4be95fed..49005dab 100644 --- a/test/ShardingCore.Test6x/ShardingTest.cs +++ b/test/ShardingCore.Test6x/ShardingTest.cs @@ -58,7 +58,7 @@ namespace ShardingCore.Test6x _shardingRouteManager = shardingRuntimeContext.GetShardingRouteManager(); _shardingReadWriteManager = shardingRuntimeContext.GetShardingReadWriteManager(); _virtualDataSource = shardingRuntimeContext.GetVirtualDataSource(); - _connectionStringManager = new ActualConnectionStringManager(_shardingReadWriteManager, _virtualDataSource); + _connectionStringManager = new ActualConnectionStringManager(_shardingReadWriteManager, _virtualDataSource,virtualDbContext); _configuration = configuration; _entityMetadataManager = shardingRuntimeContext.GetEntityMetadataManager(); _tableRouteManager = shardingRuntimeContext.GetTableRouteManager(); diff --git a/test/ShardingCore.Test6x/ShardingTestSync.cs b/test/ShardingCore.Test6x/ShardingTestSync.cs index 15b0d683..88c44d2e 100644 --- a/test/ShardingCore.Test6x/ShardingTestSync.cs +++ b/test/ShardingCore.Test6x/ShardingTestSync.cs @@ -52,7 +52,7 @@ namespace ShardingCore.Test6x _shardingRouteManager = shardingRuntimeContext.GetShardingRouteManager(); _shardingReadWriteManager = shardingRuntimeContext.GetShardingReadWriteManager(); _virtualDataSource = shardingRuntimeContext.GetVirtualDataSource(); - _connectionStringManager = new ActualConnectionStringManager(_shardingReadWriteManager,_virtualDataSource); + _connectionStringManager = new ActualConnectionStringManager(_shardingReadWriteManager,_virtualDataSource,virtualDbContext); _configuration = configuration; _entityMetadataManager = shardingRuntimeContext.GetEntityMetadataManager(); _tableRouteManager = shardingRuntimeContext.GetTableRouteManager();