Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Data.SqlClient;
- using System.Linq;
- using System.Text;
- using System.Threading;
- namespace deadlockThreads
- {
- class Program
- {
- static void Main(string[] args)
- {
- Thread t1 = new Thread(new ThreadStart(doTran1));
- Thread t2 = new Thread(new ThreadStart(doTran2));
- Object thisLock = new object();
- t1.Start();
- t2.Start();
- t1.Join();
- t2.Join();
- Console.ReadLine();
- }
- private static void doTran1()
- {
- SqlConnection cs = new SqlConnection("Data Source=DESKTOP-I7QN8TP\\SQLEXPRESS;Initial Catalog = Magazin_Dulciuri; Integrated Security = True");
- cs.Open();
- int incercari = 0;
- while (incercari < 3)
- {
- SqlTransaction tr1 = cs.BeginTransaction();
- SqlCommand cmd = new SqlCommand();
- SqlCommand cmd2 = new SqlCommand();
- SqlDataReader reader;
- cmd.CommandText = "UPDATE Produs_Dulce SET denumire='am blocat T1CSharp 'WHERE idD = 100";
- cmd.CommandType = CommandType.Text;
- cmd.Transaction = tr1;
- cmd.Connection = cs;
- cmd2.CommandText = "UPDATE Ingrediente SET nume='am blocat T1CSharp' WHERE idI = 100";
- cmd2.CommandType = CommandType.Text;
- cmd2.Transaction = tr1;
- cmd2.Connection = cs;
- try
- {
- cmd.ExecuteNonQuery(); //blocare Produs dulce
- Thread.Sleep(3000);
- cmd2.ExecuteNonQuery(); //e deja blocat in T2
- tr1.Commit();
- Console.WriteLine("tranzactie t1 reusita dupa " + incercari + " incercari");
- incercari = 3;
- }
- catch (Exception ex)
- {
- Console.WriteLine("DEADLOCK");
- try
- {
- Console.WriteLine("rollback t1");
- tr1.Rollback();
- incercari++;
- }
- catch (Exception ex2)
- {
- // This catch block will handle any errors that may have occurred
- // on the server that would cause the rollback to fail, such as
- // a closed connection.
- Console.WriteLine("T1Rollback Exception Type: {0}", ex2.GetType());
- Console.WriteLine(" Message: {0}", ex2.Message);
- }
- }
- }
- cs.Close();
- //reader = cmd.ExecuteReader();
- //// Data is accessible through the DataReader object here.
- //if (reader.HasRows)
- //{
- // while (reader.Read())
- // {
- // Console.WriteLine(reader.GetInt32(0).ToString() + reader.GetString(1));
- // }
- //}
- //else
- //{
- // Console.WriteLine("No rows found.");
- //}
- //reader.Close();
- }
- private static void doTran2()
- {
- SqlConnection cs = new SqlConnection("Data Source=DESKTOP-I7QN8TP\\SQLEXPRESS;Initial Catalog = Magazin_Dulciuri; Integrated Security = True");
- cs.Open();
- int incercari = 0;
- while (incercari < 3)
- {
- SqlTransaction tr2 = cs.BeginTransaction();
- SqlCommand cmd = new SqlCommand();
- SqlCommand cmd2 = new SqlCommand();
- SqlDataReader reader;
- cmd.CommandText = "UPDATE Produs_Dulce SET denumire='am blocat T2CSharp 'WHERE idD = 100";
- cmd.CommandType = CommandType.Text;
- cmd.Transaction = tr2;
- cmd.Connection = cs;
- cmd2.CommandText = "UPDATE Ingrediente SET nume='am blocat T2CSharp' WHERE idI = 100";
- cmd2.CommandType = CommandType.Text;
- cmd2.Transaction = tr2;
- cmd2.Connection = cs;
- try
- {
- cmd2.ExecuteNonQuery(); //blocare Imgrediente
- Thread.Sleep(3000);
- cmd.ExecuteNonQuery(); //e deja blocat in T1
- tr2.Commit();
- Console.WriteLine("tranzactie T2 reusita dupa " + incercari + " incercari");
- incercari = 3;
- }
- catch (Exception ex)
- {
- Console.WriteLine("DEADLOCK");
- try
- {
- Console.WriteLine("rollback t2");
- tr2.Rollback();
- incercari++;
- }
- catch (Exception ex2)
- {
- // This catch block will handle any errors that may have occurred
- // on the server that would cause the rollback to fail, such as
- // a closed connection.
- Console.WriteLine("T2Rollback Exception Type: {0}", ex2.GetType());
- Console.WriteLine(" Message: {0}", ex2.Message);
- }
- }
- }
- cs.Close();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement