I agree with Richard's strategy *but* I think it lacks an important feature:
paging.
 
You may want to check the thread "Paging large database result sets"
 
Executive summary:
1. provide a method to get the count of records that fit the query
2. provide a method to get a pge of records
 
So instead of:
public class PayRollBean implements javax.ejb.SessionBean [
    public EJBResultSet getEmployeeSalaries( ){
        DataSource dataSource =
(DataSource)cntx.lookup("java:comp/env/jdbc/db");
        Connection con = dataSource.getConnection( );
        Statement stmt = con.createStatement( );
        ResultSet set = stmt.executeQuery("select LNAME, FNAME, SALARY from
EMPLOYEE");

        CustomResultSet customSet = new CustomResultSet(set);
        return (EJBResultSet)customSet;
   }
    ...
}
use:
 
public class PayRollBean implements javax.ejb.SessionBean [

  public int getEmployeeSalariesCount(){
        DataSource dataSource = (DataSource)cntx.lookup("java:comp/env/jdbc/db");
        Connection con = dataSource.getConnection( );
        Statement stmt = con.createStatement( );
        ResultSet set = stmt.executeQuery("select COUNT(*) from EMPLOYEE");
        return set.getInt(1);
    }

    public Vector getEmployeeSalaries(int pini, int ptot){
        DataSource dataSource = (DataSource)cntx.lookup("java:comp/env/jdbc/db");
        Connection con = dataSource.getConnection( );
        Statement stmt = con.createStatement( );
        ResultSet set = stmt.executeQuery("select LNAME, FNAME, SALARY from
EMPLOYEE");
        // get a page using JDBC 1.22
       /*int nrec = 0;
       int tot  = pini+ptot;
       Vector result = new Vector();
       while( set.next() ) {
         if ( nrec++ < pini ) continue;    // wasted effort JDBC 1.22
         if ( (ptot>0)&&(nrec>tot) ) break;// no more records, thank you
         result.addElement( makeAnObjectOutOfCurrentRecord() );
       }*/
        // get a page using JDBC 2.0
       absolute(pini);
       int nrec = 0;
       Vector result = new Vector();
       while( set.next() ) {
         //if ( nrec++ < pini ) continue; // NO wasted effort using JDBC 2.0
         if ( (ptot>0)&&(++nrec>tot) ) break;// no more records, thank you
         result.addElement( makeAnObjectOutOfCurrentRecord() );
       }
       return result;
    }
    ...
}
 
The client first asks how many records fit the query, then asks for pages
depending on the user actions.
 
This way we are not collapsing the client memory with thousands of records
it does not really need.
 
Regards
 
    Javier
 

Reply via email to