關於ejb使用單例方式獲取id時發生的死鎖2?

jorwang發表於2004-08-12
程式碼如下:
private void getNextKeyFromDB(String tableName,int count,Integer[] id)
throws JorException{
if (count == 0) {
throw new JorException("...");
}

boolean success = false;
PreparedStatement pstmt = null;
Connection conn = null;
ResultSet rs = null;
try {
conn = PoolManage.getEngineeConnection();
pstmt = conn.prepareStatement("SELECT MAXID FROM SYS_IDCREATOR where TableName=?");
pstmt.setString(1, tableName);
rs = pstmt.executeQuery();
if (!rs.next()) {
throw new JorException("xxxx");
}
int currentID = rs.getInt(1);
rs.close();
pstmt.close();
int newID = currentID + 1;
pstmt = conn.prepareStatement("UPDATE SYS_IDCREATOR SET MAXID = ? where TableName=? and MAXID = ?");

pstmt.setInt(1, newID);
pstmt.setString(2, tableName);
pstmt.setInt(3, currentID);

success = pstmt.executeUpdate() == 1;
if (success) {
id[0] = new Integer(currentID);
}
}
catch (SQLException ex) {
throw new JorException(ex.toString());
}
finally {
DBHelp.closeit(conn, rs, pstmt);
}
if (!success) {
try {
Thread.currentThread().sleep(30);
} catch (InterruptedException ie) { }
getNextKeyFromDB(tableName,count-1,id);
}
}

透過多執行緒客戶端測試過程中發現執行rs = pstmt.executeQuery()時會出現莫名其妙的停止,不知道是否是死鎖,但是select語句應該不會出現這種問題啊。完全暈了。請各位大蝦幫我分析一下,給一個解決方案吧。謝謝了。

相關文章