I have a situation here, I need to insert a record in 10 tables and that's need to b atomic in nature hence I am using transaction, this need to be done for a collection of records and need to have better performance hence can't use sequential approach. However the SQL Transactions are causing deadlock in Parallel.ForEach and thus I need a different solution. Could anyone please advise how I can achieve this parallel processing with encountering any deadlocks??
int[] itemListArr = GetAllItems(); Parallel.ForEach(Partitioner.Create(0, itemListArr.Count(), 5), new ParallelOptions { MaxDegreeOfParallelism = 8 }, (range) => { for (int i = rangeFrom; i < rangeTo; i++) { _database.BeginTransaction(connectionString); try { foreach (var table in _template.Tables) { sqlCmd = string.Format(insertQuery, table.DestinationTable, strColumns, strColumnsValue); _database.InsertWithMultiTransaction(sqlCmd); } _database.CommitTransaction(); } catch (Exception ex) { _database.RollbackTransaction(); } } });
sqlCmd = string.Format(insertQuery, table.DestinationTable, strColumns, strColumnsValue);
_database
parallel.foreach
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)