Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Configuration;
- using System.Data;
- using System.Data.SqlClient;
- using System.Linq;
- using System.Text;
- using System.Threading;
- namespace lab3_sgbd
- {
- class Program
- {
- static SqlConnection connection;
- static public void ShowSample()
- {
- connection = new SqlConnection(ConfigurationManager.ConnectionStrings["db"].ConnectionString);
- connection.Open();
- SqlCommand selectCommand1 = new SqlCommand("select * from Managers where Mid = 1", connection);
- SqlCommand selectCommand2 = new SqlCommand("select * from Employees where Eid = 4", connection);
- SqlDataReader selection1 = selectCommand1.ExecuteReader();
- selection1.Read();
- Console.WriteLine(selection1.GetValue(1));
- selection1.Close();
- SqlDataReader selection2 = selectCommand2.ExecuteReader();
- selection2.Read();
- Console.WriteLine(selection2.GetValue(1));
- selection2.Close();
- }
- static public void RunThread1()
- {
- connection = new SqlConnection(ConfigurationManager.ConnectionStrings["db"].ConnectionString);
- connection.Open();
- for (int i = 0; i < 3; i++)
- {
- try
- {
- SqlCommand command = new SqlCommand("tran1", connection);
- command.CommandType = CommandType.StoredProcedure;
- command.ExecuteNonQuery();
- ShowSample();
- Console.WriteLine("Thread1 executed transaction successfully after " + i + " tries!!\n");
- return;
- }
- catch (SqlException e)
- {
- Console.WriteLine("Thread1 is ded but still trying -----> deadlock!!\n ");
- }
- }
- Console.WriteLine("Thread1 is ded forever :(!!\n");
- }
- static public void RunThread2()
- {
- SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["db"].ConnectionString);
- connection.Open();
- for (int i = 0; i < 3; i++)
- {
- try
- {
- SqlCommand command = new SqlCommand("tran2", connection);
- command.CommandType = CommandType.StoredProcedure;
- command.ExecuteNonQuery();
- ShowSample();
- Console.WriteLine("Thread2 executed transaction successfully after " + i + " tries!!\n");
- return;
- }
- catch (SqlException e)
- {
- Console.WriteLine("Thread2 is ded but still trying -----> deadlock!!\n ");
- }
- }
- Console.WriteLine("Thread2 is ded forever :(!!\n");
- }
- static public void Main(string[] args)
- {
- Thread thr1 = new Thread(new ThreadStart(RunThread1));
- Thread thr2 = new Thread(new ThreadStart(RunThread2));
- thr1.Start();
- thr2.Start();
- Console.ReadLine();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement