[CONJ-83] getGeneratedKeys() does not return generated keys in case of a batch SQL INSERT Created: 2014-03-05  Updated: 2014-10-21  Resolved: 2014-10-21

Status: Closed
Project: MariaDB Connector/J
Component/s: None
Affects Version/s: 1.1.5, 1.1.6
Fix Version/s: 1.1.8

Type: Bug Priority: Major
Reporter: Tero Heittola Assignee: Massimo Siani (Inactive)
Resolution: Fixed Votes: 1
Labels: None
Environment:

JDK 1.7, (QueryDSL-SQL 3.3.1)



 Description   

It is currently not possible to execute a batch insert and retrieve the generated keys.

Expected outcome:
After calling the MySQLPreparedStatement.executeBatch(), the MySQLPreparedStatement.getGeneratedKeys() method should return a ResultSet from which the keys generated in SQL INSERTs can be obtained.

Actual outcome:
The MySQLPreparedStatement.getGeneratedKeys() returns MySQLResultSet.EMPTY after calling MySQLPreparedStatement.executeBatch().

More details:

  • The problem is reproducible e.g. by using QueryDSL-SQL. Its com.mysema.query.sql.dml.SQLInsertClause performs following operation:

    // Code simplified, see full implementation in https://github.com/mysema/querydsl/blob/master/querydsl-sql/src/main/java/com/mysema/query/sql/dml/SQLInsertClause.java:
     
    public ResultSet executeWithKeys() {
        final PreparedStatement stmt = createBatchStatement();
        stmt.executeBatch();
        return stmt.getGeneratedKeys();
    }
     
    private PreparedStatement createStatement(boolean withKeys) throws SQLException {
    	SQLSerializer serializer = new SQLSerializer(configuration, true);
    	serializer.serializeInsert(metadata, entity, batches.get(0).getColumns(), batches.get(0).getValues(), batches.get(0).getSubQuery());
     
    	PreparedStatement stmt = prepareStatementAndSetParameters(serializer, withKeys);
     
    	// add first batch
    	stmt.addBatch();
     
    	// add other batches
    	for (int i = 1; i < batches.size(); i++) {
    		SQLInsertBatch batch = batches.get(i);
    		serializer = new SQLSerializer(configuration, true);
    		serializer.serializeInsert(metadata, entity, batch.getColumns(), batch.getValues(), batch.getSubQuery());
    		setParameters(stmt, serializer.getConstants(), serializer.getConstantPaths(), metadata.getParams());
    		stmt.addBatch();
    	}
    	return stmt;
    }

  • The above example code from QueryDSL-SQL works with mysql:mysql-connector-java:5.1.29. I.e. it returns the generated keys after batch INSERT.

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