tags:

views:

157

answers:

2

hello fellow java developers.

I'm having a bit of an issue here. I have code that gets a resultset from an oracle database, prints each row to a file, then gets the next row - and continues till the end of the resultset.

Only this isn't what happens. What happens is that it gets the resultset, starts iterating through the rows, printing to file as it goes, until it runs out of memory - claiming it needs more space on the java heap.

The app is currently running with 2g of memory on the heap and the code breaks at about the 150000th row.

I'm using jodbc6.jar and java 6

Here is an idea of what my code is doing:

Connection conn = DriverManager.getConnection(url,"name","pwd");

conn.setAutoCommit(false);
Statement stmt = conn.createStatement();
ResultSet rset = stmt.executeQuery(strSql);

String strVar_1 = null;

long lCount = 0;
while(rset.next()){
        lCount++;
        if (lCount % 100000 == 0){
            System.out.println(lCount + " rows completed");
        }
      strVar_1 = rset.getString("StringID");  /// breaks here!!!!!!!!!

      if (strVar_1 == null){
        strVar_1 = "";
      }         
      if (!strQuery_1.equals("")){
        out.write(strVar_1 + "\n");
      }
 }
 out.close();
+4  A: 

Try below:

Statement stmt = conn.createStatement();
stmt.setFetchSize(someInt);
ResultSet rset = stmt.executeQuery(strSql);

This will control how many records are fetched at a time.

Taylor Leese
Ok, will try it and report back (query takes a while to return records from across the network) thanks
rockit
I figured your query was returning quite a large number of rows. Just as a side note, you should also call close on the ResultSet, Statement, and Connection.
Taylor Leese
I do, just left it out of the code above - thanks
rockit
I think its working, its just a little weird... the result is that when my count is written to the screen down to X00,000. all of the counts clear, and begin again starting at 100,000. Very odd behaviour... Its like its running the process over and over again. Do I need to add anything special to my while clause? Like, getnextfetchset or something?
rockit
No, it should be transparent.
Taylor Leese
What does your actual SQL query look like?
Taylor Leese
select * order by x,y,z
rockit
its still running, so its definitely working much better than before. Thanks for your help! (marked as answered)
rockit
If you are actually using select * you should change that to only select what you actually need. As a general rule, you should never use select * in a production system.
Taylor Leese
sadly, that is what I need. I need to analyze all the data in the table!
rockit
A: 

Well maybe another way of dealing with such large data is to keep returning the row and writing it to a file. This way the string buffer does not keep growing and you should be able to write all the records to file system and then read them late

Fazal
Thanks - and I do the out.write(strVar_1 + "\n"); is me writing the data to file.
rockit