Skip to content

Commit

Permalink
SqlServer按时间分表批量插入异常修复
Browse files Browse the repository at this point in the history
  • Loading branch information
Coldairarrow committed Jun 24, 2020
1 parent afd5d53 commit afcf89d
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 10 deletions.
1 change: 1 addition & 0 deletions examples/Demo.DI/Demo.DI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

<ItemGroup>
<ProjectReference Include="..\..\src\EFCore.Sharding.SQLite\EFCore.Sharding.SQLite.csproj" />
<ProjectReference Include="..\..\src\EFCore.Sharding.SqlServer\EFCore.Sharding.SqlServer.csproj" />
<ProjectReference Include="..\..\src\EFCore.Sharding.Tests\EFCore.Sharding.Tests.csproj" />
<ProjectReference Include="..\..\src\EFCore.Sharding\EFCore.Sharding.csproj" />
</ItemGroup>
Expand Down
8 changes: 4 additions & 4 deletions examples/Demo.DI/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@ static void Main(string[] args)
services.UseEFCoreSharding(config =>
{
//单表
config.UseDatabase(Config.SQLITE1, DatabaseType.SQLite);
config.UseDatabase(Config.CONSTRING1, DatabaseType.SqlServer);
//使用多个数据库
config.UseDatabase<IMyDbAccessor>(Config.SQLITE1, DatabaseType.SQLite);
config.UseDatabase<IMyDbAccessor>(Config.CONSTRING1, DatabaseType.SqlServer);

DateTime startTime = DateTime.Now.AddMinutes(-5);
DateTime endTime = DateTime.MaxValue;
//分表
config.AddAbsDb(DatabaseType.SQLite)//添加抽象数据库
config.AddAbsDb(DatabaseType.SqlServer)//添加抽象数据库
.AddPhysicDbGroup()//添加物理数据库组
.AddPhysicDb(ReadWriteType.Read | ReadWriteType.Write, Config.SQLITE1)//添加物理数据库1
.AddPhysicDb(ReadWriteType.Read | ReadWriteType.Write, Config.CONSTRING1)//添加物理数据库1
.SetDateShardingRule<Base_UnitTest>(nameof(Base_UnitTest.CreateTime))//设置分表规则
.AutoExpandByDate<Base_UnitTest>(//设置为按时间自动分表
ExpandByDateMode.PerMinute,
Expand Down
2 changes: 1 addition & 1 deletion src/EFCore.Sharding.MySql/EFCore.Sharding.MySql.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<PackageProjectUrl>https://github.com/Coldairarrow/EFCore.Sharding</PackageProjectUrl>
<RepositoryUrl>https://github.com/Coldairarrow/EFCore.Sharding</RepositoryUrl>
<RepositoryType>github</RepositoryType>
<Version>3.1.4.23</Version>
<Version>3.1.4.24</Version>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="3.1.1" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<PackageProjectUrl>https://github.com/Coldairarrow/EFCore.Sharding</PackageProjectUrl>
<RepositoryUrl>https://github.com/Coldairarrow/EFCore.Sharding</RepositoryUrl>
<RepositoryType>github</RepositoryType>
<Version>3.1.4.23</Version>
<Version>3.1.4.24</Version>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
Expand Down
2 changes: 1 addition & 1 deletion src/EFCore.Sharding.SQLite/EFCore.Sharding.SQLite.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<PackageProjectUrl>https://github.com/Coldairarrow/EFCore.Sharding</PackageProjectUrl>
<RepositoryUrl>https://github.com/Coldairarrow/EFCore.Sharding</RepositoryUrl>
<RepositoryType>github</RepositoryType>
<Version>3.1.4.23</Version>
<Version>3.1.4.24</Version>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="3.1.5" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<PackageProjectUrl>https://github.com/Coldairarrow/EFCore.Sharding</PackageProjectUrl>
<RepositoryUrl>https://github.com/Coldairarrow/EFCore.Sharding</RepositoryUrl>
<RepositoryType>github</RepositoryType>
<Version>3.1.4.23</Version>
<Version>3.1.4.24</Version>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.5" />
Expand Down
2 changes: 1 addition & 1 deletion src/EFCore.Sharding/EFCore.Sharding.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<PackageProjectUrl>https://github.com/Coldairarrow/EFCore.Sharding</PackageProjectUrl>
<RepositoryUrl>https://github.com/Coldairarrow/EFCore.Sharding</RepositoryUrl>
<RepositoryType>github</RepositoryType>
<Version>3.1.4.23</Version>
<Version>3.1.4.24</Version>
<GenerateAssemblyInfo>true</GenerateAssemblyInfo>
</PropertyGroup>

Expand Down
19 changes: 18 additions & 1 deletion src/EFCore.Sharding/Sharding/ShardingDbAccessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,24 @@ private async Task<int> WriteTableAsync<T>(List<T> entities, Func<object, IDbAcc

return await PackAccessDataAsync(async () =>
{
var tasks = mapConfigs.Select(aConfig => accessDataAsync(aConfig.targetObj, aConfig.targetDb));
//同一个IDbAccessor对象只能在一个线程中
List<Task<int>> tasks = new List<Task<int>>();
var dbs = mapConfigs.Select(x => x.targetDb).Distinct().ToList();
dbs.ForEach(aDb =>
{
tasks.Add(Task.Run(async () =>
{
int count = 0;
var objs = mapConfigs.Where(x => x.targetDb == aDb).ToList();
foreach (var aObj in objs)
{
count += await accessDataAsync(aObj.targetObj, aObj.targetDb);
}

return count;
}));
});

return (await Task.WhenAll(tasks.ToArray())).Sum();
});
}
Expand Down

0 comments on commit afcf89d

Please sign in to comment.