修复[#72] 未分表与分表提交先后顺序问题
This commit is contained in:
parent
058df99f26
commit
5e49aa2dcf
|
@ -14,6 +14,7 @@ using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions;
|
|||
using ShardingCore.DbContexts;
|
||||
using ShardingCore.DbContexts.ShardingDbContexts;
|
||||
using ShardingCore.Exceptions;
|
||||
using ShardingCore.Extensions;
|
||||
using ShardingCore.Infrastructures;
|
||||
using ShardingCore.Sharding.Abstractions;
|
||||
|
||||
|
@ -28,6 +29,7 @@ namespace ShardingCore.Sharding.ShardingDbContextExecutors
|
|||
*/
|
||||
public class DataSourceDbContext<TShardingDbContext> : IDataSourceDbContext where TShardingDbContext : DbContext, IShardingDbContext
|
||||
{
|
||||
private static readonly IComparer<string> _comparer = new NoShardingFirstComparer();
|
||||
public bool IsDefault { get; }
|
||||
public int DbContextCount => _dataSourceDbContexts.Count;
|
||||
private readonly IShardingDbContextOptionsBuilderConfig<TShardingDbContext> _shardingDbContextOptionsBuilderConfig;
|
||||
|
@ -39,8 +41,8 @@ namespace ShardingCore.Sharding.ShardingDbContextExecutors
|
|||
/// </summary>
|
||||
public string DataSourceName { get; }
|
||||
|
||||
private ConcurrentDictionary<string, DbContext> _dataSourceDbContexts =
|
||||
new ConcurrentDictionary<string, DbContext>();
|
||||
private SortedDictionary<string, DbContext> _dataSourceDbContexts =
|
||||
new SortedDictionary<string, DbContext>(_comparer);
|
||||
|
||||
|
||||
private bool _isBeginTransaction => _shardingDbContext.Database.CurrentTransaction != null;
|
||||
|
@ -117,7 +119,7 @@ namespace ShardingCore.Sharding.ShardingDbContextExecutors
|
|||
}
|
||||
else
|
||||
{
|
||||
if (_dataSourceDbContexts.IsEmpty)
|
||||
if (_dataSourceDbContexts.IsEmpty())
|
||||
{
|
||||
var connectionString =
|
||||
_actualConnectionStringManager.GetConnectionString(DataSourceName, true);
|
||||
|
@ -166,7 +168,7 @@ namespace ShardingCore.Sharding.ShardingDbContextExecutors
|
|||
if (!_dataSourceDbContexts.TryGetValue(cacheKey, out var dbContext))
|
||||
{
|
||||
dbContext = _shardingDbContextFactory.Create(CreateShareDbContextOptionsBuilder(), routeTail);
|
||||
_dataSourceDbContexts.TryAdd(cacheKey, dbContext);
|
||||
_dataSourceDbContexts.Add(cacheKey, dbContext);
|
||||
ShardingDbTransaction();
|
||||
}
|
||||
return dbContext;
|
||||
|
@ -203,7 +205,7 @@ namespace ShardingCore.Sharding.ShardingDbContextExecutors
|
|||
{
|
||||
if (!IsDefault)
|
||||
{
|
||||
if (!_dataSourceDbContexts.IsEmpty)
|
||||
if (!_dataSourceDbContexts.IsEmpty())
|
||||
{
|
||||
var isolationLevel = _shardingContextTransaction.GetDbTransaction().IsolationLevel;
|
||||
var firstTransaction = _dataSourceDbContexts.Values
|
||||
|
|
|
@ -0,0 +1,32 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails;
|
||||
using ShardingCore.Extensions;
|
||||
|
||||
namespace ShardingCore.Sharding.ShardingDbContextExecutors
|
||||
{
|
||||
/*
|
||||
* @Author: xjm
|
||||
* @Description:
|
||||
* @Date: Sunday, 28 November 2021 21:47:38
|
||||
* @Email: 326308290@qq.com
|
||||
*/
|
||||
public class NoShardingFirstComparer:IComparer<string>
|
||||
{
|
||||
private readonly string _defaultTail;
|
||||
|
||||
public NoShardingFirstComparer()
|
||||
{
|
||||
_defaultTail = new SingleQueryRouteTail(string.Empty).GetRouteTailIdentity();
|
||||
}
|
||||
|
||||
public int Compare(string? x, string? y)
|
||||
{
|
||||
if (_defaultTail.Equals(x))
|
||||
return -1;
|
||||
if (_defaultTail.Equals(y))
|
||||
return -1;
|
||||
return x.SafeCompareToWith(y,true);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue