Hi firstly thank you for your attention on this question; Is there any way to implement a transaction like this in c#
using (Transactionscope x=new Transactionscope ())
{
Thead A()=> Independent Transactionscope() A(Insert into table X )
Thead B()=> Independent Transactionscope() B(Insert into table Y )
Thead C()=> Independent Transactionscope() C(Insert into table Z )
Thread.WaitAll(A,B,C)
commit big transaction x/ rollback big transaction x
}
Note that Distributed Transactions are currently not supported on .Net Core, only on .Net Framework.
In order to use a TransactionScope to span multiple threads, you'll need to use DependentClone to tie the threads into the parent TransactionScope.
The steps are:
TransactionScope on your main / first threadDependentClone to create a DependentTransaction, and then pass this DependentTransaction instance to the new thread.TransactionScope(DependentTransaction) constructor overload to create a linked TransactionScope, in which the child thread can perform local transactions.TransactionScope and the DependentTransactionTransactionScopeThere's some caveats too:
DependentTransaction on multiple threads will immediately require the use of MSDTC.SqlBulkCopy for that), and you'll want to measure whether parallel inserts into different tables, same database under a DTC transaction warrants the locking overhead or returns any performance benefit.async, then you'll need TransactionScopeAsyncFlowOption.EnabledMore about Transction Scope here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With