关于ejb使用单例方式获取id时发生的死锁2?

04-08-12 jorwang

代码如下:

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语句应该不会出现这种问题啊。完全晕了。请各位大虾帮我分析一下,给一个解决方案吧。谢谢了。

banq
2004-08-15 16:00

>PoolManage.getEngineeConnection();

你使用了自己的连接池,有可能争夺锁定在同一个线程上。