1 Reply Latest reply: Jan 3, 2011 12:23 AM by 811814 RSS

    Heap space error while creating XML document from Resultset

    imation3m
      I am getting Heap space error while creating XML document from Resultset.

      It was working fine from small result set object but when the size of resultset was more than 25,000, heap space error

      I am already using -Xms32m -Xmx1024m

      Is there a way to directly write to xml file from resultset instead of creating the whole document first and then writing it to file? Code examples please?

      here is my code:
      stmt = conn.prepareStatement(sql);
      result = stmt.executeQuery();
      result.setFetchSize(999);
      
      Document doc = JDBCUtil.toDocument(result, Application.BANK_ID, interfaceType, Application.VERSION);
      
      JDBCUtil.write(doc, fileName);
      public static Document toDocument(ResultSet rs, String bankId, String interfaceFileType, String version)
          throws ParserConfigurationException, SQLException {
              DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
              DocumentBuilder builder = factory.newDocumentBuilder();
              Document doc = builder.newDocument();
              
              Element results = doc.createElement("sims");
              results.setAttribute("bank", bankId);
              results.setAttribute("record_type", "HEADER");
              results.setAttribute("file_type", interfaceFileType);
              results.setAttribute("version", version);
              doc.appendChild(results);
              
              ResultSetMetaData rsmd = rs.getMetaData();
              int colCount = rsmd.getColumnCount();
              String columnName="";
              Object value;
      
              while (rs.next()) {
                  Element row = doc.createElement("rec");
                  results.appendChild(row);
                  
                  for (int i = 1; i <= colCount; i++) {
                      columnName = rsmd.getColumnLabel(i);
                      value = rs.getObject(i);
                      
                      Element node = doc.createElement(columnName);
                      if(value != null)
                          node.appendChild(doc.createTextNode(value.toString()));
                      else
                          node.appendChild(doc.createTextNode(""));
                      row.appendChild(node);
                  }
              }
              return doc;
          }
      
      
      public static void write(Document document, String filename) {
      
              //long start = System.currentTimeMillis();
              // lets write to a file
              OutputFormat format = new OutputFormat(document); // Serialize DOM
              format.setIndent(2);
              format.setLineSeparator(System.getProperty("line.separator"));
              format.setLineWidth(80);
              try {
      
                  FileWriter writer = new FileWriter(filename);
                  BufferedWriter buf = new BufferedWriter(writer);
                  XMLSerializer FileSerial = new XMLSerializer(writer, format);
                  FileSerial.asDOMSerializer(); // As a DOM Serializer
                  FileSerial.serialize(document);
                  
                  writer.close();
                  
              } catch (IOException ioe) {
                  ioe.printStackTrace();
      
              }
              //long end = System.currentTimeMillis();
              //System.err.println("W3C File write time :" + (end - start) + "  " + filename);
          }