vycma Posted February 7, 2010 Posted February 7, 2010 Hello, I've written below sample in which I'm creating a deadlock: 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 <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 ? Quote
Leaders snarfblam Posted February 7, 2010 Leaders Posted February 7, 2010 Well, what exactly is supposed to happen when the transactions timeout? The deadlock is the result of (infinite) recursion of your own locks. Both threads have frozen and are not running code, and there is no way for a TransactionAbortedException to be thrown. Are you trying to simulate an actual problem you ran into? Quote [sIGPIC]e[/sIGPIC]
vycma Posted February 7, 2010 Author Posted February 7, 2010 It's not a real example i wrote that because i had no better things to do that time. I understand transaction timeout as a time, after which the transaction is aborted if it has problems with commited properly. The deadlock blocks that transaction because it's nature of deadlock. I want to kill such a infinite transaction, i thought that the timeout is enough for doing that (just like sql server do when discover a deadlock "Transaction (id) was deadlocked on resources with another process and has been chosen as the deadlock victim"). What for is the transaction timeout ? I have to kill transaction by myself ? Quote
vycma Posted February 8, 2010 Author Posted February 8, 2010 Ok I know now, it was my bad understanding of that, thanks Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.