Bug #112195 getWarnings() of StatementImpl contains all warnings
Submitted: 28 Aug 2023 6:58 Modified: 15 Dec 2023 22:16
Reporter: jacky lai Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / J Severity:S3 (Non-critical)
Version:8.1.0, 8.0.33 OS:Any
Assigned to: CPU Architecture:Any

[28 Aug 2023 6:58] jacky lai
Description:
The doc of getWarings method in StatementImpl says that "The warning chain is automatically cleared each time a statement is (re)executed". But it's not actually. I don't know it's a bug or ducument wrong.

How to repeat:
Here is the test case, it will create two tables fist run without log, but second time it will print duplicate log as below, the table1 warning log twice.

SQLWarning ignored: SQL state '42S01', error code '1050', message [Table 'table1' already exists]
SQLWarning ignored: SQL state '42S01', error code '1050', message [Table 'table1' already exists]
SQLWarning ignored: SQL state '42S01', error code '1050', message [Table 'table2' already exists]

        String sql1 = "CREATE TABLE IF NOT EXISTS `table1`(`id` bigint unsigned);";
        String sql2 = "CREATE TABLE IF NOT EXISTS `table2`(`id` bigint unsigned);";
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/lfz", "root", "root");
        Statement statement = connection.createStatement();
        statement.execute(sql1);
        SQLWarning warningToLog = statement.getWarnings();
        while (warningToLog != null) {
            System.out.println(("SQLWarning ignored: SQL state '" + warningToLog.getSQLState() +
                    "', error code '" + warningToLog.getErrorCode() +
                    "', message [" + warningToLog.getMessage() + "]"));
            warningToLog = warningToLog.getNextWarning();
        }
        statement.execute(sql2);
        warningToLog = statement.getWarnings();
        while (warningToLog != null) {
            System.out.println(("SQLWarning ignored: SQL state '" + warningToLog.getSQLState() +
                    "', error code '" + warningToLog.getErrorCode() +
                    "', message [" + warningToLog.getMessage() + "]"));
            warningToLog = warningToLog.getNextWarning();
        }
[28 Aug 2023 7:09] jacky lai
test case

Attachment: jdbcdemo.zip (application/zip, text), 13.48 KiB.

[28 Aug 2023 9:48] MySQL Verification Team
Hello Jacky lai,

Thank you for the report and test case.
Verified as described.

regards,
Umesh
[15 Dec 2023 22:16] Daniel So
Posted by developer:
 
Added the following entry to the Connector/J 8.3.0 changelog: 

"Statement.getWarnings() did not clear the warning chain automatically each time a statement was executed, as it was expected to do."