[CONJ-1149] addBatch doesn't cleanup subquery parameters Created: 2024-01-11  Updated: 2024-01-12  Resolved: 2024-01-12

Status: Closed
Project: MariaDB Connector/J
Component/s: MySQL compatibility
Affects Version/s: 3.3.2
Fix Version/s: N/A

Type: Bug Priority: Major
Reporter: Alessandro Assignee: Diego Dupin
Resolution: Duplicate Votes: 0
Labels: None

Issue Links:
Problem/Incident
is caused by MDEV-23864 BULK insert wrong values when command... Open

 Description   

    private static final String INSERT_COSMETICS = """
            INSERT IGNORE INTO cosmetic(name, category)
            VALUES(?, (SELECT id FROM cosmetic_category WHERE name = ?))
            """;
 
    public boolean insertCosmetics() throws SQLException {
        try (Connection connection = dataSource.getConnection();
             PreparedStatement statement = connection.prepareStatement(INSERT_COSMETICS)) {
 
            for (CosmeticCategory category : CosmeticCategory.CATEGORIES) {
                for (Cosmetic<?> cosmetic : cosmetics.getRegistrar().getCosmetics(category)) {
                    statement.setString(1, cosmetic.getName());
                    statement.setString(2, category.getName());
                    statement.addBatch();
                }
            }
 
            statement.executeBatch();
            return true;
        } catch (SQLException e) {
            throw e;
        }
    }

The following code worked perfectly in the past, but after upgrading to a newer version of the connector, the second variable is not cleaned up after running addBatch. So only the first addBatch correctly sets the second value (category.getName), while all other queries are executed without changing the second parameter, probably because it is inside a subquery? I tested the code without any batch and it worked perfectly.



 Comments   
Comment by Diego Dupin [ 2024-01-12 ]

This is indeed due to inner select used with batch. Server issue is MDEV-23864
workaround is either get ride of inner select or disable bulk in batch setting option `useBulkStmtsForInserts` to false.

Generated at Thu Feb 08 03:20:59 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.