Bug #2612 ResultSet fetchSize default is not working
Submitted: 2 Feb 2004 8:17 Modified: 2 Feb 2004 8:35
Reporter: Daniel Taut Email Updates:
Status: Not a Bug Impact on me:
None 
Category:Connector / J Severity:S2 (Serious)
Version:3.0.10 stable OS:Linux (Linux)
Assigned to: Mark Matthews CPU Architecture:Any

[2 Feb 2004 8:17] Daniel Taut
Description:
In my opinion there is a problem with the fetchSize in a ResultSet. The default fetchSize it should be the one set in the Statement (according with the java API doc but the default is 0)

In the code attached there are 3 prints of the getFetchedSize() one from teh statement one from teh ResultSet it should be the same at that is 5 but the output is:

stmt: fetch size: 5
rs: fetch size, should be 5, is: 0

How to repeat:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;

public class RS_test {
	
 static Connection conn = null; // database connection object.
 static Statement stmt  = null; // Statement object.     
 static ResultSet rs    = null; // ResultSet object.

 public static void main(String[] arg) { 
  try {
   // Connect to the local database
   Class.forName("com.mysql.jdbc.Driver").newInstance();
   //just making a connection to a database
   conn =  DriverManager.getConnection("jdbc:mysql://localhost/tst","root", "");
   stmt = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,
                               ResultSet.CONCUR_READ_ONLY);
   
   //seting the FetchSize on 5
   stmt.setFetchSize(5);
   String sql = "SELECT * FROM test_rs";
   System.out.println("stmt: fetch size: " + stmt.getFetchSize());
   
   //creating the result set 
   rs = (ResultSet)stmt.executeQuery(sql);
   System.out.println("rs: fetch size, should be 5, is: " + rs.getFetchSize());
   
   } catch (Exception e) {
    e.printStackTrace();
   }
  }
}

Suggested fix:
After a quick look at the sources (so probably is not the best solution or a solution at all) and starting the investigation from Statement.executeQuery(String sql) method I've concluded the folowing:

In the MysqlIO.buildResultSetWithUpdates(...) method after creating the resultSet that will be returned

ResultSet updateRs = new ResultSet(updateCount, updateID);

you should set the FetchSize with something like this:

updateRs.setFetchSize(stmt.getFetchSize());

where stmt is the initial statement.

Of course after this a testing to see if actualy the fetchSize is working propertly is necesarry.

Best regards,
Daniel Taut
[2 Feb 2004 8:35] Mark Matthews
Statement.setFetchSize() is a no-op in MySQL (because there are no cursors in MySQL), and thus the same thing is applicable to ResultSet.setFetchSize() and ResultSet.getFetchSize().

The spec states that setFetchSize() is a _hint_, and that getFetchSize() should return the actual fetch size _in_use_. Since setFetchSize() doesn't actually affect the 'in-use' value, it would be improper to return the value passed in the statement to setFetchSize() via ResultSet.getFetchSize().

In fact, really, the driver does have a bug, in that it currently _does_ return the fetch size for a given interface (in preparation for cursors being added to MySQL).