1818using ShardingCore . Exceptions ;
1919using ShardingCore . Extensions ;
2020using ShardingCore . Infrastructures ;
21-
2221using ShardingCore . Sharding . Abstractions ;
2322
2423namespace ShardingCore . Sharding . ShardingDbContextExecutors
@@ -32,11 +31,10 @@ namespace ShardingCore.Sharding.ShardingDbContextExecutors
3231 */
3332 public class DataSourceDbContext : IDataSourceDbContext
3433 {
35-
3634 private static readonly IComparer < string > _comparer = new NoShardingFirstComparer ( ) ;
3735
38- private readonly ILogger < DataSourceDbContext > _logger ;
3936 public Type DbContextType { get ; }
37+
4038 /// <summary>
4139 /// 当前是否是默认的dbcontext 也就是不分片的dbcontext
4240 /// </summary>
@@ -82,6 +80,7 @@ public class DataSourceDbContext : IDataSourceDbContext
8280 /// shell dbcontext最外面的壳
8381 /// </summary>
8482 private readonly DbContext _shardingShellDbContext ;
83+
8584 private readonly IShardingRuntimeContext _shardingRuntimeContext ;
8685
8786 /// <summary>
@@ -117,20 +116,18 @@ public DataSourceDbContext(string dataSourceName,
117116 bool isDefault ,
118117 DbContext shardingShellDbContext ,
119118 IDbContextCreator dbContextCreator ,
120- ActualConnectionStringManager actualConnectionStringManager ,
121- ILogger < DataSourceDbContext > logger )
119+ ActualConnectionStringManager actualConnectionStringManager )
122120 {
123121 var shardingDbContext = ( IShardingDbContext ) shardingShellDbContext ;
124122 DataSourceName = dataSourceName ;
125123 IsDefault = isDefault ;
126124 _shardingShellDbContext = shardingShellDbContext ;
127125 _shardingRuntimeContext = shardingShellDbContext . GetShardingRuntimeContext ( ) ;
128126 DbContextType = shardingShellDbContext . GetType ( ) ;
129- _virtualDataSource = shardingDbContext
127+ _virtualDataSource = shardingDbContext
130128 . GetVirtualDataSource ( ) ;
131129 _dbContextCreator = dbContextCreator ;
132130 _actualConnectionStringManager = actualConnectionStringManager ;
133- this . _logger = logger ;
134131 }
135132
136133 /// <summary>
@@ -155,8 +152,9 @@ private DbContextOptions CreateShareDbContextOptionsBuilder()
155152 {
156153 //先创建dbcontext option builder
157154 var dbContextOptionBuilderCreator = _shardingRuntimeContext . GetDbContextOptionBuilderCreator ( ) ;
158- var dbContextOptionsBuilder = dbContextOptionBuilderCreator . CreateDbContextOptionBuilder ( ) . UseShardingOptions ( _shardingRuntimeContext ) ;
159-
155+ var dbContextOptionsBuilder = dbContextOptionBuilderCreator . CreateDbContextOptionBuilder ( )
156+ . UseShardingOptions ( _shardingRuntimeContext ) ;
157+
160158 if ( IsDefault )
161159 {
162160 //如果是默认的需要使用shell的dbconnection为了保证可以使用事务
@@ -342,70 +340,88 @@ public void Rollback()
342340 {
343341 if ( IsDefault )
344342 return ;
345- try
346- {
347- CurrentDbContextTransaction ? . Rollback ( ) ;
348- }
349- catch ( Exception e )
350- {
351- _logger . LogError ( e , "rollback error." ) ;
352- }
343+ CurrentDbContextTransaction ? . Rollback ( ) ;
353344 }
354345
355346 /// <summary>
356347 /// 提交数据
357348 /// </summary>
358- /// <param name="dataSourceCount">如果只有一个数据源那么就直接报错否则就忽略</param>
359- public void Commit ( int dataSourceCount )
349+ public void Commit ( )
360350 {
361351 if ( IsDefault )
362352 return ;
363- try
364- {
365- CurrentDbContextTransaction ? . Commit ( ) ;
366- }
367- catch ( Exception e )
368- {
369- _logger . LogError ( e , "commit error." ) ;
370- if ( dataSourceCount == 1 )
371- throw ;
372- }
353+ CurrentDbContextTransaction ? . Commit ( ) ;
373354 }
374355#if ! NETCOREAPP2_0
375356 public async Task RollbackAsync ( CancellationToken cancellationToken = new CancellationToken ( ) )
376357 {
377358 cancellationToken . ThrowIfCancellationRequested ( ) ;
378359 if ( IsDefault )
379360 return ;
380- try
381- {
382- if ( CurrentDbContextTransaction != null )
383- await CurrentDbContextTransaction . RollbackAsync ( cancellationToken ) ;
384- }
385- catch ( Exception e )
386- {
387- _logger . LogError ( e , "rollback error." ) ;
388- }
361+ if ( CurrentDbContextTransaction != null )
362+ await CurrentDbContextTransaction . RollbackAsync ( cancellationToken ) ;
389363 }
390364
391- public async Task CommitAsync ( int dataSourceCount , CancellationToken cancellationToken =
392- new CancellationToken ( ) )
365+ public async Task CommitAsync ( CancellationToken cancellationToken =
366+ new CancellationToken ( ) )
393367 {
394368 cancellationToken . ThrowIfCancellationRequested ( ) ;
395369 if ( IsDefault )
396370 return ;
397- try
398- {
399- if ( CurrentDbContextTransaction != null )
400- await CurrentDbContextTransaction . CommitAsync ( cancellationToken ) ;
401- }
402- catch ( Exception e )
403- {
404- _logger . LogError ( e , "commit error." ) ;
405- if ( dataSourceCount == 1 )
406- throw ;
407- }
371+ if ( CurrentDbContextTransaction != null )
372+ await CurrentDbContextTransaction . CommitAsync ( cancellationToken ) ;
408373 }
374+ #if ! NETCOREAPP3_0 && ! NETSTANDARD2_0
375+ // public void CreateSavepoint(string name)
376+ // {
377+ // if (IsDefault)
378+ // return;
379+ // CurrentDbContextTransaction?.CreateSavepoint(name);
380+ // }
381+ //
382+ // public async Task CreateSavepointAsync(string name,
383+ // CancellationToken cancellationToken = new CancellationToken())
384+ // {
385+ // cancellationToken.ThrowIfCancellationRequested();
386+ // if (IsDefault)
387+ // return;
388+ // if (CurrentDbContextTransaction != null)
389+ // await CurrentDbContextTransaction.CreateSavepointAsync(name, cancellationToken);
390+ // }
391+ //
392+ // public void RollbackToSavepoint(string name)
393+ // {
394+ // if (IsDefault)
395+ // return;
396+ // CurrentDbContextTransaction?.RollbackToSavepoint(name);
397+ // }
398+ //
399+ // public async Task RollbackToSavepointAsync(string name,
400+ // CancellationToken cancellationToken = default(CancellationToken))
401+ // {
402+ // cancellationToken.ThrowIfCancellationRequested();
403+ // if (IsDefault)
404+ // return;
405+ // if (CurrentDbContextTransaction != null)
406+ // await CurrentDbContextTransaction.RollbackToSavepointAsync(name, cancellationToken);
407+ // }
408+ //
409+ // public void ReleaseSavepoint(string name)
410+ // {
411+ // if (IsDefault)
412+ // return;
413+ // CurrentDbContextTransaction?.ReleaseSavepoint(name);
414+ // }
415+ //
416+ // public async Task ReleaseSavepointAsync(string name, CancellationToken cancellationToken = default(CancellationToken))
417+ // {
418+ // cancellationToken.ThrowIfCancellationRequested();
419+ // if (IsDefault)
420+ // return;
421+ // if (CurrentDbContextTransaction != null)
422+ // await CurrentDbContextTransaction.ReleaseSavepointAsync(name, cancellationToken);
423+ // }
424+ #endif
409425#endif
410426
411427 public void Dispose ( )
0 commit comments