Skip to content

Commit a811fa5

Browse files
committed
Refactor pool creation in the driver
1 parent bf091c8 commit a811fa5

File tree

25 files changed

+320
-346
lines changed

25 files changed

+320
-346
lines changed

vertx-db2-client/src/main/java/io/vertx/db2client/DB2Pool.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import io.vertx.core.Future;
2222
import io.vertx.core.Handler;
2323
import io.vertx.core.Vertx;
24+
import io.vertx.db2client.impl.Db2PoolOptions;
2425
import io.vertx.db2client.spi.DB2Driver;
2526
import io.vertx.sqlclient.Pool;
2627
import io.vertx.sqlclient.PoolOptions;
@@ -105,7 +106,7 @@ static DB2Pool pool(List<DB2ConnectOptions> databases, PoolOptions options) {
105106
* {@link Vertx} instance.
106107
*/
107108
static DB2Pool pool(Vertx vertx, List<DB2ConnectOptions> databases, PoolOptions options) {
108-
return new DB2Driver().createPool(vertx, databases, options);
109+
return (DB2Pool) DB2Driver.INSTANCE.createPool(vertx, databases, options);
109110
}
110111

111112
/**
@@ -174,7 +175,7 @@ static SqlClient client(List<DB2ConnectOptions> databases, PoolOptions options)
174175
* {@link Vertx} instance.
175176
*/
176177
static SqlClient client(Vertx vertx, List<DB2ConnectOptions> databases, PoolOptions options) {
177-
return new DB2Driver().createClient(vertx, databases, options);
178+
return DB2Driver.INSTANCE.createPool(vertx, databases, new Db2PoolOptions(options).setPipelined(true));
178179
}
179180

180181
@Override

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

Lines changed: 2 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,12 @@
1717

1818
import io.vertx.core.Future;
1919
import io.vertx.core.Handler;
20-
import io.vertx.core.Vertx;
2120
import io.vertx.core.impl.CloseFuture;
2221
import io.vertx.core.impl.ContextInternal;
2322
import io.vertx.core.impl.VertxInternal;
2423
import io.vertx.core.spi.metrics.ClientMetrics;
25-
import io.vertx.core.spi.metrics.VertxMetrics;
2624
import io.vertx.db2client.DB2ConnectOptions;
2725
import io.vertx.db2client.DB2Pool;
28-
import io.vertx.db2client.spi.DB2Driver;
2926
import io.vertx.sqlclient.PoolOptions;
3027
import io.vertx.sqlclient.SqlConnectOptions;
3128
import io.vertx.sqlclient.SqlConnection;
@@ -35,51 +32,12 @@
3532
import io.vertx.sqlclient.impl.tracing.QueryTracer;
3633
import io.vertx.sqlclient.spi.ConnectionFactory;
3734

38-
import java.util.List;
3935
import java.util.function.Supplier;
40-
import java.util.stream.Collectors;
4136

4237
public class DB2PoolImpl extends PoolBase<DB2PoolImpl> implements DB2Pool {
4338

44-
public static DB2PoolImpl create(VertxInternal vertx, boolean pipelined, List<? extends SqlConnectOptions> servers, PoolOptions poolOptions) {
45-
DB2ConnectOptions baseConnectOptions = DB2ConnectOptions.wrap(servers.get(0));
46-
VertxInternal vx;
47-
if (vertx == null) {
48-
if (Vertx.currentContext() != null) {
49-
throw new IllegalStateException(
50-
"Running in a Vertx context => use DB2Pool#pool(Vertx, DB2ConnectOptions, PoolOptions) instead");
51-
}
52-
vx = (VertxInternal) Vertx.vertx();
53-
} else {
54-
vx = vertx;
55-
}
56-
QueryTracer tracer = vx.tracer() == null ? null : new QueryTracer(vx.tracer(), baseConnectOptions);
57-
VertxMetrics vertxMetrics = vx.metricsSPI();
58-
int pipeliningLimit = pipelined ? baseConnectOptions.getPipeliningLimit() : 1;
59-
ClientMetrics metrics = vertxMetrics != null ? vertxMetrics.createClientMetrics(baseConnectOptions.getSocketAddress(), "sql", baseConnectOptions.getMetricsName()) : null;
60-
DB2PoolImpl pool = new DB2PoolImpl(vx, pipeliningLimit, poolOptions, baseConnectOptions, null, tracer, metrics);
61-
pool.init();
62-
DB2Driver driver = new DB2Driver();
63-
List<ConnectionFactory> lst = servers.stream().map(options -> driver.createConnectionFactory(vx, options)).collect(Collectors.toList());
64-
ConnectionFactory factory = ConnectionFactory.roundRobinSelector(lst);
65-
pool.connectionProvider(factory::connect);
66-
CloseFuture closeFuture = pool.closeFuture();
67-
closeFuture.add(factory);
68-
if (vertx == null) {
69-
closeFuture.future().onComplete(ar -> vx.close());
70-
} else {
71-
ContextInternal ctx = vx.getContext();
72-
if (ctx != null) {
73-
ctx.addCloseHook(closeFuture);
74-
} else {
75-
vx.addCloseHook(closeFuture);
76-
}
77-
}
78-
return pool;
79-
}
80-
81-
private DB2PoolImpl(VertxInternal vertx, int pipeliningLimit, PoolOptions poolOptions, DB2ConnectOptions baseConnectOptions, Supplier<Future<SqlConnectOptions>> connectOptionsProvider, QueryTracer tracer, ClientMetrics metrics) {
82-
super(vertx, baseConnectOptions, connectOptionsProvider, tracer, metrics, pipeliningLimit, poolOptions);
39+
public DB2PoolImpl(VertxInternal vertx, int pipeliningLimit, PoolOptions poolOptions, DB2ConnectOptions baseConnectOptions, Supplier<Future<SqlConnectOptions>> connectOptionsProvider, QueryTracer tracer, ClientMetrics metrics, CloseFuture closeFuture) {
40+
super(vertx, baseConnectOptions, connectOptionsProvider, tracer, metrics, pipeliningLimit, poolOptions, closeFuture);
8341
}
8442

8543
@SuppressWarnings("rawtypes")
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright (C) 2017 Julien Viet
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
package io.vertx.db2client.impl;
18+
19+
import io.vertx.sqlclient.PoolOptions;
20+
21+
public class Db2PoolOptions extends PoolOptions {
22+
23+
public Db2PoolOptions(PoolOptions other) {
24+
super(other);
25+
}
26+
27+
private boolean pipelined;
28+
29+
public boolean isPipelined() {
30+
return pipelined;
31+
}
32+
33+
public Db2PoolOptions setPipelined(boolean pipelined) {
34+
this.pipelined = pipelined;
35+
return this;
36+
}
37+
}

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

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,44 @@
1616
package io.vertx.db2client.spi;
1717

1818
import io.vertx.core.Vertx;
19+
import io.vertx.core.impl.CloseFuture;
1920
import io.vertx.core.impl.VertxInternal;
21+
import io.vertx.core.spi.metrics.ClientMetrics;
22+
import io.vertx.core.spi.metrics.VertxMetrics;
2023
import io.vertx.db2client.DB2ConnectOptions;
2124
import io.vertx.db2client.DB2Pool;
2225
import io.vertx.db2client.impl.DB2ConnectionFactory;
2326
import io.vertx.db2client.impl.DB2PoolImpl;
27+
import io.vertx.db2client.impl.Db2PoolOptions;
2428
import io.vertx.sqlclient.PoolOptions;
2529
import io.vertx.sqlclient.SqlConnectOptions;
30+
import io.vertx.sqlclient.impl.tracing.QueryTracer;
2631
import io.vertx.sqlclient.spi.Driver;
2732
import io.vertx.sqlclient.spi.ConnectionFactory;
2833

2934
import java.util.List;
35+
import java.util.stream.Collectors;
3036

3137
public class DB2Driver implements Driver {
3238

33-
@Override
34-
public DB2Pool createPool(Vertx vertx, List<? extends SqlConnectOptions> databases, PoolOptions options) {
35-
return DB2PoolImpl.create((VertxInternal) vertx, false, databases, options);
36-
}
39+
public static final DB2Driver INSTANCE = new DB2Driver();
3740

38-
public DB2Pool createClient(Vertx vertx, List<? extends SqlConnectOptions> servers, PoolOptions options) {
39-
return DB2PoolImpl.create((VertxInternal) vertx, true, servers, options);
41+
@Override
42+
public DB2Pool newPool(Vertx vertx, List<? extends SqlConnectOptions> databases, PoolOptions options, CloseFuture closeFuture) {
43+
VertxInternal vx = (VertxInternal) vertx;
44+
DB2ConnectOptions baseConnectOptions = DB2ConnectOptions.wrap(databases.get(0));
45+
QueryTracer tracer = vx.tracer() == null ? null : new QueryTracer(vx.tracer(), baseConnectOptions);
46+
VertxMetrics vertxMetrics = vx.metricsSPI();
47+
boolean pipelinedPool = options instanceof Db2PoolOptions && ((Db2PoolOptions) options).isPipelined();
48+
int pipeliningLimit = pipelinedPool ? baseConnectOptions.getPipeliningLimit() : 1;
49+
ClientMetrics metrics = vertxMetrics != null ? vertxMetrics.createClientMetrics(baseConnectOptions.getSocketAddress(), "sql", baseConnectOptions.getMetricsName()) : null;
50+
DB2PoolImpl pool = new DB2PoolImpl(vx, pipeliningLimit, options, baseConnectOptions, null, tracer, metrics, closeFuture);
51+
pool.init();
52+
List<ConnectionFactory> lst = databases.stream().map(o -> createConnectionFactory(vertx, o)).collect(Collectors.toList());
53+
ConnectionFactory factory = ConnectionFactory.roundRobinSelector(lst);
54+
pool.connectionProvider(factory::connect);
55+
closeFuture.add(factory);
56+
return pool;
4057
}
4158

4259
@Override

vertx-mssql-client/src/main/java/io/vertx/mssqlclient/MSSQLPool.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ static MSSQLPool pool(List<MSSQLConnectOptions> databases, PoolOptions options)
9494
* Like {@link #pool(List, PoolOptions)} with a specific {@link Vertx} instance.
9595
*/
9696
static MSSQLPool pool(Vertx vertx, List<MSSQLConnectOptions> databases, PoolOptions options) {
97-
return new MSSQLDriver().createPool(vertx, databases, options);
97+
return (MSSQLPool) MSSQLDriver.INSTANCE.createPool(vertx, databases, options);
9898
}
9999

100100
@Override

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

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,12 @@
1313

1414
import io.vertx.core.Future;
1515
import io.vertx.core.Handler;
16-
import io.vertx.core.Vertx;
1716
import io.vertx.core.impl.CloseFuture;
1817
import io.vertx.core.impl.ContextInternal;
1918
import io.vertx.core.impl.VertxInternal;
2019
import io.vertx.core.spi.metrics.ClientMetrics;
21-
import io.vertx.core.spi.metrics.VertxMetrics;
2220
import io.vertx.mssqlclient.MSSQLConnectOptions;
2321
import io.vertx.mssqlclient.MSSQLPool;
24-
import io.vertx.mssqlclient.spi.MSSQLDriver;
2522
import io.vertx.sqlclient.PoolOptions;
2623
import io.vertx.sqlclient.SqlConnectOptions;
2724
import io.vertx.sqlclient.SqlConnection;
@@ -31,49 +28,12 @@
3128
import io.vertx.sqlclient.impl.tracing.QueryTracer;
3229
import io.vertx.sqlclient.spi.ConnectionFactory;
3330

34-
import java.util.List;
3531
import java.util.function.Supplier;
36-
import java.util.stream.Collectors;
3732

3833
public class MSSQLPoolImpl extends PoolBase<MSSQLPoolImpl> implements MSSQLPool {
3934

40-
public static MSSQLPoolImpl create(VertxInternal vertx, List<? extends SqlConnectOptions> servers, PoolOptions poolOptions) {
41-
MSSQLConnectOptions baseConnectOptions = MSSQLConnectOptions.wrap(servers.get(0));
42-
VertxInternal vx;
43-
if (vertx == null) {
44-
if (Vertx.currentContext() != null) {
45-
throw new IllegalStateException("Running in a Vertx context => use MSSQLPool#pool(Vertx, MSSQLConnectOptions, PoolOptions) instead");
46-
}
47-
vx = (VertxInternal) Vertx.vertx();
48-
} else {
49-
vx = vertx;
50-
}
51-
QueryTracer tracer = vx.tracer() == null ? null : new QueryTracer(vx.tracer(), baseConnectOptions);
52-
VertxMetrics vertxMetrics = vx.metricsSPI();
53-
ClientMetrics metrics = vertxMetrics != null ? vertxMetrics.createClientMetrics(baseConnectOptions.getSocketAddress(), "sql", baseConnectOptions.getMetricsName()) : null;
54-
MSSQLPoolImpl pool = new MSSQLPoolImpl(vx, baseConnectOptions, null, tracer, metrics, poolOptions);
55-
pool.init();
56-
MSSQLDriver driver = new MSSQLDriver();
57-
List<ConnectionFactory> lst = servers.stream().map(options -> driver.createConnectionFactory(vx, options)).collect(Collectors.toList());
58-
ConnectionFactory factory = ConnectionFactory.roundRobinSelector(lst);
59-
pool.connectionProvider(factory::connect);
60-
CloseFuture closeFuture = pool.closeFuture();
61-
closeFuture.add(factory);
62-
if (vertx == null) {
63-
closeFuture.future().onComplete(ar -> vx.close());
64-
} else {
65-
ContextInternal ctx = vx.getContext();
66-
if (ctx != null) {
67-
ctx.addCloseHook(closeFuture);
68-
} else {
69-
vx.addCloseHook(closeFuture);
70-
}
71-
}
72-
return pool;
73-
}
74-
75-
private MSSQLPoolImpl(VertxInternal vertx, MSSQLConnectOptions baseConnectOptions, Supplier<Future<SqlConnectOptions>> connectOptionsProvider, QueryTracer tracer, ClientMetrics metrics, PoolOptions poolOptions) {
76-
super(vertx, baseConnectOptions, connectOptionsProvider, tracer, metrics, 1, poolOptions);
35+
public MSSQLPoolImpl(VertxInternal vertx, MSSQLConnectOptions baseConnectOptions, Supplier<Future<SqlConnectOptions>> connectOptionsProvider, QueryTracer tracer, ClientMetrics metrics, PoolOptions poolOptions, CloseFuture closeFuture) {
36+
super(vertx, baseConnectOptions, connectOptionsProvider, tracer, metrics, 1, poolOptions, closeFuture);
7737
}
7838

7939
@Override

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

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,41 @@
1616
package io.vertx.mssqlclient.spi;
1717

1818
import io.vertx.core.Vertx;
19+
import io.vertx.core.impl.CloseFuture;
1920
import io.vertx.core.impl.VertxInternal;
21+
import io.vertx.core.spi.metrics.ClientMetrics;
22+
import io.vertx.core.spi.metrics.VertxMetrics;
2023
import io.vertx.mssqlclient.MSSQLConnectOptions;
2124
import io.vertx.mssqlclient.MSSQLPool;
2225
import io.vertx.mssqlclient.impl.MSSQLConnectionFactory;
2326
import io.vertx.mssqlclient.impl.MSSQLPoolImpl;
2427
import io.vertx.sqlclient.PoolOptions;
2528
import io.vertx.sqlclient.SqlConnectOptions;
29+
import io.vertx.sqlclient.impl.tracing.QueryTracer;
2630
import io.vertx.sqlclient.spi.Driver;
2731
import io.vertx.sqlclient.spi.ConnectionFactory;
2832

2933
import java.util.List;
34+
import java.util.stream.Collectors;
3035

3136
public class MSSQLDriver implements Driver {
3237

38+
public static final MSSQLDriver INSTANCE = new MSSQLDriver();
39+
3340
@Override
34-
public MSSQLPool createPool(Vertx vertx, List<? extends SqlConnectOptions> databases, PoolOptions options) {
35-
return MSSQLPoolImpl.create((VertxInternal) vertx, databases, options);
41+
public MSSQLPool newPool(Vertx vertx, List<? extends SqlConnectOptions> databases, PoolOptions options, CloseFuture closeFuture) {
42+
VertxInternal vx = (VertxInternal) vertx;
43+
MSSQLConnectOptions baseConnectOptions = MSSQLConnectOptions.wrap(databases.get(0));
44+
QueryTracer tracer = vx.tracer() == null ? null : new QueryTracer(vx.tracer(), baseConnectOptions);
45+
VertxMetrics vertxMetrics = vx.metricsSPI();
46+
ClientMetrics metrics = vertxMetrics != null ? vertxMetrics.createClientMetrics(baseConnectOptions.getSocketAddress(), "sql", baseConnectOptions.getMetricsName()) : null;
47+
MSSQLPoolImpl pool = new MSSQLPoolImpl(vx, baseConnectOptions, null, tracer, metrics, options, closeFuture);
48+
pool.init();
49+
List<ConnectionFactory> lst = databases.stream().map(o -> createConnectionFactory(vertx, o)).collect(Collectors.toList());
50+
ConnectionFactory factory = ConnectionFactory.roundRobinSelector(lst);
51+
pool.connectionProvider(factory::connect);
52+
closeFuture.add(factory);
53+
return pool;
3654
}
3755

3856
@Override

vertx-mysql-client/src/main/java/io/vertx/mysqlclient/MySQLPool.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ static MySQLPool pool(List<MySQLConnectOptions> databases, PoolOptions options)
9696
* Like {@link #pool(List, PoolOptions)} with a specific {@link Vertx} instance.
9797
*/
9898
static MySQLPool pool(Vertx vertx, List<MySQLConnectOptions> databases, PoolOptions options) {
99-
return new MySQLDriver().createPool(vertx, databases, options);
99+
return (MySQLPool) MySQLDriver.INSTANCE.createPool(vertx, databases, options);
100100
}
101101

102102
@Override

vertx-mysql-client/src/main/java/io/vertx/mysqlclient/impl/MySQLPoolImpl.java

Lines changed: 2 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,12 @@
1313

1414
import io.vertx.core.Future;
1515
import io.vertx.core.Handler;
16-
import io.vertx.core.Vertx;
17-
import io.vertx.core.VertxOptions;
1816
import io.vertx.core.impl.CloseFuture;
1917
import io.vertx.core.impl.ContextInternal;
2018
import io.vertx.core.impl.VertxInternal;
2119
import io.vertx.core.spi.metrics.ClientMetrics;
22-
import io.vertx.core.spi.metrics.VertxMetrics;
2320
import io.vertx.mysqlclient.MySQLConnectOptions;
2421
import io.vertx.mysqlclient.MySQLPool;
25-
import io.vertx.mysqlclient.spi.MySQLDriver;
2622
import io.vertx.sqlclient.PoolOptions;
2723
import io.vertx.sqlclient.SqlConnectOptions;
2824
import io.vertx.sqlclient.SqlConnection;
@@ -32,53 +28,12 @@
3228
import io.vertx.sqlclient.impl.tracing.QueryTracer;
3329
import io.vertx.sqlclient.spi.ConnectionFactory;
3430

35-
import java.util.List;
3631
import java.util.function.Supplier;
37-
import java.util.stream.Collectors;
3832

3933
public class MySQLPoolImpl extends PoolBase<MySQLPoolImpl> implements MySQLPool {
4034

41-
public static MySQLPoolImpl create(VertxInternal vertx, List<? extends SqlConnectOptions> servers, PoolOptions poolOptions) {
42-
MySQLConnectOptions baseConnectOptions = MySQLConnectOptions.wrap(servers.get(0));
43-
VertxInternal vx;
44-
if (vertx == null) {
45-
if (Vertx.currentContext() != null) {
46-
throw new IllegalStateException("Running in a Vertx context => use MySQLPool#pool(Vertx, MySQLConnectOptions, PoolOptions) instead");
47-
}
48-
VertxOptions vertxOptions = new VertxOptions();
49-
if (baseConnectOptions.isUsingDomainSocket()) {
50-
vertxOptions.setPreferNativeTransport(true);
51-
}
52-
vx = (VertxInternal) Vertx.vertx(vertxOptions);
53-
} else {
54-
vx = vertx;
55-
}
56-
QueryTracer tracer = vx.tracer() == null ? null : new QueryTracer(vx.tracer(), baseConnectOptions);
57-
VertxMetrics vertxMetrics = vx.metricsSPI();
58-
ClientMetrics metrics = vertxMetrics != null ? vertxMetrics.createClientMetrics(baseConnectOptions.getSocketAddress(), "sql", baseConnectOptions.getMetricsName()) : null;
59-
MySQLPoolImpl pool = new MySQLPoolImpl(vx, baseConnectOptions, null, tracer, metrics, poolOptions);
60-
pool.init();
61-
MySQLDriver driver = new MySQLDriver();
62-
List<ConnectionFactory> lst = servers.stream().map(options -> driver.createConnectionFactory(vx, options)).collect(Collectors.toList());
63-
ConnectionFactory factory = ConnectionFactory.roundRobinSelector(lst);
64-
pool.connectionProvider(factory::connect);
65-
CloseFuture closeFuture = pool.closeFuture();
66-
closeFuture.add(factory);
67-
if (vertx == null) {
68-
closeFuture.future().onComplete(ar -> vx.close());
69-
} else {
70-
ContextInternal ctx = vx.getContext();
71-
if (ctx != null) {
72-
ctx.addCloseHook(closeFuture);
73-
} else {
74-
vx.addCloseHook(closeFuture);
75-
}
76-
}
77-
return pool;
78-
}
79-
80-
private MySQLPoolImpl(VertxInternal vertx, MySQLConnectOptions baseConnectOptions, Supplier<Future<SqlConnectOptions>> connectOptionsProvider, QueryTracer tracer, ClientMetrics metrics, PoolOptions poolOptions) {
81-
super(vertx, baseConnectOptions, connectOptionsProvider, tracer, metrics, 1, poolOptions);
35+
public MySQLPoolImpl(VertxInternal vertx, MySQLConnectOptions baseConnectOptions, Supplier<Future<SqlConnectOptions>> connectOptionsProvider, QueryTracer tracer, ClientMetrics metrics, PoolOptions poolOptions, CloseFuture closeFuture) {
36+
super(vertx, baseConnectOptions, connectOptionsProvider, tracer, metrics, 1, poolOptions, closeFuture);
8237
}
8338

8439
@Override

0 commit comments

Comments
 (0)