Salve a tutti; stavo provando a lavorare con i file XML; in pratrica da una tabella denominata "Anagrafica" su DB Oracle sto cercando prelevare dei dati e scaricarli tutti in un file xml; il problema sta nel fatto che i dati da prelevare sono circa 500000 e, per il pezzo di codice che scrivo, per ogni record che estrapolo creo dei nuovi oggetti Element...il problema è che dopo un po' di tempo l'applicazione va in eccezione java.lang.OutOfMemoryException; ho provato ad aumentare la memoria della VM utilizzando java -Xmx190m dandole così 190 MB di spazio ma non è cambiato quasi nulla (solo il tempo necessari che si riempisse la memoria ); le domande che volevo porvi sono le seguenti: secodo voi è giusta una tecnica di tal tipo? Nel senso che XML è potente per il trasporto datri ma questi non saranno un po' troppi? E poi conoscete qualche metodo per evitare di istanziare sempre questi Element?Di seguito riporto il codice da me scritto.......e scusatemi per la lunghezza del messaggio
import java.io.*;
import java.sql.*;
import java.util.*;
/*
IMPORTIAMO I PACKAGE NECESSARI PER
LAVORATE CON GLI XML
*/
import org.jdom.*;
import org.jdom.output.XMLOutputter;
/**
*
Title: </p>
*
Description: </p>
*
Copyright: Copyright (c) 2003</p>
*
Company: </p>
* @author not attributable
* @version 1.0
*/
public class Scrivi {
private Connection co;
private Statement st;
private String select = " SELECT cod_fis_ass codice, nome_ass nome, cogn_ass cognome,"+
" ind_ass indirizzo,cap_ass cap,com_ass_res comune,pro_ass prov,"+
" cod_asl_res asl FROM anagrafica ORDER BY cod_fis_ass";
private String count = " SELECT count(*) dimensione FROM assistito";
private int dimensione;
private ResultSet rsDim;
private ResultSet rsScr;
public Scrivi() {
//System.setProperty("Memoria"," -Xms90m -Xmx200m ");
}
private void write(){
String nomeFile = "C:/sviluppo/records.xml";
try{
writeFile(nomeFile);
}catch(Exception e){
e.printStackTrace();
System.err.println ("errore: "+e.getMessage());
}
}
public void writeFile(String path) throws Exception{
File fileTmp = new File(path);
/*
ISTANZIAMO UN OGGETTO Element
PASSANDOGLI LA STRINGA
CHE SARA' IL NOME DEL NOSTRO SUPERTAG */
Element root = new Element("Prova");
/*
ISTANZIAMO UN OGGETTO Document
PASSANDOGLI COME PARAMETRO L'OGGETTO
ELEMENT PRECEDENTEMENTE ISTANZIATO
/**/
Document documento = new Document(root);
Class.forName("oracle.jdbc.driver.OracleDriver");
this.co=DriverManager.getConnection("jdbcracle:thin:xxx/xxx@xxx.xxx.xx.xx:xxxx:xxxx");
this.st = this.co.createStatement();
rsDim = st.executeQuery(this.count);
if( rsDim.next() ){
this.dimensione = rsDim.getInt(1);
}
/*
ISTANZIAMO ALTRI OGGETTI Element
PASSANDOGLI LE STRINGHE
CHE SARANNO I NOMI DEI NOSTRI TAG
*/
Element initparam[] = new Element[this.dimensione];
Element paramname[] = new Element[this.dimensione];
Element param[] = new Element[this.dimensione];
Element paramn[] = new Element[this.dimensione];
Element paramna[] = new Element[this.dimensione];
Element par[] = new Element[this.dimensione];
Element para[] = new Element[this.dimensione];
Element p[] = new Element[this.dimensione];
Element pa[] = new Element[this.dimensione];
int u = 1;
Calendar c = Calendar.getInstance();
for( int y = 0; y < this.dimensione; y++ ){
initparam[y] = new Element("codici");
paramname[y] = new Element("Codice_fiscale");
param[y] = new Element("nome");
paramn[y] = new Element("cognome");
paramna[y] = new Element("indirizzo");
par[y] = new Element("cap");
para[y] = new Element("comune");
p[y] = new Element("prov");
pa[y] = new Element("asl");
System.gc();
}
this.st.clearBatch();
rsScr = st.executeQuery(this.select);
int i = 0;
int j=0;
while( this.rsScr.next() ){
paramname[i].addContent(rsScr.getString(1));
param[i].addContent(rsScr.getString(2));
paramn[i].addContent(rsScr.getString(3));
paramna[i].addContent(rsScr.getString(4));
par[i].addContent(rsScr.getString(5));
para[i].addContent(rsScr.getString(6));
p[i].addContent(rsScr.getString(7));
pa[i].addContent(rsScr.getString(8));
initparam[i].addContent(paramname[i].toString());
initparam[i].addContent(param[i].toString());
initparam[i].addContent(paramn[i].toString());
initparam[i].addContent(paramna[i].toString());
initparam[i].addContent(par[i].toString());
initparam[i].addContent(para[i].toString());
initparam[i].addContent(p[i].toString());
initparam[i].addContent(pa[i].toString());
root.addContent(initparam[i].toString());
System.gc();
i++;
j++;
if(j==99){
System.out.println(i+" record "+c.get(Calendar.HOUR)+":"+c.get(Calendar.MINUTE)+ ":"+c.get(Calendar.SECOND));
j=0;
}
}
BufferedWriter out = null;
try{
out = new BufferedWriter(new FileWriter(path));
/*
ISTANZIAMO LA CLASSE XMLOutputter
CHE CI PERMETTE DI FORMATTARE IL
FILE XML.
*/
XMLOutputter xout = new XMLOutputter();
xout.setIndent(true);
xout.setNewlines(true);
/*
CON QUESTA ISTRUZIONE SCRIVIAMO
IL DOCUMENTO documento IN UN
BufferedWriter CHE CREA IL FILE
TRAMITE UN FileWriter
*/
xout.output(documento,out);
}catch (IOException e){
System.err.println ("ore: "+e.getMessage());
}finally{
try{
out.close();
}catch(IOException e){
e.printStackTrace();
System.err.println ("rrore: "+e);
}
}
}
public static void main(String[] args){
Scrivi s = new Scrivi();
s.write();
}
}