我收到了这个 SQL 异常:
16043 10.8.38.30> 2012-03-06 14:21:00,870 | INFO | SubThread-10 | DatabaseSender: Executing SQL Statement...; SQL Statement = select ERRORCODE from ERR_MASTER where ErrorDescription='State must be 'pre-installed', 'active', 'inactive' or 'deactive''
16044 10.8.38.30> 2012-03-06 14:21:00,872 | ERROR | SubThread-10 | Exception caught: E_01_003_0 ORA-00933: SQL command not properly ended
16045
16046 10.8.38.30> 2012-03-06 14:21:00,872 | ERROR | SubThread-10 | SQLState: 42000
16047 10.8.38.30> 2012-03-06 14:21:00,872 | ERROR | SubThread-10 | Errorcode: 933
16048 10.8.38.30> 2012-03-06 14:21:00,872 | ERROR | SubThread-10 | Exception Stack:
16049 10.8.38.30> 2012-03-06 14:21:00,873 | ERROR | SubThread-10 | java.sql.SQLException: ORA-00933: SQL command not properly ended
当然,问题来自ErrorDescription='State must be 'pre-installed', 'active', 'inactive' or 'deactive''在单引号内有单引号字符串。
但是给出下面的代码,遇到上面的异常会不会导致连接泄漏呢?
执行 SQL 的代码:
private String getErrorCode(Connection conn, PreparedStatement ps, ResultSet result, String sErrorDesc) throws Exception {
String sErrorCode = null;
StringBuffer sBuffer = new StringBuffer();
sBuffer.append("SELECT ").append(aaConstants.COL_ERRMASTER_ERRORCODE);
sBuffer.append(" from ").append(aaConstants.TABLE_ERRMASTER);
sBuffer.append(" where ").append(aaConstants.COL_ERRMASTER_ERRORDESCRIPTION);
sBuffer.append(" = '").append(sErrorDesc).append("'");
try{
ps = conn.prepareStatement(sBuffer.toString());
Trace.info("sql : " +sBuffer.toString());
result = ps.executeQuery();
if (result.next())
sErrorCode = result.getString(aaConstants.COL_ERRMASTER_ERRORCODE);
}finally{
aaUtils.cleanUp(null, ps, result);
}
if (sErrorCode == null || sErrorCode.equals(""))
sErrorCode = aaErrorCode.MsgCode_Default_ErrorCode;
return sErrorCode;
}
上面的方法在这里被调用:
public String getServerExpiredResponse() throws AuthException {
String retstr="", sessionid="";
Connection conn = null;
PreparedStatement ps = null;
ResultSet result = null;
if (!loginok) {
try{
conn = aaUtils.getDBConnection();
String session_loginfailure_faultcode = getErrorCode(conn, ps, result, sErrorDesc);
String session_loginfailure_faultstring = sErrorDesc;
if (session_loginfailure_faultcode==null) {session_loginfailure_faultcode="";}
if (session_loginfailure_faultstring==null) {session_loginfailure_faultstring="Failed.";}
retstr +=
"<faultcode>"
+ session_loginfailure_faultcode
+ "</faultcode>"
+ "<faultstring>"
+ session_loginfailure_faultstring
+ "</faultstring>";
} catch (Exception e) {
throw new AuthException(Trace.stack2string(e));
}finally{
aaUtils.cleanUp(conn, ps, result);
}
}
Trace.info("In getServerExpiredResponse " +retstr);
return retstr;
}
调用getErrorCode()方法中DB连接关闭,由于getErrorCode()中发生异常,getServerExpiredResponse()中连接是否仍然关闭?
您可以看到在 getErrorCode() 内部只有Resultset 和 PreparedStatement 是关闭的。