Parallel requests within the same transaction

240 Views Asked by At

I would like to execute several SQL statements in parallel and within the same transaction in order to get work around a high latency to the database server (SQL Server).

From my research, it seems like R2DBC would be my best bet for accomplishing this. However, when I experiment it seems like:

  • All statements that are to execute within the same transaction must go via the same connection object.
  • All statements using the same connection object are serially executed.

Is this true? It makes perfect sense that the database server would force statements within the same transaction to execute serially, but I would have imagined that this serialization would take place on the server side and that the driver would have allowed several in flights requests at the same time.

If this is not true I would like some help figuring out what I am doing wrong. This is how I have set up my connection pool:

return new MssqlConnectionFactory(MssqlConnectionConfiguration.builder()
                .host("localhost")
                .port(port)
                .database(db)
                .username(user)
                .password(password)
                .build());

And this is my test code:

var connection = pool.create().block();
Mono.from(connection.setAutoCommit(false)).block();
Scheduler scheduler = Schedulers.newParallel("foo",  10);
for (int i = 0; i < 10; i++) {
    final int j = i;
    Mono.just(connection)
        .subscribeOn(scheduler)
        .flatMap(conn -> Mono.from(conn.createStatement("select " + j).execute()))
        .flatMap(result -> Mono.from(result.map((row, meta) -> row.get(0, Integer.class))))
        .doOnNext(System.out::println)
        .subscribe();
}

Depending on the parallelism value of the scheduler I may get an error that the request queue is full. If I fetch a new connection object for every request instead of reusing the same one everything executes in parallel but as far as I can tell there's no way to get them to execute within the same transaction then.

I am using toxiproxy to simulate extra latency to the sql server instance.

0

There are 0 best solutions below