Hello, I've written below sample in which I'm creating a deadlock:
maximum timeout for transaction to be end (abort) is set up in app.config file for 5 seconds
Besides that, even after 10 minutes of executing, program is still hang up because of deadlock. Console out is:
T2 attempt to lock RB
T2 is modifying RB
T1 attempt to lock RA
T1 is modifying RA
T2 attempt to lock RA
T1 attempt to lock RB
Why the transactions do not abort theirs execution and how to make them to do that ?
Code:
using System;
using System.Threading;
using System.Transactions;
namespace SampleDeadlock
{
class Program
{
private static object lockerA = new object();
private static object lockerB = new object();
static void Main(string[] args)
{
Thread transaction1 = new Thread(new ThreadStart(Transaction1));
Thread transaction2 = new Thread(new ThreadStart(Transaction2));
transaction1.Start();
transaction2.Start();
transaction1.Join();
transaction2.Join();
Console.WriteLine("done");
Console.ReadKey();
}
private static void Transaction1()
{
try
{
using (TransactionScope scope = new TransactionScope(TransactionScopeOption.RequiresNew))
{
ModifyResourceA("T1");
scope.Complete();
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
private static void Transaction2()
{
try
{
using (TransactionScope scope = new TransactionScope(TransactionScopeOption.RequiresNew))
{
ModifyResourceB("T2");
scope.Complete();
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
private static void ModifyResourceA(string transactionId)
{
Console.WriteLine(transactionId + " attempt to lock RA");
lock(lockerA)
{
Console.WriteLine(transactionId + " is modifying RA");
Thread.Sleep(1000);
ModifyResourceB(transactionId);
}
}
private static void ModifyResourceB(string transactionId)
{
Console.WriteLine(transactionId + " attempt to lock RB");
lock (lockerB)
{
Console.WriteLine(transactionId + " is modifying RB");
Thread.Sleep(500);
ModifyResourceA(transactionId);
}
}
}
}
maximum timeout for transaction to be end (abort) is set up in app.config file for 5 seconds
Code:
<configuration>
<system.transactions>
<defaultSettings timeout="00:00:05" />
</system.transactions>
</configuration>
Besides that, even after 10 minutes of executing, program is still hang up because of deadlock. Console out is:
T2 attempt to lock RB
T2 is modifying RB
T1 attempt to lock RA
T1 is modifying RA
T2 attempt to lock RA
T1 attempt to lock RB
Why the transactions do not abort theirs execution and how to make them to do that ?