我有一个基于SQL Server 2012的Web应用程序,我使用Java来更新数据库中的数据。 (Windows Server 2008,JSP,Tomcat7,Java7)
相关代码如下:
public static synchronized int execute(String dsName, String packageAndFunction, List fields) {
// prepare insertStr
String executeStr = buildStatement(dsName, packageAndFunction, null, fields);
dbConn = DBConnection.getInstance();
Connection conn = dbConn.getConnection();
CallableStatement stmt = null;
int result = RESULT_FAILED;
try {
stmt = conn.prepareCall(executeStr);
// fill statement parameters (each ?)
fillStatement(stmt, fields);
stmt.execute();
result = stmt.getInt(fields.size());
} catch(SQLException e) {
Log.getInstance().write("Exception on executeGeneral (" + packageAndFunction + ") " + e.toString());
} finally {
try {
stmt.close();
dbConn.returnConnection(conn);
} catch(SQLException e) {
Log.getInstance().write("Exception on executeGeneral (" + packageAndFunction + ") " + e.toString());
}
}
return result;
}
大约90%的时间,代码运行良好。剩下的时间里,桌子上有某种锁定,大约半小时左右就会消失。该锁甚至可以防止表上的简单SELECT查询执行(在SQL Server Management Studio中)。在严重的情况下,它阻止了整个应用程序的运行。
我有一个想法是使用stmt.executeUpdate()
而不是stmt.execute()
,但我试图对此进行研究,但我没有看到任何使用stmt.execute()
进行更新导致锁定的证据。
有人可以帮忙吗? 谢谢!
答案 0 :(得分:2)
使用该代码进行诊断很困难。下次发生这种情况时,请在SQL服务器上调出活动监视器,看看sql命令持有锁。