2016-04-07 122 views
-4

我目前在我的代碼(Java,Struts)中面臨連接泄漏問題。我已經關閉了所有方法的所有結果集,準備好的語句,可調用語句和連接。我仍然面臨這個問題。附加信息是,我正在使用StructDescriptor.createDescriptor來創建oracle對象。它會導致任何連接泄漏?請指教。JDBC連接泄漏

代碼如下

  public boolean updatedDetails(Distribution distribution, String appCode, Connection dbConnection) { 
    boolean savedFlag = true; 
    CallableStatement updateStoredProc = null; 
    PreparedStatement pstmt1 = null; 
    try { 
     logger.debug("In DistributionDAO.updatedDistributionDetails"); 
     //PreparedStatement pstmt1 = null; 
     ARRAY liArray = null; 
     ARRAY conArray = null; 
     ARRAY payArray = null; 
    ArrayDescriptor licenseeArrDesc = ArrayDescriptor.createDescriptor(LICENSEE_TAB, dbConnection); 
     ArrayDescriptor contractArrDesc = ArrayDescriptor.createDescriptor(DISTRIBUTION_CONTRACT_TAB, dbConnection); 
     ArrayDescriptor paymentArrDesc = ArrayDescriptor.createDescriptor(DISTRIBUTION_PAYMENT_TAB, dbConnection); 
     licenseeArray = new ARRAY(licenseeArrDesc, dbConnection, licenseeEleList.toArray()); 
      contractArray = new ARRAY(contractArrDesc, dbConnection, contractEleList.toArray()); 
      paymentArray = new ARRAY(paymentArrDesc, dbConnection, paymentEleList.toArray());   
      updateStoredProc = dbConnection.prepareCall("{CALL DIS_UPDATE_PROC(?,?,to_clob(?),?,?,?,?)}"); 
      updateStoredProc.setLong(1, distribution.getDistributionId()); 
      updateStoredProc.setString(2, distribution.getId()); 
      updateStoredProc.setString(3, distribution.getNotes()); 
      updateStoredProc.setString(4, distribution.getNotesUpdateFlag()); 
      updateStoredProc.setArray(5, liArray); 
      updateStoredProc.setArray(6, conArray); 
      updateStoredProc.setArray(7, payArray); 
      String sql1="Update STORY set LAST_UPDATE_DATE_TIME= sysdate WHERE STORY_ID = ? "; 
      pstmt1=dbConnection.prepareStatement(sql1); 
      pstmt1.setLong(1,distribution.getStoryId()); 
      pstmt1.execute(); 
      List<Object> removedEleList = new ArrayList<Object>(); 
      removedEleList.add(createDeleteElementObject(removedEle, dbConnection)); 
      catch (SQLException sqle) { 
     savedFlag = false; 

    } catch (Exception e) { 
     savedFlag = false; 

    } finally { 
     try { 
      updateStoredProc.close(); 
      updateStoredProc = null;    
      pstmt1.close(); 
      pstmt1 = null;  
      dbConnection.close(); 
     } catch (SQLException e) { 

     } 
    } 
    return savedFlag; 
} 




// Method createDeleteElementObject 
private Object createDeleteElementObject(String removedEle, 
     Connection connection) { 

    StructDescriptor structDesc; 
    STRUCT structObj = null; 
    try { 
     structDesc = StructDescriptor.createDescriptor(DISTRIBUTION_REMOVED_ELEMENT_OBJ, connection); 
     if(removedEle != null) { 
      String[] tmpArr = removedEle.split("\\|"); 
      if(tmpArr.length == 2) { 
       Object[] obj = new Object[2]; 
       String eleType = tmpArr[0]; 
       long eleId = Integer.parseInt(tmpArr[1]); 
       obj[0] = eleType.toUpperCase(); 
       obj[1] = eleId; 
       structObj = new STRUCT(structDesc, connection, obj); 
      } 
     } 
    } catch (ArrayIndexOutOfBoundsException e) { 

    } catch (NumberFormatException e) { 

    } catch (SQLException e) { 

    } 

    return structObj; 
}  
+3

哪裏是你的代碼? –

+0

我在編輯中添加了代碼。 – lal1990

+0

我強烈建議您切換到試用資源。您在代碼中關閉資源的方式可能會泄漏。考慮如果'updateStoredProc.close()'或'pstmt1.close()'拋出一個異常會發生什麼。 –

回答

2

一些提示代碼:

你傳遞一個Connection變量到您的電話,但關閉它你的電話裏面 - 是主叫方知道這個事實?讓你的代碼返回內部的連接也未關閉(調用方法負責),這將是清潔

例外是爲了被抓,不會被忽略 - 你不登錄你的例外 - 你會永遠不知道會發生什麼我敢打賭,您的catch塊中的簡單e.printStackTrace()將顯示有用的信息。

使用try-with-resource(見this post

//Resources declared in try-with-resource will be closed automatically. 
try(Connection con = getConnection(); 
    PreparedStatement ps = con.prepareStatement(sql)) { 

    //Process Statement... 

} catch(SQLException e) { 
    e.printStackTrace(); 
} 

至少,把每一個接近單一的try-catch裏面:

} finally { 
    try { 
     if(updateStoredProc != null) { 
      updateStoredProc.close(); 
     }   
    } catch (SQLException e) { 
     e.printStackTrace(); 
    } 
    try { 
     if(pstmt1!= null) { 
      pstmt1.close(); 
     }   
    } catch (SQLException e) { 
     e.printStackTrace(); 
    } 
    try { 
     if(dbConnection != null) { 
      dbConnection.close(); 
     }   
    } catch (SQLException e) { 
     e.printStackTrace(); 
    } 
    } 
+0

感謝您的回覆Jan,我嘗試關閉語句和連接第二種方法。我仍然遇到連接泄漏問題。在我的standalone.xml最大池中聲明瞭該方法的次數後,我得不到任何託管異常錯誤。 – lal1990

+0

您看到日誌中有任何異常? – Jan

+0

哦 - 並且你從那裏調用許多方法傳遞連接作爲參數。您還需要檢查它們(共享代碼?) – Jan