0

I try to perform a batch insert using r2dbc.

I have see that with DatabaseClient from spring boot, it's not yet possible. I have try to do that using R2DBC SPI Statement and the and method, like that:

Mono.from(this.factory.create())
            .map(connection -> connection.createStatement(insertSQL))
            .map(statement -> {
                lines.forEach(line -> {
                    statement
                        .add()
                        .bind(0, line.getId())
                        ;
                });
                return statement;
            })
            .flatMap(statement -> Mono.from(statement.execute()));

I have see on the log that two insert request are done.

2019-12-18 16:27:36.092 DEBUG [] 4133 --- [tor-tcp-epoll-1] io.r2dbc.postgresql.QUERY                : Executing query: insert into table(id) values ($1)
2019-12-18 16:27:36.116 DEBUG [] 4133 --- [tor-tcp-epoll-1] i.r.p.client.ReactorNettyClient          : Request:  Bind{}
2019-12-18 16:27:36.126 DEBUG [] 4133 --- [tor-tcp-epoll-1] io.r2dbc.postgresql.QUERY                : Executing query: insert into table(id) values ($1)
2019-12-18 16:27:36.130 DEBUG [] 4133 --- [tor-tcp-epoll-1] i.r.p.client.ReactorNettyClient          : Request:  Bind{}

Is add perform a batch update or just run two requests?

Thanks.

deblockt
  • 91
  • 5

1 Answers1

0

A little bit late, but better than never I guess. I would say that your code issue two separate requests. Using DatabaseClient, which I guess have matured since the question was asked:

    Mono.from(connectionFactory.create())
        .map(connection -> connection.createBatch())
        .map(batch -> {
            batch.add("delete from table1");
            batch.add("delete from table2");
            batch.add("delete from table3");
            return batch.execute();
        })
        // Etc

When running the above, the following is logged:
Query:["delete from table1","delete from table2","delete from table3"] Bindings:[]

  • I am trying your solution and after batch.execute my next step is .map(r -> r.getRowsUpdated()) which I expect to return delete count for postgress db but its not returning this result. Any Idea ? – Kiran Bhagwat Feb 10 '21 at 17:18