我想创建一个数据库死锁,并且正在使用JUnit。我有两个并发的测试运行,它们都在循环中反复更新表中的同一行。如何使用jdbc和JUNIT创建数据库死锁
我的想法是,你在一次测试中一次又一次地更新表A中的A行,然后B表中的B行。然后同时更新行B表B,然后重复更新A行表A。根据我的理解,这最终会导致僵局。
这里是代码第一次测试。
public static void testEditCC()
{
try{
int rows = 0;
int counter = 0;
int large=10000000;
Connection c=DataBase.getConnection();
while(counter<large)
{
int pid = 87855;
int cCode = 655;
String newCountry="Egypt";
int bpl = 0;
stmt = c.createStatement();
rows = stmt.executeUpdate("UPDATE main " + //create lock on main table
"SET BPL="+cCode+
"WHERE ID="+pid);
rows = stmt.executeUpdate("UPDATE BPL SET DESCRIPTION='SomeWhere' WHERE ID=602"); //create lock on bpl table
counter++;
}
assertTrue(rows == 1);
//rows = stmt.executeUpdate("Insert into BPL (ID, DESCRIPTION) VALUES ("+cCode+", '"+newCountry+"')");
}
catch(SQLException ex)
{
ex.printStackTrace();
//ex.getMessage();
}
}
这里是第二个测试的代码。
public static void testEditCC()
{
try{
int rows = 0;
int counter = 0;
int large=10000000;
Connection c=DataBase.getConnection();
while(counter<large)
{
int pid = 87855;
int cCode = 655;
String newCountry="Jordan";
int bpl = 0;
stmt = c.createStatement();
//stmt.close();
rows = stmt.executeUpdate("UPDATE BPL SET DESCRIPTION='SomeWhere' WHERE ID=602"); //create lock on bpl table
rows = stmt.executeUpdate("UPDATE main " + //create lock on main table
"SET BPL="+cCode+
"WHERE ID="+pid);
counter++;
}
assertTrue(rows == 1);
//rows = stmt.executeUpdate("Insert into BPL (ID, DESCRIPTION) VALUES ("+cCode+", '"+newCountry+"')");
}
catch(SQLException ex)
{
ex.printStackTrace();
}
}
我在同一时间运行两个独立的JUnit测试,并正在连接到,我在网络模式运行在Eclipse的Apache Derby数据库。任何人都可以帮我弄清楚为什么僵局不会发生?也许我正在使用JUnit错误。
您如何在同一时间运行两种测试方法? JUnit正在按顺序执行测试方法。 – 2010-04-11 19:49:08
我有两个JUnit测试用例并运行一个,然后切换到另一个并运行该测试用例,并显示它们都在运行。 – Isawpalmetto 2010-04-11 19:53:37
我明白了。您使用的是什么事务隔离级别? – 2010-04-11 19:55:45