2012-06-01 154 views
0

我很新使用jtds連接驅動程序。我已經編寫了一個應用程序,它可以讀取大約2500個大型xml文件,並對SQL服務器進行SQL查詢和運行。我看到,當我達到一定數量的XML時,我的程序運行時內存不足。我使用內存分析器在eclipse中檢查了我的phd dump文件,發現net.sourceforge.jtds.jdbc.cache.SimpleLRUCache持有大量內存。我連接到SQL服務器一次,並保持連接活着,直到我衝我所有的查詢。以下是我對服務器運行查詢的代碼。我不知道如何獲得net.sourceforge.jtds.jdbc.cache.SimpleLRUCache類的句柄,因爲它有一個明確的方法,我認爲可以清除緩存。我再次對jtds的驅動程序不太瞭解。任何人都可以幫我解決這個問題嗎?net.sourceforge.jtds.jdbc.cache.SimpleLRUCache導致內存泄漏

public boolean runQueries(String query){ 
    if (getConn() != null && query != null) { 
     Statement statement = null; 
     try { 
      long start = System.currentTimeMillis(); 
      try { 
       if(log.isLoggable(Level.FINEST)){ 
        log.finest("Processing: "+query); 
       } 
       statement = getConn().createStatement(); 
       statement.executeUpdate(query); 
      } catch (Exception e) { 
       if(log.isLoggable(Level.FINEST)){ 
        log.log(Level.SEVERE, "Failed to process  query: " 
         + query, e); 
       }else{ 
        String reportQuery = query.length() > MAX_CHARS_DISPLAY ? query.substring(0,MAX_CHARS_DISPLAY)+"..." : query; 
         log.log(Level.SEVERE, "Failed to process query: " 
           + reportQuery , e); 
       } 
      }finally{ 
       if(statement != null){ 
        try { 
         statement.close(); 
        } catch (SQLException e) { 
         log.log(Level.SEVERE,"Failed to close statement: ",e); 
        } 
       } 
      } 

      long end = System.currentTimeMillis(); 

      return true; 
     }finally{ 
      if(statement != null){ 
       try { 
        statement.close(); 
       } catch (SQLException e) { 
        log.log(Level.SEVERE,"Failed to close statement: ",e); 
       } 
      } 
     } 
    } 
    return false; 
} 

回答

1

FAQ說明如何在JDBC URL maxStatements設定一個較低的值來改變語句緩存的大小。如果您正在構建非常大的語句,則可能需要將此限制設置爲遠低於默認值500.您還可能需要查看其中的一些其他內存設置。

+0

謝謝約翰。它確實解決了我的問題。 –