Skip to content

Commit c3a7539

Browse files
committed
Reimplement pool creation from List by a Supplier
1 parent 7ae11b0 commit c3a7539

File tree

8 files changed

+8
-188
lines changed

8 files changed

+8
-188
lines changed

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

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import io.vertx.core.spi.metrics.ClientMetrics;
2424
import io.vertx.core.spi.metrics.VertxMetrics;
2525
import io.vertx.db2client.DB2ConnectOptions;
26-
import io.vertx.db2client.DB2Pool;
2726
import io.vertx.db2client.impl.*;
2827
import io.vertx.sqlclient.Pool;
2928
import io.vertx.sqlclient.PoolOptions;
@@ -35,9 +34,7 @@
3534
import io.vertx.sqlclient.spi.ConnectionFactory;
3635
import io.vertx.sqlclient.spi.Driver;
3736

38-
import java.util.List;
3937
import java.util.function.Supplier;
40-
import java.util.stream.Collectors;
4138

4239
public class DB2Driver implements Driver {
4340

@@ -72,34 +69,6 @@ private PoolImpl newPoolImpl(VertxInternal vertx, Supplier<? extends SqlConnectO
7269
return pool;
7370
}
7471

75-
@Override
76-
public DB2Pool newPool(Vertx vertx, List<? extends SqlConnectOptions> databases, PoolOptions options, CloseFuture closeFuture) {
77-
VertxInternal vx = (VertxInternal) vertx;
78-
PoolImpl pool;
79-
if (options.isShared()) {
80-
pool = vx.createSharedClient(SHARED_CLIENT_KEY, options.getName(), closeFuture, cf -> newPoolImpl(vx, databases, options, cf));
81-
} else {
82-
pool = newPoolImpl(vx, databases, options, closeFuture);
83-
}
84-
return new DB2PoolImpl(vx, closeFuture, pool);
85-
}
86-
87-
private PoolImpl newPoolImpl(VertxInternal vertx, List<? extends SqlConnectOptions> databases, PoolOptions options, CloseFuture closeFuture) {
88-
DB2ConnectOptions baseConnectOptions = DB2ConnectOptions.wrap(databases.get(0));
89-
QueryTracer tracer = vertx.tracer() == null ? null : new QueryTracer(vertx.tracer(), baseConnectOptions);
90-
VertxMetrics vertxMetrics = vertx.metricsSPI();
91-
ClientMetrics metrics = vertxMetrics != null ? vertxMetrics.createClientMetrics(baseConnectOptions.getSocketAddress(), "sql", baseConnectOptions.getMetricsName()) : null;
92-
boolean pipelinedPool = options instanceof Db2PoolOptions && ((Db2PoolOptions) options).isPipelined();
93-
int pipeliningLimit = pipelinedPool ? baseConnectOptions.getPipeliningLimit() : 1;
94-
PoolImpl pool = new PoolImpl(vertx, this, tracer, metrics, pipeliningLimit, options, null, null, closeFuture);
95-
List<ConnectionFactory> lst = databases.stream().map(o -> createConnectionFactory(vertx, o)).collect(Collectors.toList());
96-
ConnectionFactory factory = ConnectionFactory.roundRobinSelector(lst);
97-
pool.connectionProvider(factory::connect);
98-
pool.init();
99-
closeFuture.add(factory);
100-
return pool;
101-
}
102-
10372
@Override
10473
public DB2ConnectOptions parseConnectionUri(String uri) {
10574
JsonObject conf = DB2ConnectionUriParser.parse(uri, false);

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

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import io.vertx.core.spi.metrics.ClientMetrics;
2424
import io.vertx.core.spi.metrics.VertxMetrics;
2525
import io.vertx.mssqlclient.MSSQLConnectOptions;
26-
import io.vertx.mssqlclient.MSSQLPool;
2726
import io.vertx.mssqlclient.impl.MSSQLConnectionFactory;
2827
import io.vertx.mssqlclient.impl.MSSQLConnectionImpl;
2928
import io.vertx.mssqlclient.impl.MSSQLConnectionUriParser;
@@ -38,9 +37,7 @@
3837
import io.vertx.sqlclient.spi.ConnectionFactory;
3938
import io.vertx.sqlclient.spi.Driver;
4039

41-
import java.util.List;
4240
import java.util.function.Supplier;
43-
import java.util.stream.Collectors;
4441

4542
public class MSSQLDriver implements Driver {
4643

@@ -50,24 +47,6 @@ public class MSSQLDriver implements Driver {
5047

5148
@Override
5249
public Pool newPool(Vertx vertx, Supplier<? extends SqlConnectOptions> databases, PoolOptions options, CloseFuture closeFuture) {
53-
return null;
54-
}
55-
56-
private PoolImpl newPoolImpl(VertxInternal vertx, Supplier<? extends SqlConnectOptions> databases, PoolOptions options, CloseFuture closeFuture) {
57-
MSSQLConnectOptions baseConnectOptions = MSSQLConnectOptions.wrap(databases.get());
58-
QueryTracer tracer = vertx.tracer() == null ? null : new QueryTracer(vertx.tracer(), baseConnectOptions);
59-
VertxMetrics vertxMetrics = vertx.metricsSPI();
60-
ClientMetrics metrics = vertxMetrics != null ? vertxMetrics.createClientMetrics(baseConnectOptions.getSocketAddress(), "sql", baseConnectOptions.getMetricsName()) : null;
61-
PoolImpl pool = new PoolImpl(vertx, this, tracer, metrics, 1, options, null, null, closeFuture);
62-
ConnectionFactory factory = createConnectionFactory(vertx, databases);
63-
pool.connectionProvider(factory::connect);
64-
pool.init();
65-
closeFuture.add(factory);
66-
return pool;
67-
}
68-
69-
@Override
70-
public MSSQLPool newPool(Vertx vertx, List<? extends SqlConnectOptions> databases, PoolOptions options, CloseFuture closeFuture) {
7150
VertxInternal vx = (VertxInternal) vertx;
7251
PoolImpl pool;
7352
if (options.isShared()) {
@@ -78,14 +57,13 @@ public MSSQLPool newPool(Vertx vertx, List<? extends SqlConnectOptions> database
7857
return new MSSQLPoolImpl(vx, closeFuture, pool);
7958
}
8059

81-
private PoolImpl newPoolImpl(VertxInternal vertx, List<? extends SqlConnectOptions> databases, PoolOptions options, CloseFuture closeFuture) {
82-
MSSQLConnectOptions baseConnectOptions = MSSQLConnectOptions.wrap(databases.get(0));
60+
private PoolImpl newPoolImpl(VertxInternal vertx, Supplier<? extends SqlConnectOptions> databases, PoolOptions options, CloseFuture closeFuture) {
61+
MSSQLConnectOptions baseConnectOptions = MSSQLConnectOptions.wrap(databases.get());
8362
QueryTracer tracer = vertx.tracer() == null ? null : new QueryTracer(vertx.tracer(), baseConnectOptions);
8463
VertxMetrics vertxMetrics = vertx.metricsSPI();
8564
ClientMetrics metrics = vertxMetrics != null ? vertxMetrics.createClientMetrics(baseConnectOptions.getSocketAddress(), "sql", baseConnectOptions.getMetricsName()) : null;
8665
PoolImpl pool = new PoolImpl(vertx, this, tracer, metrics, 1, options, null, null, closeFuture);
87-
List<ConnectionFactory> lst = databases.stream().map(o -> createConnectionFactory(vertx, o)).collect(Collectors.toList());
88-
ConnectionFactory factory = ConnectionFactory.roundRobinSelector(lst);
66+
ConnectionFactory factory = createConnectionFactory(vertx, databases);
8967
pool.connectionProvider(factory::connect);
9068
pool.init();
9169
closeFuture.add(factory);

vertx-mysql-client/src/main/java/io/vertx/mysqlclient/spi/MySQLDriver.java

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import io.vertx.core.spi.metrics.ClientMetrics;
2424
import io.vertx.core.spi.metrics.VertxMetrics;
2525
import io.vertx.mysqlclient.MySQLConnectOptions;
26-
import io.vertx.mysqlclient.MySQLPool;
2726
import io.vertx.mysqlclient.impl.*;
2827
import io.vertx.sqlclient.Pool;
2928
import io.vertx.sqlclient.PoolOptions;
@@ -35,9 +34,7 @@
3534
import io.vertx.sqlclient.spi.ConnectionFactory;
3635
import io.vertx.sqlclient.spi.Driver;
3736

38-
import java.util.List;
3937
import java.util.function.Supplier;
40-
import java.util.stream.Collectors;
4138

4239
public class MySQLDriver implements Driver {
4340

@@ -72,34 +69,6 @@ private PoolImpl newPoolImpl(VertxInternal vertx, Supplier<? extends SqlConnectO
7269
return pool;
7370
}
7471

75-
@Override
76-
public MySQLPool newPool(Vertx vertx, List<? extends SqlConnectOptions> databases, PoolOptions options, CloseFuture closeFuture) {
77-
VertxInternal vx = (VertxInternal) vertx;
78-
PoolImpl pool;
79-
if (options.isShared()) {
80-
pool = vx.createSharedClient(SHARED_CLIENT_KEY, options.getName(), closeFuture, cf -> newPoolImpl(vx, databases, options, cf));
81-
} else {
82-
pool = newPoolImpl(vx, databases, options, closeFuture);
83-
}
84-
return new MySQLPoolImpl(vx, closeFuture, pool);
85-
}
86-
87-
private PoolImpl newPoolImpl(VertxInternal vertx, List<? extends SqlConnectOptions> databases, PoolOptions options, CloseFuture closeFuture) {
88-
MySQLConnectOptions baseConnectOptions = MySQLConnectOptions.wrap(databases.get(0));
89-
QueryTracer tracer = vertx.tracer() == null ? null : new QueryTracer(vertx.tracer(), baseConnectOptions);
90-
VertxMetrics vertxMetrics = vertx.metricsSPI();
91-
ClientMetrics metrics = vertxMetrics != null ? vertxMetrics.createClientMetrics(baseConnectOptions.getSocketAddress(), "sql", baseConnectOptions.getMetricsName()) : null;
92-
boolean pipelinedPool = options instanceof MySQLPoolOptions && ((MySQLPoolOptions) options).isPipelined();
93-
int pipeliningLimit = pipelinedPool ? baseConnectOptions.getPipeliningLimit() : 1;
94-
PoolImpl pool = new PoolImpl(vertx, this, tracer, metrics, pipeliningLimit, options, null, null, closeFuture);
95-
List<ConnectionFactory> lst = databases.stream().map(o -> createConnectionFactory(vertx, o)).collect(Collectors.toList());
96-
ConnectionFactory factory = ConnectionFactory.roundRobinSelector(lst);
97-
pool.connectionProvider(factory::connect);
98-
pool.init();
99-
closeFuture.add(factory);
100-
return pool;
101-
}
102-
10372
@Override
10473
public MySQLConnectOptions parseConnectionUri(String uri) {
10574
JsonObject conf = MySQLConnectionUriParser.parse(uri, false);

vertx-oracle-client/src/main/java/io/vertx/oracleclient/spi/OracleDriver.java

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import io.vertx.core.spi.metrics.ClientMetrics;
2020
import io.vertx.core.spi.metrics.VertxMetrics;
2121
import io.vertx.oracleclient.OracleConnectOptions;
22-
import io.vertx.oracleclient.OraclePool;
2322
import io.vertx.oracleclient.impl.*;
2423
import io.vertx.sqlclient.Pool;
2524
import io.vertx.sqlclient.PoolOptions;
@@ -31,10 +30,8 @@
3130
import io.vertx.sqlclient.spi.ConnectionFactory;
3231
import io.vertx.sqlclient.spi.Driver;
3332

34-
import java.util.List;
3533
import java.util.function.Function;
3634
import java.util.function.Supplier;
37-
import java.util.stream.Collectors;
3835

3936
public class OracleDriver implements Driver {
4037

@@ -69,34 +66,6 @@ private PoolImpl newPoolImpl(VertxInternal vertx, Supplier<? extends SqlConnectO
6966
return pool;
7067
}
7168

72-
@Override
73-
public OraclePool newPool(Vertx vertx, List<? extends SqlConnectOptions> databases, PoolOptions options, CloseFuture closeFuture) {
74-
VertxInternal vx = (VertxInternal) vertx;
75-
PoolImpl pool;
76-
if (options.isShared()) {
77-
pool = vx.createSharedClient(SHARED_CLIENT_KEY, options.getName(), closeFuture, cf -> newPoolImpl(vx, databases, options, cf));
78-
} else {
79-
pool = newPoolImpl(vx, databases, options, closeFuture);
80-
}
81-
return new OraclePoolImpl(vx, closeFuture, pool);
82-
}
83-
84-
private PoolImpl newPoolImpl(VertxInternal vertx, List<? extends SqlConnectOptions> databases, PoolOptions options, CloseFuture closeFuture) {
85-
OracleConnectOptions baseConnectOptions = OracleConnectOptions.wrap(databases.get(0));
86-
QueryTracer tracer = vertx.tracer() == null ? null : new QueryTracer(vertx.tracer(), baseConnectOptions);
87-
VertxMetrics vertxMetrics = vertx.metricsSPI();
88-
ClientMetrics metrics = vertxMetrics != null ? vertxMetrics.createClientMetrics(baseConnectOptions.getSocketAddress(), "sql", baseConnectOptions.getMetricsName()) : null;
89-
Function<Connection, Future<Void>> afterAcquire = conn -> ((OracleJdbcConnection) conn).afterAcquire();
90-
Function<Connection, Future<Void>> beforeRecycle = conn -> ((OracleJdbcConnection) conn).beforeRecycle();
91-
PoolImpl pool = new PoolImpl(vertx, this, tracer, metrics, 1, options, afterAcquire, beforeRecycle, closeFuture);
92-
List<ConnectionFactory> lst = databases.stream().map(o -> createConnectionFactory(vertx, o)).collect(Collectors.toList());
93-
ConnectionFactory factory = ConnectionFactory.roundRobinSelector(lst);
94-
pool.connectionProvider(factory::connect);
95-
pool.init();
96-
closeFuture.add(factory);
97-
return pool;
98-
}
99-
10069
@Override
10170
public OracleConnectOptions parseConnectionUri(String uri) {
10271
JsonObject conf = OracleConnectionUriParser.parse(uri, false);

vertx-pg-client/src/main/java/io/vertx/pgclient/spi/PgDriver.java

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import io.vertx.core.spi.metrics.ClientMetrics;
99
import io.vertx.core.spi.metrics.VertxMetrics;
1010
import io.vertx.pgclient.PgConnectOptions;
11-
import io.vertx.pgclient.PgPool;
1211
import io.vertx.pgclient.impl.*;
1312
import io.vertx.sqlclient.Pool;
1413
import io.vertx.sqlclient.PoolOptions;
@@ -30,34 +29,6 @@ public class PgDriver implements Driver {
3029

3130
public static final PgDriver INSTANCE = new PgDriver();
3231

33-
@Override
34-
public PgPool newPool(Vertx vertx, List<? extends SqlConnectOptions> databases, PoolOptions options, CloseFuture closeFuture) {
35-
VertxInternal vx = (VertxInternal) vertx;
36-
PoolImpl pool;
37-
if (options.isShared()) {
38-
pool = vx.createSharedClient(SHARED_CLIENT_KEY, options.getName(), closeFuture, cf -> newPoolImpl(vx, databases, options, cf));
39-
} else {
40-
pool = newPoolImpl(vx, databases, options, closeFuture);
41-
}
42-
return new PgPoolImpl(vx, closeFuture, pool);
43-
}
44-
45-
private PoolImpl newPoolImpl(VertxInternal vertx, List<? extends SqlConnectOptions> databases, PoolOptions options, CloseFuture closeFuture) {
46-
PgConnectOptions baseConnectOptions = PgConnectOptions.wrap(databases.get(0));
47-
QueryTracer tracer = vertx.tracer() == null ? null : new QueryTracer(vertx.tracer(), baseConnectOptions);
48-
VertxMetrics vertxMetrics = vertx.metricsSPI();
49-
ClientMetrics metrics = vertxMetrics != null ? vertxMetrics.createClientMetrics(baseConnectOptions.getSocketAddress(), "sql", baseConnectOptions.getMetricsName()) : null;
50-
boolean pipelinedPool = options instanceof PgPoolOptions && ((PgPoolOptions) options).isPipelined();
51-
int pipeliningLimit = pipelinedPool ? baseConnectOptions.getPipeliningLimit() : 1;
52-
PoolImpl pool = new PoolImpl(vertx, this, tracer, metrics, pipeliningLimit, options, null, null, closeFuture);
53-
List<ConnectionFactory> lst = databases.stream().map(o -> createConnectionFactory(vertx, o)).collect(Collectors.toList());
54-
ConnectionFactory factory = ConnectionFactory.roundRobinSelector(lst);
55-
pool.connectionProvider(factory::connect);
56-
pool.init();
57-
closeFuture.add(factory);
58-
return pool;
59-
}
60-
6132
@Override
6233
public Pool newPool(Vertx vertx, Supplier<? extends SqlConnectOptions> databases, PoolOptions options, CloseFuture closeFuture) {
6334
VertxInternal vx = (VertxInternal) vertx;

vertx-pg-client/src/test/java/io/vertx/pgclient/PoolMultiTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public void teardown(TestContext ctx) {
4949

5050
@Test
5151
public void testListLoadBalancing(TestContext ctx) {
52-
testLoadBalancing(ctx, PgPool.pool(vertx, ConnectionFactory.roundRobinSupplier(Arrays.asList(db1.options(), db2.options())),new PoolOptions().setMaxSize(5)));
52+
testLoadBalancing(ctx, PgPool.pool(vertx, Arrays.asList(db1.options(), db2.options()),new PoolOptions().setMaxSize(5)));
5353
}
5454

5555
@Test

vertx-sql-client-templates/src/test/java/io/vertx/sqlclient/templates/TemplateBuilderTest.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,6 @@ public int appendQueryPlaceholder(StringBuilder queryBuilder, int index, int cur
4444
return FakeClient.this.appendQueryPlaceholder(queryBuilder, index, current);
4545
}
4646
@Override
47-
public Pool newPool(Vertx vertx, List<? extends SqlConnectOptions> databases, PoolOptions options, CloseFuture closeFuture) {
48-
throw new UnsupportedOperationException();
49-
}
50-
@Override
5147
public Pool newPool(Vertx vertx, Supplier<? extends SqlConnectOptions> databases, PoolOptions options, CloseFuture closeFuture) {
5248
throw new UnsupportedOperationException();
5349
}

vertx-sql-client/src/main/java/io/vertx/sqlclient/spi/Driver.java

Lines changed: 4 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -100,41 +100,7 @@ default Pool createPool(Vertx vertx, Supplier<? extends SqlConnectOptions> datab
100100
* @return the connection pool
101101
*/
102102
default Pool createPool(Vertx vertx, List<? extends SqlConnectOptions> databases, PoolOptions options) {
103-
VertxInternal vx;
104-
if (vertx == null) {
105-
if (Vertx.currentContext() != null) {
106-
throw new IllegalStateException("Running in a Vertx context => use Pool#pool(Vertx, SqlConnectOptions, PoolOptions) instead");
107-
}
108-
VertxOptions vertxOptions = new VertxOptions();
109-
SqlConnectOptions database = databases.get(0);
110-
if (database.isUsingDomainSocket()) {
111-
vertxOptions.setPreferNativeTransport(true);
112-
}
113-
vx = (VertxInternal) Vertx.vertx(vertxOptions);
114-
} else {
115-
vx = (VertxInternal) vertx;
116-
}
117-
CloseFuture closeFuture = new CloseFuture();
118-
Pool pool;
119-
try {
120-
pool = newPool(vx, databases, options, closeFuture);
121-
} catch (Exception e) {
122-
if (vertx == null) {
123-
vx.close();
124-
}
125-
throw e;
126-
}
127-
if (vertx == null) {
128-
closeFuture.future().onComplete(ar -> vx.close());
129-
} else {
130-
ContextInternal ctx = vx.getContext();
131-
if (ctx != null) {
132-
ctx.addCloseHook(closeFuture);
133-
} else {
134-
vx.addCloseHook(closeFuture);
135-
}
136-
}
137-
return pool;
103+
return createPool(vertx, ConnectionFactory.roundRobinSupplier(databases), options);
138104
}
139105

140106
/**
@@ -148,7 +114,9 @@ default Pool createPool(Vertx vertx, List<? extends SqlConnectOptions> databases
148114
* @param closeFuture the close future
149115
* @return the connection pool
150116
*/
151-
Pool newPool(Vertx vertx, List<? extends SqlConnectOptions> databases, PoolOptions options, CloseFuture closeFuture);
117+
default Pool newPool(Vertx vertx, List<? extends SqlConnectOptions> databases, PoolOptions options, CloseFuture closeFuture) {
118+
return newPool(vertx, ConnectionFactory.roundRobinSupplier(databases), options, closeFuture);
119+
}
152120

153121
/**
154122
* Create a connection pool to the database configured with the given {@code connectOptions} and {@code poolOptions}.

0 commit comments

Comments
 (0)