Advertisement
Guest User

Untitled

a guest
May 21st, 2019
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.37 KB | None | 0 0
  1.  
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Configuration;
  5. using System.Data;
  6. using System.Data.SqlClient;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading;
  10.  
  11. namespace lab3_sgbd
  12. {
  13. class Program
  14. {
  15. static SqlConnection connection;
  16.  
  17. static public void ShowSample()
  18. {
  19. connection = new SqlConnection(ConfigurationManager.ConnectionStrings["db"].ConnectionString);
  20. connection.Open();
  21.  
  22. SqlCommand selectCommand1 = new SqlCommand("select * from Managers where Mid = 1", connection);
  23. SqlCommand selectCommand2 = new SqlCommand("select * from Employees where Eid = 4", connection);
  24. SqlDataReader selection1 = selectCommand1.ExecuteReader();
  25. selection1.Read();
  26. Console.WriteLine(selection1.GetValue(1));
  27. selection1.Close();
  28.  
  29. SqlDataReader selection2 = selectCommand2.ExecuteReader();
  30. selection2.Read();
  31. Console.WriteLine(selection2.GetValue(1));
  32. selection2.Close();
  33. }
  34.  
  35. static public void RunThread1()
  36. {
  37. connection = new SqlConnection(ConfigurationManager.ConnectionStrings["db"].ConnectionString);
  38. connection.Open();
  39.  
  40. for (int i = 0; i < 3; i++)
  41. {
  42. try
  43. {
  44. SqlCommand command = new SqlCommand("tran1", connection);
  45.  
  46. command.CommandType = CommandType.StoredProcedure;
  47. command.ExecuteNonQuery();
  48. ShowSample();
  49. Console.WriteLine("Thread1 executed transaction successfully after " + i + " tries!!\n");
  50. return;
  51. }
  52. catch (SqlException e)
  53. {
  54. Console.WriteLine("Thread1 is ded but still trying -----> deadlock!!\n ");
  55. }
  56.  
  57. }
  58. Console.WriteLine("Thread1 is ded forever :(!!\n");
  59.  
  60.  
  61. }
  62.  
  63. static public void RunThread2()
  64. {
  65. SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["db"].ConnectionString);
  66. connection.Open();
  67. for (int i = 0; i < 3; i++)
  68. {
  69. try
  70. {
  71. SqlCommand command = new SqlCommand("tran2", connection);
  72.  
  73. command.CommandType = CommandType.StoredProcedure;
  74. command.ExecuteNonQuery();
  75. ShowSample();
  76. Console.WriteLine("Thread2 executed transaction successfully after " + i + " tries!!\n");
  77.  
  78. return;
  79. }
  80. catch (SqlException e)
  81. {
  82. Console.WriteLine("Thread2 is ded but still trying -----> deadlock!!\n ");
  83. }
  84.  
  85. }
  86. Console.WriteLine("Thread2 is ded forever :(!!\n");
  87.  
  88. }
  89.  
  90.  
  91. static public void Main(string[] args)
  92. {
  93.  
  94. Thread thr1 = new Thread(new ThreadStart(RunThread1));
  95. Thread thr2 = new Thread(new ThreadStart(RunThread2));
  96.  
  97. thr1.Start();
  98. thr2.Start();
  99.  
  100. Console.ReadLine();
  101.  
  102. }
  103. }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement