zzzprojects / Dapper-Plus

Dapper Plus - High-Efficient Bulk Actions (Insert, Update, Delete, and Merge) for .NET
https://dapper-plus.net/
383 stars 85 forks source link

Dapper Plus in Multithread application facing following Exception using bulk methods #86

Closed zvig-g closed 3 years ago

zvig-g commented 3 years ago

2021-03-02 12:26:08,182 [5] [(null)] ERROR DbManager: BulkUpdateTempMovements failed. Id:44979369-e738-4692-b256-d1bda5468067 CommandString:select fd.MovementTypeID, fd.ResultId, fd.IFPlusSeparateAvoidGrossMovement from dbo.FA_FeeDecision fd (nolock) where fd.IFPlusSeparateAvoidGrossMovement = 1 ExecuteType:Reader DurationMilliseconds:3.4 Errored:False
System.InvalidOperationException: Collection was modified; enumeration operation may not execute. at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource) at System.Collections.Generic.List1.ForEach(Action1 action) at Z.Dapper.Plus.DapperPlusEntityMapper1.?(List1 ?) at Z.Dapper.Plus.DapperPlusAction.Remap(BulkOperation bulkOperation, DapperPlusEntityMapper config) at Z.Dapper.Plus.DapperPlusAction.Map(BulkOperation bulkOperation, DapperPlusEntityMapper config) at Z.Dapper.Plus.DapperPlusAction.ApplyConfig(BulkOperation bulkOperation, DapperPlusEntityMapper config, DapperPlusActionKind actionKind) at Z.Dapper.Plus.DapperPlusAction.Execute() at Z.Dapper.Plus.DapperPlusActionSet1.AddAction(String mapperKey, DapperPlusActionKind actionKind, TEntity item) at Z.Dapper.Plus.DapperPlusActionSet1.DapperPlusActionSetBuilder(DapperPlusContext context, IDbConnection connection, IDbTransaction transaction, String mapperKey, DapperPlusActionKind actionKind, TEntity item, Func2[] selectors) at Z.Dapper.Plus.DapperPlusExtensions.BulkUpdate[T](IDbConnection connection, String mapperKey, T item, Func2[] selectors) at Z.Dapper.Plus.DapperPlusExtensions.BulkUpdate[T](IDbConnection connection, T item, Func2[] selectors) at InterchangeBilling.Dao.DbManager.BulkUpdateTempMovements(List1 list) in C:\tfs\Pricing\InterchangeBilling\Features\DEFECT-81824\Sources\InterchangeBilling.Dao\DbManager.cs:line 949 2021-03-02 12:26:08,179 [12] [(null)] INFO DbManager: _loadFallbackData ended. select * from dbo.FA_InterchangeFallbackFeesRates

2021-03-02 12:15:08,119 [12] [(null)] INFO DbManager: _loadFallbackData ended. select from dbo.FA_InterchangeFallbackFeesRates 2021-03-02 12:15:08,149 [8] [(null)] ERROR System.NullReferenceException: Object reference not set to an instance of an object. at Dapper.SqlMapper.d1381.MoveNext() in C:\projects\dapper\Dapper\SqlMapper.cs:line 1103 at System.Collections.Generic.List1..ctor(IEnumerable1 collection) at System.Linq.Enumerable.ToList[TSource](IEnumerable1 source) at Dapper.SqlMapper.Query[T](IDbConnection cnn, String sql, Object param, IDbTransaction transaction, Boolean buffered, Nullable1 commandTimeout, Nullable1 commandType) in C:\projects\dapper\Dapper\SqlMapper.cs:line 723 at InterchangeBilling.Dao.DbManager.GetIcAmountTransactionCurrency(Nullable1 trxMainId, Nullable1 trxDate) in C:\tfs\Pricing\InterchangeBilling\Features\DEFECT-81824\Sources\InterchangeBilling.Dao\DbManager.cs:line 1436 at InterchangeBilling.Engine.InterchangeCalculator.MovementCalculationRequiredHandler.HandleMovement(MovementTemp tempMovement, CalculationContext calculationContext) in C:\tfs\Pricing\InterchangeBilling\Features\DEFECT-81824\Sources\InterchangeBilling.Engine\InterchangeBilling.Engine.InterchangeCalculator\MovementCalculationRequiredHandler.cs:line 102 at InterchangeBilling.Engine.InterchangeCalculator.Calculator.<>cDisplayClass12_1.b__0(MovementTemp temporaryMovement) in C:\tfs\Pricing\InterchangeBilling\Features\DEFECT-81824\Sources\InterchangeBilling.Engine\InterchangeBilling.Engine.InterchangeCalculator\Calculator.cs:line 124 2021-03-02 12:15:08,162 [11] [(null)] INFO DbManager: _loadFallbackData ended. select from dbo.FA_InterchangeFallbackFeesRates 2021-03-02 12:15:08,162 [10] [(null)] INFO DbManager: _loadFallbackData ended. select from dbo.FA_InterchangeFallbackFeesRates 2021-03-02 12:15:08,165 [8] [(null)] INFO Failed movement - trxID:2110000000002997589, trxDate: 01/28/2021 16:55:55, ClientID:11843, BankID:383, ex.msg: Object reference not set to an instance of an object., ex.st: at Dapper.SqlMapper.d1381.MoveNext() in C:\projects\dapper\Dapper\SqlMapper.cs:line 1103 at System.Collections.Generic.List1..ctor(IEnumerable1 collection) at System.Linq.Enumerable.ToList[TSource](IEnumerable1 source) at Dapper.SqlMapper.Query[T](IDbConnection cnn, String sql, Object param, IDbTransaction transaction, Boolean buffered, Nullable1 commandTimeout, Nullable1 commandType) in C:\projects\dapper\Dapper\SqlMapper.cs:line 723 at InterchangeBilling.Dao.DbManager.GetIcAmountTransactionCurrency(Nullable1 trxMainId, Nullable1 trxDate) in C:\tfs\Pricing\InterchangeBilling\Features\DEFECT-81824\Sources\InterchangeBilling.Dao\DbManager.cs:line 1436 at InterchangeBilling.Engine.InterchangeCalculator.MovementCalculationRequiredHandler.HandleMovement(MovementTemp tempMovement, CalculationContext calculationContext) in C:\tfs\Pricing\InterchangeBilling\Features\DEFECT-81824\Sources\InterchangeBilling.Engine\InterchangeBilling.Engine.InterchangeCalculator\MovementCalculationRequiredHandler.cs:line 102 at InterchangeBilling.Engine.InterchangeCalculator.Calculator.<>cDisplayClass12_1.b__0(MovementTemp temporaryMovement) in C:\tfs\Pricing\InterchangeBilling\Features\DEFECT-81824\Sources\InterchangeBilling.Engine\InterchangeBilling.Engine.InterchangeCalculator\Calculator.cs:line 124 2021-03-02 12:15:08,189 [5] [(null)] INFO DbManager: _loadFallbackData ended. select from dbo.FA_InterchangeFallbackFeesRates

JonathanMagnan commented 3 years ago

Hello @zvig-g ,

Looking at the stack trace, I believe you are using a Global Context Mapping in the same way that you should use a Instance Context Mapping instead.

When using a Global Context Mapping, you should map only once at the beginning of the application, otherwise, you can end up with some weird concurrency error like this one.

When using a Instance Context Mapping, you can do your mapping over and over such as inside a method since this is an instance.

You can learn more about the different way of mapping here: https://dapper-plus.net/getting-started-mapping

Let me know if that was your error and if you find out the solution.

Best Regards,

Jon

JonathanMagnan commented 3 years ago

Hello @zvig-g ,

Since our last conversation, I haven't heard from you!

As mentioned in my previous message. I believe you are using a Global Context Mapping in the same way that you should use a Instance Context Mapping instead.

Don't hesitate to contact us if this was your issue and if you found the solution.

Best regards,

Jon

JonathanMagnan commented 3 years ago

Hello @zvig-g ,

A simple reminder that we are here to assist you!

Feel free to contact us with further assistance or questions,

Jon