Description:
When I use JDBC with cursor-based streaming to retrieve a set number of rows each time (useCursorFetch=true, FetchSize>0), and also set the JDBC parameter allowMultiQueries=true, if the SQL statement I execute contains a semicolon (;), then in JDBC 8.0.25, cursor-based reading will fail, and all the data will be directly cached on the client, which can easily cause a client OOM (Out of Memory) error if the table size is large. However, this issue does not exist in JDBC version 5.1.40. Please confirm if this is a bug.
How to repeat:
You are experiencing a problem where using JDBC 8.0.25 with cursor-based streaming (useCursorFetch=true, FetchSize>0) and multi-query (allowMultiQueries=true) settings, the SQL statement executed contains a semicolon (;), results in a failure of cursor-based reading. This causes all data to be cached client-side, leading to potential Out Of Memory (OOM) errors—a problem that does not occur in JDBC 5.1.40.
Here’s a Java code example to reproduce this issue:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class JDBCCursorFetchExample {
public static void main(String[] args) {
String jdbcUrl = "jdbc:mysql://localhost:3306/your_database?useCursorFetch=true&allowMultiQueries=true";
String username = "your_username";
String password = "your_password";
int fetchSize = 10;
try (Connection connection = DriverManager.getConnection(jdbcUrl, username, password);
Statement statement = connection.createStatement()) {
statement.setFetchSize(fetchSize);
String sql = "SELECT * FROM large_table1;";
boolean isResultSet = statement.execute(sql);
while (isResultSet) {
try (ResultSet resultSet = statement.getResultSet()) {
while (resultSet.next()) {
// Process the rows here
System.out.println(resultSet.getString(1));
}
}
// Move to the next result
isResultSet = statement.getMoreResults();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}