[英]How to create deadlock without using transaction?
I create simple console application in C# to simulate deadlock error. 我在C#中创建了简单的控制台应用程序,以模拟死锁错误。 I used two threads.
我使用了两个线程。 Code:
码:
class Program
{
static void Main(string[] args)
{
Thread[] tt = new Thread[2];
try
{
tt[0] = new Thread(queryAdo1);
tt[0].Priority = ThreadPriority.Highest;
tt[0].Start();
tt[1] = new Thread(queryAdo2);
tt[1].Priority = ThreadPriority.Highest;
tt[1].Start();
}
catch (Exception ex)
{
Console.WriteLine(ex);
Console.ReadLine();
}
}
private static void queryAdo1()
{
try
{
SqlConnection con = new SqlConnection("data source=.; database=BazaTest; integrated security=SSPI");
SqlCommand cmd = new SqlCommand("spTransaction1", con);
cmd.CommandType = System.Data.CommandType.StoredProcedure;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
Console.WriteLine("Done Well 1");
}
catch (SqlException ex)
{
if (ex.Number == 1205)
{
Console.WriteLine("DeadLock 1");
Console.ReadLine();
}
}
}
private static void queryAdo2()
{
try
{
SqlConnection con = new SqlConnection("data source=.; database=BazaTest; integrated security=SSPI");
SqlCommand cmd = new SqlCommand("spTransaction2", con);
cmd.CommandType = System.Data.CommandType.StoredProcedure;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
Console.WriteLine("Done Well 2");
}
catch (SqlException ex)
{
if (ex.Number == 1205)
{
Console.WriteLine("DeadLock 2");
Console.ReadLine();
}
}
}
}
Stored procedures: 存储过程:
Create table TableA
(
Id int identity primary key,
Name nvarchar(50)
)
Go
Insert into TableA values ('Mark')
Go
Create table TableB
(
Id int identity primary key,
Name nvarchar(50)
)
Go
Insert into TableB values ('Mary')
Go
Create procedure spTransaction1
as
Begin
Begin Tran
Update TableA Set Name = 'Mark Transaction 1' where Id = 1
Waitfor delay '00:00:05'
Update TableB Set Name = 'Mary Transaction 1' where Id = 1
Commit Transaction
End
Create procedure spTransaction2
as
Begin
Begin Tran
Update TableB Set Name = 'Mark Transaction 2' where Id = 1
Waitfor delay '00:00:05'
Update TableA Set Name = 'Mary Transaction 2' where Id = 1
Commit Transaction
End
When I run my code I get response in console like this: 当我运行代码时,我会在控制台中得到如下响应:
Done well 1
Deadlock 2
and it works correctly. 它可以正常工作。 I tried to create deadlock without using transaction but now i have no more ideas.
我试图在不使用事务的情况下创建死锁,但是现在我没有更多的想法了。 I tried to eliminate transaction command from stored procedures spTransaction1 and spTransaction2 and i create more Threads using loop but I didnt create deadlock as I expected.I don't have big experience in parallel programming so maybe I did something wrong.. Is there any possibility to create deadlock without using transaction?
我试图从存储过程spTransaction1和spTransaction2中消除事务命令,并使用循环创建了更多线程,但是我没有按预期的那样创建死锁。我在并行编程方面没有丰富的经验,所以也许我做错了什么。创建死锁而不使用事务? If yes please write some example.
如果是,请写一些例子。 It will be great if you'll use my code to solve this case ;) Thank you!
如果您将使用我的代码来解决这种情况,那就太好了;)谢谢!
Deadlocks are unresolvable conflicts for resources - A needs B, B needs A during a sequence of instructions. 死锁是资源无法解决的冲突-一系列指令期间A需要B,B需要A。 Without transactions you're just running a bunch of single instructions.
没有交易,您将只运行一堆单一指令。 They might fail (or complete) but they won't deadlock because there is no dependency between the bits.
它们可能会失败(或完成),但不会死锁,因为这些位之间没有依赖性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.