修复启动创建导致的部分dbcontext没有回收的bug
This commit is contained in:
parent
aecc1239a3
commit
f5feb8db1e
src/ShardingCore
|
@ -54,7 +54,8 @@ namespace ShardingCore.DynamicDataSources
|
|||
{
|
||||
using (_virtualDataSourceManager.CreateScope(virtualDataSource.ConfigId))
|
||||
{
|
||||
using var context = serviceScope.ServiceProvider.GetService<TShardingDbContext>();
|
||||
using (var context = serviceScope.ServiceProvider.GetService<TShardingDbContext>())
|
||||
{
|
||||
if (_entityConfigOptions.EnsureCreatedWithOutShardingTable || !isOnStart)
|
||||
EnsureCreated(virtualDataSource, context, dataSourceName);
|
||||
else if (_entityConfigOptions.CreateDataBaseOnlyOnStart.GetValueOrDefault())
|
||||
|
@ -98,6 +99,7 @@ namespace ShardingCore.DynamicDataSources
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void CreateDataTable(string dataSourceName, IVirtualTable virtualTable, ISet<string> existTables,
|
||||
bool isOnStart)
|
||||
|
@ -164,21 +166,12 @@ namespace ShardingCore.DynamicDataSources
|
|||
{
|
||||
if (context is IShardingDbContext shardingDbContext)
|
||||
{
|
||||
var dbContext =
|
||||
shardingDbContext.GetDbContext(dataSourceName, false, _routeTailFactory.Create(string.Empty));
|
||||
|
||||
using (var dbContext =
|
||||
shardingDbContext.GetDbContext(dataSourceName, false,
|
||||
_routeTailFactory.Create(string.Empty, false)))
|
||||
{
|
||||
var isDefault = virtualDataSource.IsDefault(dataSourceName);
|
||||
|
||||
var modelCacheSyncObject = dbContext.GetModelCacheSyncObject();
|
||||
|
||||
var acquire = Monitor.TryEnter(modelCacheSyncObject, TimeSpan.FromSeconds(3));
|
||||
if (!acquire)
|
||||
{
|
||||
throw new ShardingCoreException("cant get modelCacheSyncObject lock");
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
if (isDefault)
|
||||
{
|
||||
dbContext.RemoveDbContextRelationModelThatIsShardingTable();
|
||||
|
@ -189,11 +182,6 @@ namespace ShardingCore.DynamicDataSources
|
|||
}
|
||||
|
||||
dbContext.Database.EnsureCreated();
|
||||
dbContext.RemoveModelCache();
|
||||
}
|
||||
finally
|
||||
{
|
||||
Monitor.Exit(modelCacheSyncObject);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -208,7 +196,6 @@ namespace ShardingCore.DynamicDataSources
|
|||
dbContext.RemoveDbContextAllRelationModel();
|
||||
dbContext.Database.EnsureCreated();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -50,7 +50,9 @@ namespace ShardingCore.TableCreator
|
|||
{
|
||||
using (var serviceScope = _serviceProvider.CreateScope())
|
||||
{
|
||||
var dbContext = serviceScope.ServiceProvider.GetService<TShardingDbContext>();
|
||||
using (var dbContext = serviceScope.ServiceProvider.GetService<TShardingDbContext>())
|
||||
{
|
||||
|
||||
var shardingDbContext = (IShardingDbContext)dbContext;
|
||||
using (var context = shardingDbContext.GetDbContext(dataSourceName, false,
|
||||
_routeTailFactory.Create(tail, false)))
|
||||
|
@ -74,4 +76,5 @@ namespace ShardingCore.TableCreator
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue