Advertisement
Guest User

Untitled

a guest
May 20th, 2018
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.56 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Data.SqlClient;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading;
  8. namespace deadlockThreads
  9. {
  10. class Program
  11. {
  12. static void Main(string[] args)
  13. {
  14. Thread t1 = new Thread(new ThreadStart(doTran1));
  15. Thread t2 = new Thread(new ThreadStart(doTran2));
  16. Object thisLock = new object();
  17. t1.Start();
  18. t2.Start();
  19. t1.Join();
  20. t2.Join();
  21. Console.ReadLine();
  22. }
  23.  
  24. private static void doTran1()
  25. {
  26. SqlConnection cs = new SqlConnection("Data Source=DESKTOP-I7QN8TP\\SQLEXPRESS;Initial Catalog = Magazin_Dulciuri; Integrated Security = True");
  27. cs.Open();
  28. int incercari = 0;
  29. while (incercari < 3)
  30. {
  31. SqlTransaction tr1 = cs.BeginTransaction();
  32. SqlCommand cmd = new SqlCommand();
  33. SqlCommand cmd2 = new SqlCommand();
  34. SqlDataReader reader;
  35.  
  36. cmd.CommandText = "UPDATE Produs_Dulce SET denumire='am blocat T1CSharp 'WHERE idD = 100";
  37. cmd.CommandType = CommandType.Text;
  38. cmd.Transaction = tr1;
  39. cmd.Connection = cs;
  40.  
  41. cmd2.CommandText = "UPDATE Ingrediente SET nume='am blocat T1CSharp' WHERE idI = 100";
  42. cmd2.CommandType = CommandType.Text;
  43. cmd2.Transaction = tr1;
  44. cmd2.Connection = cs;
  45.  
  46.  
  47. try
  48. {
  49. cmd.ExecuteNonQuery(); //blocare Produs dulce
  50. Thread.Sleep(3000);
  51. cmd2.ExecuteNonQuery(); //e deja blocat in T2
  52.  
  53. tr1.Commit();
  54. Console.WriteLine("tranzactie t1 reusita dupa " + incercari + " incercari");
  55. incercari = 3;
  56.  
  57.  
  58. }
  59. catch (Exception ex)
  60. {
  61. Console.WriteLine("DEADLOCK");
  62. try
  63. {
  64. Console.WriteLine("rollback t1");
  65. tr1.Rollback();
  66. incercari++;
  67.  
  68. }
  69. catch (Exception ex2)
  70. {
  71. // This catch block will handle any errors that may have occurred
  72. // on the server that would cause the rollback to fail, such as
  73. // a closed connection.
  74. Console.WriteLine("T1Rollback Exception Type: {0}", ex2.GetType());
  75. Console.WriteLine(" Message: {0}", ex2.Message);
  76. }
  77. }
  78. }
  79. cs.Close();
  80. //reader = cmd.ExecuteReader();
  81. //// Data is accessible through the DataReader object here.
  82. //if (reader.HasRows)
  83. //{
  84. // while (reader.Read())
  85. // {
  86. // Console.WriteLine(reader.GetInt32(0).ToString() + reader.GetString(1));
  87. // }
  88. //}
  89. //else
  90. //{
  91. // Console.WriteLine("No rows found.");
  92. //}
  93. //reader.Close();
  94.  
  95.  
  96. }
  97.  
  98. private static void doTran2()
  99. {
  100. SqlConnection cs = new SqlConnection("Data Source=DESKTOP-I7QN8TP\\SQLEXPRESS;Initial Catalog = Magazin_Dulciuri; Integrated Security = True");
  101. cs.Open();
  102. int incercari = 0;
  103. while (incercari < 3)
  104. {
  105. SqlTransaction tr2 = cs.BeginTransaction();
  106. SqlCommand cmd = new SqlCommand();
  107. SqlCommand cmd2 = new SqlCommand();
  108. SqlDataReader reader;
  109.  
  110. cmd.CommandText = "UPDATE Produs_Dulce SET denumire='am blocat T2CSharp 'WHERE idD = 100";
  111. cmd.CommandType = CommandType.Text;
  112. cmd.Transaction = tr2;
  113. cmd.Connection = cs;
  114.  
  115. cmd2.CommandText = "UPDATE Ingrediente SET nume='am blocat T2CSharp' WHERE idI = 100";
  116. cmd2.CommandType = CommandType.Text;
  117. cmd2.Transaction = tr2;
  118. cmd2.Connection = cs;
  119.  
  120.  
  121. try
  122. {
  123. cmd2.ExecuteNonQuery(); //blocare Imgrediente
  124. Thread.Sleep(3000);
  125.  
  126. cmd.ExecuteNonQuery(); //e deja blocat in T1
  127. tr2.Commit();
  128. Console.WriteLine("tranzactie T2 reusita dupa " + incercari + " incercari");
  129. incercari = 3;
  130.  
  131.  
  132. }
  133. catch (Exception ex)
  134. {
  135. Console.WriteLine("DEADLOCK");
  136. try
  137. {
  138. Console.WriteLine("rollback t2");
  139. tr2.Rollback();
  140. incercari++;
  141.  
  142. }
  143. catch (Exception ex2)
  144. {
  145. // This catch block will handle any errors that may have occurred
  146. // on the server that would cause the rollback to fail, such as
  147. // a closed connection.
  148. Console.WriteLine("T2Rollback Exception Type: {0}", ex2.GetType());
  149. Console.WriteLine(" Message: {0}", ex2.Message);
  150. }
  151. }
  152. }
  153. cs.Close();
  154. }
  155.  
  156.  
  157. }
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement