Philippe Marschall created DERBY-6994:
-----------------------------------------

             Summary: Only last generated key in batch update is returned
                 Key: DERBY-6994
                 URL: https://issues.apache.org/jira/browse/DERBY-6994
             Project: Derby
          Issue Type: Bug
    Affects Versions: 10.14.1.0
            Reporter: Philippe Marschall
         Attachments: DerbyGeneratedKeysTest.java

When a batch update is executed only the generated keys of the last update are 
returned. Consider the following code.

{code}
    EmbeddedDataSource derbyDataSource = new EmbeddedDataSource();
    derbyDataSource.setDatabaseName("memory:test");
    derbyDataSource.setCreateDatabase("create");


    try (Connection connection = derbyDataSource.getConnection()) {

      try (Statement statement = connection.createStatement()) {
        statement.execute("CREATE TABLE test_table ("
                + "id INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START 
WITH 1, INCREMENT BY 1) PRIMARY KEY,"
                + "test_value INTEGER"
                + ")");
      }

      try (PreparedStatement preparedStatement = connection.prepareStatement(
              "INSERT INTO test_table(test_value) VALUES (?)",
              Statement.RETURN_GENERATED_KEYS)) {
        preparedStatement.setObject(1, Integer.valueOf(42));
        preparedStatement.addBatch();
        preparedStatement.setObject(1, Integer.valueOf(43));
        preparedStatement.addBatch();

        int[] updateCount = preparedStatement.executeBatch();
        assertArrayEquals(new int[] {1, 1}, updateCount, "update count");

        List<Integer> generatedIntegers = new ArrayList<>(2);
        try (ResultSet generatedKeys = preparedStatement.getGeneratedKeys()) {
          while (generatedKeys.next()) {
            generatedIntegers.add(generatedKeys.getObject(1, Integer.class));
          }
        }
        assertEquals(Arrays.asList(1, 2), generatedIntegers, "generated keys");
      }
    }
{code}

 

two updates are performed in a single batch. The first generates the key {{1}} 
and the second generates the key {{2}} but only the key {{2}} is returned.



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to