Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Data.SqlClient;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Windows.Forms;
- namespace LAB3
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- SqlConnection connection = null;
- string connectionString = "Server=DESKTOP-OJG59VC;Database=americantvseries;Integrated Security=true;";
- SqlCommand cmd_tran1;
- SqlCommand cmd_tran2;
- private void Form1_Load(object sender, EventArgs e)
- {
- try
- {
- connection = new SqlConnection(connectionString);
- connection.Open();
- }
- catch (Exception err)
- {
- MessageBox.Show("The error message is: " + err.Message.ToString());
- }
- }
- private void tran1btn_Click(object sender, EventArgs e)
- {
- Thread t = new Thread(new ThreadStart(execute_trans1));
- t.Start();
- }
- private void tran2btn_Click(object sender, EventArgs e)
- {
- Thread t = new Thread(new ThreadStart(execute_trans2));
- t.Start();
- }
- private void execute_trans1()
- {
- 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);
- SqlTransaction transaction = connection.BeginTransaction("tr1");
- cmd_tran1.Transaction = transaction;
- cmd_tran1.CommandTimeout = 3000;
- int i_try = 3;
- while (i_try != 0)
- {
- try
- {
- cmd_tran1.ExecuteNonQuery();
- Console.WriteLine(i_try.ToString());
- transaction.Commit();
- i_try = 0;
- MessageBox.Show("executed trans1");
- }
- catch (SqlException err)
- {
- Console.WriteLine(err.ToString());
- if (err.Number == 1205)
- {
- i_try--;
- MessageBox.Show("DEADLOCK IN TRANSACTION 1");
- }
- }
- }
- }
- private void execute_trans2()
- {
- 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);
- SqlTransaction transaction2 = connection.BeginTransaction("tr2");
- cmd_tran2.Transaction = transaction2;
- cmd_tran2.CommandTimeout = 3000;
- int i_try = 3;
- while (i_try != 0)
- {
- try
- {
- cmd_tran2.ExecuteNonQuery();
- Console.WriteLine(i_try.ToString());
- transaction2.Commit();
- i_try = 0;
- MessageBox.Show("executed trans2");
- }
- catch (SqlException err)
- {
- Console.WriteLine(err.ToString());
- MessageBox.Show("errroor" + err.ToString());
- if (err.Number == 1205)
- {
- i_try--;
- MessageBox.Show("DEADLOCK IN TRANSACTION 2");
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement