Bug #117928 Cursor-based reading can fail when SQL contains a semicolon and allowMultiQueries=true
Submitted: 10 Apr 6:29 Modified: 10 Apr 9:51
Reporter: 镇熙 林 Email Updates:
Status: Duplicate Impact on me:
None 
Category:Connector / J Severity:S2 (Serious)
Version:8.0.25 OS:Any
Assigned to: CPU Architecture:Any

[10 Apr 6:29] 镇熙 林
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();
        }
    }
}
[10 Apr 9:02] MySQL Verification Team
Hello 镇熙 林,

Thank you for the report and test case.
IMHO this is duplicate of Bug #115832, please see Bug #115832 for more details.

regards,
Umesh
[10 Apr 9:51] 镇熙 林
not same with 115832 which is about multi-statements (multiple statements within a single string separated by ; characters),but this is about single statement just end with ';'