【发布时间】:2011-02-06 18:52:45
【问题描述】:
我正在尝试创建数据库死锁并且我正在使用 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 正在按顺序执行测试方法。
-
我有两个 JUnit 测试用例并运行一个,然后切换到另一个并运行那个,它显示它们都在运行。
-
我明白了。您使用的是什么事务隔离级别?
-
那是我没有设置的。我不确定这意味着什么,但我记得教授告诉我们不要更改交易的超时时间。我不确定这是否相关。