我想創建一個數據庫死鎖,並且正在使用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