Advertisement
Guest User

Untitled

a guest
May 24th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.74 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Data.SqlClient;
  6. using System.Drawing;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading;
  10. using System.Windows.Forms;
  11.  
  12. namespace LAB3
  13. {
  14. public partial class Form1 : Form
  15. {
  16. public Form1()
  17. {
  18. InitializeComponent();
  19. }
  20.  
  21. SqlConnection connection = null;
  22.  
  23. string connectionString = "Server=DESKTOP-OJG59VC;Database=americantvseries;Integrated Security=true;";
  24.  
  25.  
  26. SqlCommand cmd_tran1;
  27. SqlCommand cmd_tran2;
  28.  
  29. private void Form1_Load(object sender, EventArgs e)
  30. {
  31. try
  32. {
  33. connection = new SqlConnection(connectionString);
  34. connection.Open();
  35. }
  36. catch (Exception err)
  37. {
  38. MessageBox.Show("The error message is: " + err.Message.ToString());
  39. }
  40.  
  41. }
  42.  
  43. private void tran1btn_Click(object sender, EventArgs e)
  44. {
  45. Thread t = new Thread(new ThreadStart(execute_trans1));
  46. t.Start();
  47. }
  48.  
  49. private void tran2btn_Click(object sender, EventArgs e)
  50. {
  51. Thread t = new Thread(new ThreadStart(execute_trans2));
  52. t.Start();
  53. }
  54.  
  55. private void execute_trans1()
  56. {
  57. cmd_tran1 = new SqlCommand("UPDATE Actors SET Nationality = 'nationalitate' WHERE ID = 1; WAITFOR DELAY '00:00:03'; UPDATE Series SET Name = 'serial' WHERE ID = 4;", connection);
  58.  
  59. SqlTransaction transaction = connection.BeginTransaction("tr1");
  60.  
  61. cmd_tran1.Transaction = transaction;
  62. cmd_tran1.CommandTimeout = 3000;
  63.  
  64. int i_try = 3;
  65. while (i_try != 0)
  66. {
  67. try
  68. {
  69. cmd_tran1.ExecuteNonQuery();
  70. Console.WriteLine(i_try.ToString());
  71. transaction.Commit();
  72. i_try = 0;
  73. MessageBox.Show("executed trans1");
  74. }
  75. catch (SqlException err)
  76. {
  77. Console.WriteLine(err.ToString());
  78. if (err.Number == 1205)
  79. {
  80. i_try--;
  81. MessageBox.Show("DEADLOCK IN TRANSACTION 1");
  82. }
  83. }
  84.  
  85. }
  86.  
  87. }
  88.  
  89. private void execute_trans2()
  90. {
  91. cmd_tran2 = new SqlCommand(" UPDATE Series SET Name = 'serial2' WHERE ID = 4; WHERE ID = 1; WAITFOR DELAY '00:00:03'; UPDATE Actors SET Nationality = 'nationalitate2'", connection);
  92.  
  93. SqlTransaction transaction2 = connection.BeginTransaction("tr2");
  94.  
  95.  
  96. cmd_tran2.Transaction = transaction2;
  97. cmd_tran2.CommandTimeout = 3000;
  98.  
  99.  
  100. int i_try = 3;
  101. while (i_try != 0)
  102. {
  103. try
  104. {
  105. cmd_tran2.ExecuteNonQuery();
  106. Console.WriteLine(i_try.ToString());
  107.  
  108. transaction2.Commit();
  109.  
  110. i_try = 0;
  111. MessageBox.Show("executed trans2");
  112.  
  113. }
  114. catch (SqlException err)
  115. {
  116. Console.WriteLine(err.ToString());
  117.  
  118. MessageBox.Show("errroor" + err.ToString());
  119. if (err.Number == 1205)
  120. {
  121. i_try--;
  122. MessageBox.Show("DEADLOCK IN TRANSACTION 2");
  123. }
  124. }
  125. }
  126. }
  127. }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement