Skip to content

Commit cc9ef0e

Browse files
committed
Lazy connections.
Signed-off-by: Santiago Pericas-Geertsen <[email protected]>
1 parent 249c882 commit cc9ef0e

File tree

2 files changed

+23
-13
lines changed

2 files changed

+23
-13
lines changed

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

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

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

4+
import java.util.concurrent.locks.ReentrantLock;
5+
46
import io.vertx.core.Vertx;
57
import io.vertx.pgclient.PgConnectOptions;
68
import io.vertx.pgclient.PgConnection;
@@ -14,6 +16,7 @@ class PgClientConnectionPool implements AutoCloseable {
1416
private final Vertx vertx;
1517
private final PgConnectOptions options;
1618
private final PgClientConnection[] connections;
19+
private final ReentrantLock lock = new ReentrantLock();
1720

1821
public PgClientConnectionPool(Vertx vertx, int size, PgConnectOptions options) {
1922
this.size = size;
@@ -24,29 +27,37 @@ public PgClientConnectionPool(Vertx vertx, int size, PgConnectOptions options) {
2427

2528
public PgClientConnection clientConnection() {
2629
int bucket = Thread.currentThread().hashCode() % size;
30+
if (connections[bucket] == null) {
31+
try {
32+
lock.lock();
33+
if (connections[bucket] == null) {
34+
connect(bucket);
35+
}
36+
} finally {
37+
lock.unlock();
38+
}
39+
}
2740
return connections[bucket];
2841
}
2942

30-
public void connect() {
43+
@Override
44+
public void close() {
3145
try {
32-
for (int i = 0; i < size; i++) {
33-
PgConnection conn = PgConnection.connect(vertx, options)
34-
.toCompletionStage().toCompletableFuture().get();
35-
PgClientConnection clientConn = new PgClientConnection(conn);
36-
clientConn.prepare();
37-
connections[i] = clientConn;
46+
for (PgClientConnection connection : connections) {
47+
connection.close();
3848
}
3949
} catch (Exception e) {
4050
throw new RuntimeException(e);
4151
}
4252
}
4353

44-
@Override
45-
public void close() {
54+
private void connect(int bucket) {
4655
try {
47-
for (PgClientConnection connection : connections) {
48-
connection.close();
49-
}
56+
PgConnection conn = PgConnection.connect(vertx, options)
57+
.toCompletionStage().toCompletableFuture().get();
58+
PgClientConnection clientConn = new PgClientConnection(conn);
59+
clientConn.prepare();
60+
connections[bucket] = clientConn;
5061
} catch (Exception e) {
5162
throw new RuntimeException(e);
5263
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ public PgClientRepository(Config config) {
3434
.setPipeliningLimit(100000);
3535
int sqlPoolSize = config.get("sql-pool-size").asInt().orElse(Runtime.getRuntime().availableProcessors());
3636
connectionPool = new PgClientConnectionPool(vertx, sqlPoolSize, connectOptions);
37-
connectionPool.connect();
3837
}
3938

4039
@Override

0 commit comments

Comments
 (0)