vertx-sql-client icon indicating copy to clipboard operation
vertx-sql-client copied to clipboard

PreparedStatement should be closed when using RowStream

Open adrien-aubert-drovio opened this issue 3 years ago • 0 comments

Version

Vertx: 4.1.5 Postgresql: 9, 10 or 11 Java 11

Context

When using the row stream according to the documentation (https://vertx.io/docs/vertx-pg-client/java/#_cursors_and_streaming), a memory leak appears.

After investigations, the PreparedStatement is never closed (By the way, the stream is manually closed in the end handler but it seems that is is automatically closed before calling the endHandler).

PreparedStatement pq = ar0.result();

    // Streams require to run within a transaction
    connection.begin(ar1 -> {
      if (ar1.succeeded()) {
        Transaction tx = ar1.result();

        // Fetch 50 rows at a time
        RowStream<Row> stream = pq.createStream(50, Tuple.of("julien"));

        // Use the stream
        stream.exceptionHandler(err -> {
          System.out.println("Error: " + err.getMessage());
        });
        stream.endHandler(v -> {
          // Close the stream to release the resources in the database
          stream.close(closed -> {
            tx.commit(committed -> {
              System.out.println("End of stream");
            });
          });
        });
        stream.handler(row -> {
          System.out.println("User: " + row.getString("last_name"));
        });
      }
    });

Meanwhile, on another place of the documentation (https://vertx.io/docs/vertx-pg-client/java/#_prepared_queries), the PreparedStatement is closed.

PreparedStatement preparedStatement = ar.result();
      preparedStatement.query()
        .execute(Tuple.of("julien"), ar2 -> {
          if (ar2.succeeded()) {
            RowSet<Row> rows = ar2.result();
            System.out.println("Got " + rows.size() + " rows ");
            preparedStatement.close();
          } else {
            System.out.println("Failure: " + ar2.cause().getMessage());
          }
        });

Do you have a reproducer?

I can give you a reproducer using JUnit and htop to check the memory, but it is mainly a documentation fix.

adrien-aubert-drovio avatar Feb 11 '22 10:02 adrien-aubert-drovio