Home » Java » java – Vert.x data access benefits over CompletableFuture.supplyAsync with JDBC-Exceptionshub

java – Vert.x data access benefits over CompletableFuture.supplyAsync with JDBC-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment


What are the benefits of using a vert.x data access library for connecting to a database (for example Reactive PostgreSQL client) over CompletableFuture.supplyAsync with JDBC in terms of thread management.

Let’s assume that we have a web application.
In both cases the request thread would not be blocked while waiting for the database, so then why would someone use the vert.x driver over the official jdbc?

For example in JDBC:

String url = "jdbc:postgresql://localhost/test";
Properties props = new Properties();

CompletableFuture.supplyAsync(() -> {
    Connection conn = DriverManager.getConnection(url, props);

    Statement st = conn.createStatement();
    ResultSet rs = st.executeQuery("SELECT * FROM mytable WHERE columnfoo = 500");
    while (rs.next())
        System.out.print("Column 1 returned ");

In vert.x

PgConnectOptions connectOptions = new PgConnectOptions()

// Pool options
PoolOptions poolOptions = new PoolOptions()

// Create the client pool
PgPool client = PgPool.pool(connectOptions, poolOptions);

// A simple query
client.query("SELECT * FROM users WHERE id='julien'", ar -> {
  if (ar.succeeded()) {
    RowSet<Row> result = ar.result();
    System.out.println("Got " + result.size() + " rows ");
  } else {
    System.out.println("Failure: " + ar.cause().getMessage());

  // Now close the pool
How to&Answers:

Indeed, in both cases, the caller thread will not be blocked.

But in the first case, the task is handed over to a thread from a pool (in your example, the JDK’s internal pool) while in the second case, the Reactive Pg Client operates asynchronously and invokes the provided callback when the results are ready.

Threads comes with a cost (memory, context switches). So if your applications need to handle a lot of requests with lower resources, the Reactive Pg Client can be a better option.