Skip to content

Commit a439953

Browse files
committed
Implement reference counted pools - fixes #1094
1 parent 71532e4 commit a439953

File tree

26 files changed

+498
-53
lines changed

26 files changed

+498
-53
lines changed

vertx-db2-client/src/main/asciidoc/index.adoc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@ The {@link io.vertx.db2client.DB2Pool} allows you to create a pool or a pooled c
109109
- pool operations are not pipelined, only connection client are pipelined
110110
- pooled client operations are pipelined
111111

112+
== Pool sharing
113+
114+
include::pool_sharing.adoc[]
115+
112116
== Configuration
113117

114118
There are several alternatives for you to configure the client.

vertx-db2-client/src/main/java/examples/SqlClientExamples.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import java.util.Arrays;
2020
import java.util.List;
2121

22+
import io.vertx.core.AbstractVerticle;
23+
import io.vertx.core.DeploymentOptions;
2224
import io.vertx.core.Vertx;
2325
import io.vertx.core.tracing.TracingPolicy;
2426
import io.vertx.db2client.DB2ConnectOptions;
@@ -357,4 +359,30 @@ public void poolConfig02(DB2Pool pool, String sql) {
357359
});
358360
});
359361
}
362+
363+
public void poolSharing1(Vertx vertx, DB2ConnectOptions database, int maxSize) {
364+
Pool pool = DB2Pool.pool(database, new PoolOptions().setMaxSize(maxSize));
365+
vertx.deployVerticle(() -> new AbstractVerticle() {
366+
@Override
367+
public void start() throws Exception {
368+
// Use the pool
369+
}
370+
}, new DeploymentOptions().setInstances(4));
371+
}
372+
373+
public void poolSharing2(Vertx vertx, DB2ConnectOptions database, int maxSize) {
374+
vertx.deployVerticle(() -> new AbstractVerticle() {
375+
Pool pool;
376+
@Override
377+
public void start() {
378+
// Get or create a shared pool
379+
// this actually creates a lease to the pool
380+
// when the verticle is undeployed, the lease will be released automaticaly
381+
pool = DB2Pool.pool(database, new PoolOptions()
382+
.setMaxSize(maxSize)
383+
.setShared(true)
384+
.setName("my-pool"));
385+
}
386+
}, new DeploymentOptions().setInstances(4));
387+
}
360388
}

vertx-db2-client/src/main/java/io/vertx/db2client/impl/DB2PoolImpl.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@
1515
*/
1616
package io.vertx.db2client.impl;
1717

18+
import io.vertx.core.impl.CloseFuture;
19+
import io.vertx.core.impl.VertxInternal;
1820
import io.vertx.db2client.DB2Pool;
1921
import io.vertx.sqlclient.Pool;
2022
import io.vertx.sqlclient.impl.PoolBase;
2123

2224
public class DB2PoolImpl extends PoolBase<DB2PoolImpl> implements DB2Pool {
2325

24-
public DB2PoolImpl(Pool delegate) {
25-
super(delegate);
26+
public DB2PoolImpl(VertxInternal vertx, CloseFuture closeFuture, Pool delegate) {
27+
super(vertx, closeFuture, delegate);
2628
}
2729
}

vertx-db2-client/src/main/java/io/vertx/db2client/spi/DB2Driver.java

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,24 +41,36 @@
4141

4242
public class DB2Driver implements Driver {
4343

44+
private static final String SHARED_CLIENT_KEY = "__vertx.shared.db2client";
45+
4446
public static final DB2Driver INSTANCE = new DB2Driver();
4547

4648
@Override
4749
public DB2Pool newPool(Vertx vertx, List<? extends SqlConnectOptions> databases, PoolOptions options, CloseFuture closeFuture) {
4850
VertxInternal vx = (VertxInternal) vertx;
51+
PoolImpl pool;
52+
if (options.isShared()) {
53+
pool = vx.createSharedClient(SHARED_CLIENT_KEY, options.getName(), closeFuture, cf -> newPoolImpl(vx, databases, options, cf));
54+
} else {
55+
pool = newPoolImpl(vx, databases, options, closeFuture);
56+
}
57+
return new DB2PoolImpl(vx, closeFuture, pool);
58+
}
59+
60+
private PoolImpl newPoolImpl(VertxInternal vertx, List<? extends SqlConnectOptions> databases, PoolOptions options, CloseFuture closeFuture) {
4961
DB2ConnectOptions baseConnectOptions = DB2ConnectOptions.wrap(databases.get(0));
50-
QueryTracer tracer = vx.tracer() == null ? null : new QueryTracer(vx.tracer(), baseConnectOptions);
51-
VertxMetrics vertxMetrics = vx.metricsSPI();
62+
QueryTracer tracer = vertx.tracer() == null ? null : new QueryTracer(vertx.tracer(), baseConnectOptions);
63+
VertxMetrics vertxMetrics = vertx.metricsSPI();
64+
ClientMetrics metrics = vertxMetrics != null ? vertxMetrics.createClientMetrics(baseConnectOptions.getSocketAddress(), "sql", baseConnectOptions.getMetricsName()) : null;
5265
boolean pipelinedPool = options instanceof Db2PoolOptions && ((Db2PoolOptions) options).isPipelined();
5366
int pipeliningLimit = pipelinedPool ? baseConnectOptions.getPipeliningLimit() : 1;
54-
ClientMetrics metrics = vertxMetrics != null ? vertxMetrics.createClientMetrics(baseConnectOptions.getSocketAddress(), "sql", baseConnectOptions.getMetricsName()) : null;
55-
PoolImpl pool = new PoolImpl(vx, this, baseConnectOptions, null, tracer, metrics, pipeliningLimit, options, closeFuture);
56-
pool.init();
67+
PoolImpl pool = new PoolImpl(vertx, this, baseConnectOptions, null, tracer, metrics, pipeliningLimit, options, closeFuture);
5768
List<ConnectionFactory> lst = databases.stream().map(o -> createConnectionFactory(vertx, o)).collect(Collectors.toList());
5869
ConnectionFactory factory = ConnectionFactory.roundRobinSelector(lst);
5970
pool.connectionProvider(factory::connect);
71+
pool.init();
6072
closeFuture.add(factory);
61-
return new DB2PoolImpl(pool);
73+
return pool;
6274
}
6375

6476
@Override

vertx-mssql-client/src/main/asciidoc/index.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,11 @@ For example, {@link io.vertx.core.net.PemTrustOptions} can be used if a PEM file
279279

280280
For further details about SSL support in Vert.x, please refer to the https://vertx.io/docs/vertx-core/java/#ssl[Vert.x Core documentation].
281281

282+
== Pool sharing
283+
284+
include::pool_sharing.adoc[]
285+
282286
== Advanced pool configuration
283287

284288
include::pool_config.adoc[]
289+

vertx-mssql-client/src/main/java/examples/SqlClientExamples.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
*/
1717
package examples;
1818

19+
import io.vertx.core.AbstractVerticle;
20+
import io.vertx.core.DeploymentOptions;
1921
import io.vertx.core.Vertx;
2022
import io.vertx.core.tracing.TracingPolicy;
2123
import io.vertx.docgen.Source;
@@ -356,4 +358,30 @@ public void poolConfig02(MSSQLPool pool, String sql) {
356358
});
357359
});
358360
}
361+
362+
public void poolSharing1(Vertx vertx, MSSQLConnectOptions database, int maxSize) {
363+
Pool pool = MSSQLPool.pool(database, new PoolOptions().setMaxSize(maxSize));
364+
vertx.deployVerticle(() -> new AbstractVerticle() {
365+
@Override
366+
public void start() throws Exception {
367+
// Use the pool
368+
}
369+
}, new DeploymentOptions().setInstances(4));
370+
}
371+
372+
public void poolSharing2(Vertx vertx, MSSQLConnectOptions database, int maxSize) {
373+
vertx.deployVerticle(() -> new AbstractVerticle() {
374+
Pool pool;
375+
@Override
376+
public void start() {
377+
// Get or create a shared pool
378+
// this actually creates a lease to the pool
379+
// when the verticle is undeployed, the lease will be released automaticaly
380+
pool = MSSQLPool.pool(database, new PoolOptions()
381+
.setMaxSize(maxSize)
382+
.setShared(true)
383+
.setName("my-pool"));
384+
}
385+
}, new DeploymentOptions().setInstances(4));
386+
}
359387
}

vertx-mssql-client/src/main/java/io/vertx/mssqlclient/impl/MSSQLPoolImpl.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@
1111

1212
package io.vertx.mssqlclient.impl;
1313

14+
import io.vertx.core.impl.CloseFuture;
15+
import io.vertx.core.impl.VertxInternal;
1416
import io.vertx.mssqlclient.MSSQLPool;
1517
import io.vertx.sqlclient.Pool;
1618
import io.vertx.sqlclient.impl.PoolBase;
1719

1820
public class MSSQLPoolImpl extends PoolBase<MSSQLPoolImpl> implements MSSQLPool {
1921

20-
public MSSQLPoolImpl(Pool delegate) {
21-
super(delegate);
22+
public MSSQLPoolImpl(VertxInternal vertx, CloseFuture closeFuture, Pool delegate) {
23+
super(vertx, closeFuture, delegate);
2224
}
2325
}

vertx-mssql-client/src/main/java/io/vertx/mssqlclient/spi/MSSQLDriver.java

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,22 +40,34 @@
4040

4141
public class MSSQLDriver implements Driver {
4242

43+
private static final String SHARED_CLIENT_KEY = "__vertx.shared.mssqlclient";
44+
4345
public static final MSSQLDriver INSTANCE = new MSSQLDriver();
4446

4547
@Override
4648
public MSSQLPool newPool(Vertx vertx, List<? extends SqlConnectOptions> databases, PoolOptions options, CloseFuture closeFuture) {
4749
VertxInternal vx = (VertxInternal) vertx;
50+
PoolImpl pool;
51+
if (options.isShared()) {
52+
pool = vx.createSharedClient(SHARED_CLIENT_KEY, options.getName(), closeFuture, cf -> newPoolImpl(vx, databases, options, cf));
53+
} else {
54+
pool = newPoolImpl(vx, databases, options, closeFuture);
55+
}
56+
return new MSSQLPoolImpl(vx, closeFuture, pool);
57+
}
58+
59+
private PoolImpl newPoolImpl(VertxInternal vertx, List<? extends SqlConnectOptions> databases, PoolOptions options, CloseFuture closeFuture) {
4860
MSSQLConnectOptions baseConnectOptions = MSSQLConnectOptions.wrap(databases.get(0));
49-
QueryTracer tracer = vx.tracer() == null ? null : new QueryTracer(vx.tracer(), baseConnectOptions);
50-
VertxMetrics vertxMetrics = vx.metricsSPI();
61+
QueryTracer tracer = vertx.tracer() == null ? null : new QueryTracer(vertx.tracer(), baseConnectOptions);
62+
VertxMetrics vertxMetrics = vertx.metricsSPI();
5163
ClientMetrics metrics = vertxMetrics != null ? vertxMetrics.createClientMetrics(baseConnectOptions.getSocketAddress(), "sql", baseConnectOptions.getMetricsName()) : null;
52-
PoolImpl pool = new PoolImpl(vx, this, baseConnectOptions, null, tracer, metrics, 1, options, closeFuture);
53-
pool.init();
64+
PoolImpl pool = new PoolImpl(vertx, this, baseConnectOptions, null, tracer, metrics, 1, options, closeFuture);
5465
List<ConnectionFactory> lst = databases.stream().map(o -> createConnectionFactory(vertx, o)).collect(Collectors.toList());
5566
ConnectionFactory factory = ConnectionFactory.roundRobinSelector(lst);
5667
pool.connectionProvider(factory::connect);
68+
pool.init();
5769
closeFuture.add(factory);
58-
return new MSSQLPoolImpl(pool);
70+
return pool;
5971
}
6072

6173
@Override

vertx-mysql-client/src/main/asciidoc/index.adoc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@ You can easily get one from the pool:
9292

9393
Once you are done with the connection you must close it to release it to the pool, so it can be reused.
9494

95+
== Pool sharing
96+
97+
include::pool_sharing.adoc[]
98+
9599
=== Unix Domain Socket
96100

97101
Sometimes for simplicity, security or performance reasons, it is required to connect via a https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_socket[Unix Domain Socket].

vertx-mysql-client/src/main/java/examples/SqlClientExamples.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
*/
1717
package examples;
1818

19+
import io.vertx.core.AbstractVerticle;
20+
import io.vertx.core.DeploymentOptions;
1921
import io.vertx.core.Vertx;
2022
import io.vertx.core.tracing.TracingPolicy;
2123
import io.vertx.docgen.Source;
@@ -341,4 +343,30 @@ public void poolConfig02(MySQLPool pool, String sql) {
341343
});
342344
});
343345
}
346+
347+
public void poolSharing1(Vertx vertx, MySQLConnectOptions database, int maxSize) {
348+
Pool pool = MySQLPool.pool(database, new PoolOptions().setMaxSize(maxSize));
349+
vertx.deployVerticle(() -> new AbstractVerticle() {
350+
@Override
351+
public void start() throws Exception {
352+
// Use the pool
353+
}
354+
}, new DeploymentOptions().setInstances(4));
355+
}
356+
357+
public void poolSharing2(Vertx vertx, MySQLConnectOptions database, int maxSize) {
358+
vertx.deployVerticle(() -> new AbstractVerticle() {
359+
Pool pool;
360+
@Override
361+
public void start() {
362+
// Get or create a shared pool
363+
// this actually creates a lease to the pool
364+
// when the verticle is undeployed, the lease will be released automaticaly
365+
pool = MySQLPool.pool(database, new PoolOptions()
366+
.setMaxSize(maxSize)
367+
.setShared(true)
368+
.setName("my-pool"));
369+
}
370+
}, new DeploymentOptions().setInstances(4));
371+
}
344372
}

0 commit comments

Comments
 (0)