【问题标题】:Vertx executeBatch not returning all rowsVertx executeBatch 不返回所有行
【发布时间】:2021-05-19 08:55:39
【问题描述】:

我正在使用 vertx JDBC 客户端池并尝试向表中插入多条记录。插入成功,但插入的记录不返回,只返回第一条记录。

使用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"));
            }
        });

输出

size = 1
id = 87

表格有四列,其中一列是自动递增的,这就是上面代码有 3 列的原因。

我这里有什么遗漏吗?

【问题讨论】:

  • 我只是想向 vert.x 报告类似的问题 - 完全异步 pgpool(无 jdbc)似乎也只返回一条记录。

标签: java asynchronous vert.x vertx-verticle vertx-eventbus


【解决方案1】:

试试这个:

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());
        }
    });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-03-27
    • 1970-01-01
    • 2021-07-10
    • 2016-02-20
    • 2016-09-23
    • 2014-01-16
    • 1970-01-01
    • 2014-09-02
    相关资源
    最近更新 更多