Skip to content

Commit d5f67f5

Browse files
committed
Lazily prepare statements to reduce connection time.
Signed-off-by: Santiago Pericas-Geertsen <[email protected]>
1 parent 2717c6d commit d5f67f5

File tree

1 file changed

+18
-16
lines changed

1 file changed

+18
-16
lines changed

frameworks/Java/helidon/nima/src/main/java/io/helidon/benchmark/nima/models/PgClientConnection.java

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11

22
package io.helidon.benchmark.nima.models;
33

4+
import java.util.concurrent.CompletableFuture;
5+
import java.util.concurrent.ExecutionException;
6+
47
import io.vertx.pgclient.PgConnection;
58
import io.vertx.sqlclient.PreparedQuery;
9+
import io.vertx.sqlclient.PreparedStatement;
610
import io.vertx.sqlclient.Row;
711
import io.vertx.sqlclient.RowSet;
812

@@ -11,9 +15,9 @@ public class PgClientConnection implements AutoCloseable {
1115
private static String SELECT_WORLD = "SELECT id, randomnumber from WORLD where id=$1";
1216
private static String SELECT_FORTUNE = "SELECT * from FORTUNE";
1317

14-
private PreparedQuery<RowSet<Row>> worldQuery;
15-
private PreparedQuery<RowSet<Row>> fortuneQuery;
16-
private PreparedQuery<RowSet<Row>>[] updateQuery;
18+
private CompletableFuture<PreparedStatement> worldQuery;
19+
private CompletableFuture<PreparedStatement> fortuneQuery;
20+
private CompletableFuture<PreparedStatement>[] updateQuery;
1721

1822
private final PgConnection conn;
1923

@@ -30,30 +34,28 @@ public void close() {
3034
conn.close();
3135
}
3236

33-
public PreparedQuery<RowSet<Row>> worldQuery() {
34-
return worldQuery;
37+
public PreparedQuery<RowSet<Row>> worldQuery() throws ExecutionException, InterruptedException {
38+
return worldQuery.get().query();
3539
}
3640

37-
public PreparedQuery<RowSet<Row>> fortuneQuery() {
38-
return fortuneQuery;
41+
public PreparedQuery<RowSet<Row>> fortuneQuery() throws ExecutionException, InterruptedException {
42+
return fortuneQuery.get().query();
3943
}
4044

41-
public PreparedQuery<RowSet<Row>> updateQuery(int queryCount) {
42-
return updateQuery[queryCount - 1];
45+
public PreparedQuery<RowSet<Row>> updateQuery(int queryCount) throws ExecutionException, InterruptedException {
46+
return updateQuery[queryCount - 1].get().query();
4347
}
4448

4549
@SuppressWarnings("unchecked")
4650
void prepare() {
4751
try {
48-
worldQuery = conn.prepare(SELECT_WORLD)
49-
.toCompletionStage().toCompletableFuture().get().query();
50-
fortuneQuery = conn.prepare(SELECT_FORTUNE)
51-
.toCompletionStage().toCompletableFuture().get().query();
52-
updateQuery = (PreparedQuery<RowSet<Row>>[]) new PreparedQuery<?>[UPDATE_QUERIES];
52+
worldQuery = conn.prepare(SELECT_WORLD).toCompletionStage().toCompletableFuture();
53+
fortuneQuery = conn.prepare(SELECT_FORTUNE).toCompletionStage().toCompletableFuture();
54+
updateQuery = (CompletableFuture<PreparedStatement>[]) new CompletableFuture<?>[UPDATE_QUERIES];
5355
for (int i = 0; i < UPDATE_QUERIES; i++) {
5456
updateQuery[i] = conn.prepare(singleUpdate(i + 1))
55-
.toCompletionStage().toCompletableFuture().get().query();
56-
}
57+
.toCompletionStage().toCompletableFuture();
58+
}
5759
} catch (Exception e) {
5860
throw new RuntimeException(e);
5961
}

0 commit comments

Comments
 (0)