简体   繁体   English

Vertx executeBatch 不返回所有行

[英]Vertx executeBatch not returning all rows

I am using vertx JDBC client pool and trying to insert multiple records to table.我正在使用 vertx JDBC 客户端池并尝试向表中插入多条记录。 The insertion is done successfully but the inserted records are not returned instead only first record is returned.插入成功,但插入的记录不返回,而只返回第一条记录。

Code to insert multiple record using batchExecute使用batchExecute插入多条记录的代码

List<Tuple> batch = new ArrayList<>();
batch.add(Tuple.of(36,"st","st"));
batch.add(Tuple.of(36,"st1","st1"));

pool.preparedQuery("INSERT INTO table (col1,col2,col3) VALUES($1, $2, $3) returning *").executeBatch(batch,rowSetAsyncResult -> {     
            System.out.println("size = " + rowSetAsyncResult.result().size()); // this always return 1
            for(Row row:rowSetAsyncResult.result()){
                System.out.println("id = " + row.getInteger("id"));
            }
        });

Output Output

size = 1
id = 87

Table has four columns and one of the column is auto-incremented, thats is why above code is having 3 columns.表有四列,其中一列是自动递增的,这就是为什么上面的代码有 3 列。

Am I missing anything here?我在这里错过了什么吗?

Try this:尝试这个:

pool.preparedQuery("INSERT INTO table (col1,col2,col3) VALUES($1, $2, $3) returning id").executeBatch(batch, res -> {
        if (res.succeeded()) { // Process rows
             RowSet<Row> rows = res.result();
             int[] count = new int[] {0};
             while (rows != null) {
                 count[0]++;
                 rows.iterator().forEachRemaining(row -> {
                    System.out.println("id: " + rows.getInteger("id"));});
                 rows = rows.next();
             }
        } else {
            System.out.println("Batch failed " + res.cause());
        }
    });

I know it is so late.我知道已经很晚了。 But I will answer it for any who faces the same issue.但我会为任何面临同样问题的人回答。 The code example below will fix the issue and get all rows.下面的代码示例将解决问题并获取所有行。

  client
  .preparedQuery("INSERT INTO color (color_name) VALUES ($1) RETURNING color_id")
  .executeBatch(Arrays.asList(Tuple.of("white"), Tuple.of("red"), Tuple.of("blue")))
  .onSuccess(res -> {
    for (RowSet<Row> rows = res;rows.next() != null;rows = rows.next()) {
      Integer colorId = rows.iterator().next().getInteger("color_id");
      System.out.println("generated key: " + colorId);
    }
  });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM