Skip to content

Commit b25f240

Browse files
committed
Move connection factory specific methods to GenericDriver.
Motivation: The Driver interface does not need to declare connection pool related details such as the connection factory and the connection wrapper methods. Those instead can be on the GenericDriver, so let's move them there.
1 parent 21a5be9 commit b25f240

File tree

10 files changed

+56
-33
lines changed

10 files changed

+56
-33
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ protected Pool newPool(VertxInternal vertx, Handler<SqlConnection> connectHandle
5858
boolean pipelinedPool = poolOptions instanceof Db2PoolOptions && ((Db2PoolOptions) poolOptions).isPipelined();
5959
ConnectionFactory<DB2ConnectOptions> factory = createConnectionFactory(vertx, transportOptions);
6060
PoolImpl pool = new PoolImpl(vertx, this, pipelinedPool, poolOptions, null, null,
61-
factory, databases, connectHandler, closeFuture);
61+
factory, databases, connectHandler, this::wrapConnection, closeFuture);
6262
pool.init();
6363
closeFuture.add(factory);
6464
return pool;
@@ -81,7 +81,7 @@ public ConnectionFactory<DB2ConnectOptions> createConnectionFactory(Vertx vertx,
8181
}
8282

8383
@Override
84-
public SqlConnectionInternal wrapConnection(ContextInternal context, ConnectionFactory<DB2ConnectOptions> factory, Connection conn) {
85-
return new DB2ConnectionImpl(context, factory, conn);
84+
public SqlConnectionInternal wrapConnection(ContextInternal context, ConnectionFactory<DB2ConnectOptions> factory, Connection connection) {
85+
return new DB2ConnectionImpl(context, factory, connection);
8686
}
8787
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public int appendQueryPlaceholder(StringBuilder queryBuilder, int index, int cur
6969
}
7070

7171
@Override
72-
public SqlConnectionInternal wrapConnection(ContextInternal context, ConnectionFactory<MSSQLConnectOptions> factory, Connection conn) {
73-
return new MSSQLConnectionImpl(context, factory, conn);
72+
public SqlConnectionInternal wrapConnection(ContextInternal context, ConnectionFactory<MSSQLConnectOptions> factory, Connection connection) {
73+
return new MSSQLConnectionImpl(context, factory, connection);
7474
}
7575
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ protected Pool newPool(VertxInternal vertx, Handler<SqlConnection> connectHandle
5858
boolean pipelinedPool = poolOptions instanceof MySQLPoolOptions && ((MySQLPoolOptions) poolOptions).isPipelined();
5959
ConnectionFactory<MySQLConnectOptions> factory = createConnectionFactory(vertx, transportOptions);
6060
PoolImpl pool = new PoolImpl(vertx, this, pipelinedPool, poolOptions, null, null, factory,
61-
databases, connectHandler, closeFuture);
61+
databases, connectHandler, this::wrapConnection, closeFuture);
6262
pool.init();
6363
closeFuture.add(factory);
6464
return pool;
@@ -81,7 +81,7 @@ public ConnectionFactory<MySQLConnectOptions> createConnectionFactory(Vertx vert
8181
}
8282

8383
@Override
84-
public SqlConnectionInternal wrapConnection(ContextInternal context, ConnectionFactory<MySQLConnectOptions> factory, Connection conn) {
85-
return new MySQLConnectionImpl(context, factory, conn);
84+
public SqlConnectionInternal wrapConnection(ContextInternal context, ConnectionFactory<MySQLConnectOptions> factory, Connection connection) {
85+
return new MySQLConnectionImpl(context, factory, connection);
8686
}
8787
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public ConnectionFactory<OracleConnectOptions> createConnectionFactory(Vertx ver
6666
}
6767

6868
@Override
69-
public SqlConnectionInternal wrapConnection(ContextInternal context, ConnectionFactory<OracleConnectOptions> factory, Connection conn) {
70-
return new OracleConnectionImpl(context, factory, conn);
69+
public SqlConnectionInternal wrapConnection(ContextInternal context, ConnectionFactory<OracleConnectOptions> factory, Connection connection) {
70+
return new OracleConnectionImpl(context, factory, connection);
7171
}
7272
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ protected Pool newPool(VertxInternal vertx, Handler<SqlConnection> connectHandle
4141
boolean pipelinedPool = poolOptions instanceof PgPoolOptions && ((PgPoolOptions) poolOptions).isPipelined();
4242
ConnectionFactory<PgConnectOptions> factory = createConnectionFactory(vertx, transportOptions);
4343
PoolImpl pool = new PoolImpl(vertx, this, pipelinedPool, poolOptions, null, null,
44-
factory, databases, connectHandler, closeFuture);
44+
factory, databases, connectHandler, this::wrapConnection, closeFuture);
4545
pool.init();
4646
closeFuture.add(factory);
4747
return pool;
@@ -75,7 +75,7 @@ public int appendQueryPlaceholder(StringBuilder queryBuilder, int index, int cur
7575
}
7676

7777
@Override
78-
public SqlConnectionInternal wrapConnection(ContextInternal context, ConnectionFactory<PgConnectOptions> factory, Connection conn) {
79-
return new PgConnectionImpl((PgConnectionFactory) factory, context, conn);
78+
public SqlConnectionInternal wrapConnection(ContextInternal context, ConnectionFactory<PgConnectOptions> factory, Connection connection) {
79+
return new PgConnectionImpl((PgConnectionFactory) factory, context, connection);
8080
}
8181
}

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,6 @@ public Pool newPool(Vertx vertx, Supplier<Future<SqlConnectOptions>> databases,
4242
throw new UnsupportedOperationException();
4343
}
4444
@Override
45-
public ConnectionFactory<SqlConnectOptions> createConnectionFactory(Vertx vertx, NetClientOptions transportOptions) {
46-
throw new UnsupportedOperationException();
47-
}
48-
@Override
4945
public boolean acceptsOptions(SqlConnectOptions connectOptions) {
5046
throw new UnsupportedOperationException();
5147
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package io.vertx.sqlclient.impl.pool;
2+
3+
import io.vertx.core.internal.ContextInternal;
4+
import io.vertx.sqlclient.SqlConnectOptions;
5+
import io.vertx.sqlclient.internal.Connection;
6+
import io.vertx.sqlclient.internal.SqlConnectionInternal;
7+
import io.vertx.sqlclient.spi.ConnectionFactory;
8+
9+
public interface ConnectionWrapper<O extends SqlConnectOptions> {
10+
11+
SqlConnectionInternal wrap(ContextInternal context, ConnectionFactory<O> factory, Connection conn);
12+
}

vertx-sql-client/src/main/java/io/vertx/sqlclient/impl/pool/PoolImpl.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public class PoolImpl extends SqlClientBase implements Pool, Closeable {
5454
private final long cleanerPeriod;
5555
private final boolean pipelined;
5656
private final Handler<SqlConnection> connectionInitializer;
57+
private final ConnectionWrapper<?> connectionWrapper;
5758
private long timerID;
5859

5960
public <O extends SqlConnectOptions> PoolImpl(VertxInternal vertx,
@@ -65,6 +66,7 @@ public <O extends SqlConnectOptions> PoolImpl(VertxInternal vertx,
6566
ConnectionFactory<O> connectionFactory,
6667
Supplier<Future<O>> connectionProvider,
6768
Handler<SqlConnection> connectionInitializer,
69+
ConnectionWrapper connectionWrapper,
6870
CloseFuture closeFuture) {
6971
super(driver);
7072

@@ -78,6 +80,7 @@ public <O extends SqlConnectOptions> PoolImpl(VertxInternal vertx,
7880
poolMetrics = null;
7981
}
8082

83+
this.connectionWrapper = connectionWrapper;
8184
this.idleTimeout = MILLISECONDS.convert(poolOptions.getIdleTimeout(), poolOptions.getIdleTimeoutUnit());
8285
this.connectionTimeout = MILLISECONDS.convert(poolOptions.getConnectionTimeout(), poolOptions.getConnectionTimeoutUnit());
8386
this.maxLifetime = MILLISECONDS.convert(poolOptions.getMaxLifetime(), poolOptions.getMaxLifetimeUnit());
@@ -95,7 +98,7 @@ public <O extends SqlConnectOptions> PoolImpl(VertxInternal vertx,
9598
private void initializeConnection(SqlConnectionPool.PooledConnection conn) {
9699
if (connectionInitializer != null) {
97100
ContextInternal current = vertx.getContext();
98-
SqlConnectionInternal wrapper = driver.wrapConnection(current, conn.factory(), conn);
101+
SqlConnectionInternal wrapper = connectionWrapper.wrap(current, conn.factory(), conn);
99102
conn.init((Connection.Holder) wrapper);
100103
current.dispatch(wrapper, connectionInitializer);
101104
}
@@ -144,7 +147,7 @@ public Future<SqlConnection> getConnection() {
144147
Promise<SqlConnectionPool.PooledConnection> promise = current.promise();
145148
acquire(current, connectionTimeout, promise);
146149
return promise.future().map(conn -> {
147-
SqlConnectionInternal wrapper = driver.wrapConnection(current, conn.factory(), conn);
150+
SqlConnectionInternal wrapper = connectionWrapper.wrap(current, conn.factory(), conn);
148151
conn.init((Connection.Holder) wrapper);
149152
return wrapper;
150153
});

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

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -101,15 +101,6 @@ default Pool createPool(Vertx vertx, Supplier<Future<C>> databases, PoolOptions
101101
*/
102102
Pool newPool(Vertx vertx, Supplier<Future<C>> databases, PoolOptions options, NetClientOptions transportOptions, Handler<SqlConnection> connectHandler, CloseFuture closeFuture);
103103

104-
/**
105-
* Create a connection factory to the given {@code database}.
106-
*
107-
* @param vertx the Vertx instance
108-
* @param transportOptions the options to configure the TCP client
109-
* @return the connection factory
110-
*/
111-
ConnectionFactory<C> createConnectionFactory(Vertx vertx, NetClientOptions transportOptions);
112-
113104
/**
114105
* @return {@code true} if the driver accepts the {@code connectOptions}, {@code false} otherwise
115106
*/
@@ -149,8 +140,4 @@ default int appendQueryPlaceholder(StringBuilder queryBuilder, int index, int cu
149140
queryBuilder.append("?");
150141
return current;
151142
}
152-
153-
default SqlConnectionInternal wrapConnection(ContextInternal context, ConnectionFactory<C> factory, Connection conn) {
154-
return new SqlConnectionBase<>(context, factory, conn, this);
155-
}
156143
}

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import io.vertx.core.Handler;
55
import io.vertx.core.Vertx;
66
import io.vertx.core.internal.CloseFuture;
7+
import io.vertx.core.internal.ContextInternal;
78
import io.vertx.core.internal.VertxInternal;
89
import io.vertx.core.net.NetClientOptions;
910
import io.vertx.sqlclient.Pool;
@@ -13,6 +14,8 @@
1314
import io.vertx.sqlclient.internal.Connection;
1415
import io.vertx.sqlclient.impl.pool.CloseablePool;
1516
import io.vertx.sqlclient.impl.pool.PoolImpl;
17+
import io.vertx.sqlclient.internal.SqlConnectionBase;
18+
import io.vertx.sqlclient.internal.SqlConnectionInternal;
1619

1720
import java.util.function.Function;
1821
import java.util.function.Supplier;
@@ -38,6 +41,28 @@ public GenericDriver(Function<Connection, Future<Void>> afterAcquire, Function<C
3841
this.beforeRecycle = beforeRecycle;
3942
}
4043

44+
/**
45+
* Create a connection factory to the given {@code database}.
46+
*
47+
* @param vertx the Vertx instance
48+
* @param transportOptions the options to configure the TCP client
49+
* @return the connection factory
50+
*/
51+
public abstract ConnectionFactory<O> createConnectionFactory(Vertx vertx, NetClientOptions transportOptions);
52+
53+
/**
54+
* Wrap a given {@code connection} into a {@link SqlConnectionInternal}. The default implementation
55+
* wraps with a generic {@link SqlConnectionBase}.
56+
*
57+
* @param context the connection context
58+
* @param factory the connection factory
59+
* @param connection the connection to wrap
60+
* @return the wrapped connection
61+
*/
62+
public SqlConnectionInternal wrapConnection(ContextInternal context, ConnectionFactory<O> factory, Connection connection) {
63+
return new SqlConnectionBase<>(context, factory, connection, this);
64+
}
65+
4166
@Override
4267
public Pool newPool(Vertx vertx, Supplier<Future<O>> databases, PoolOptions options, NetClientOptions transportOptions, Handler<SqlConnection> connectHandler, CloseFuture closeFuture) {
4368
VertxInternal vx = (VertxInternal) vertx;
@@ -53,7 +78,7 @@ public Pool newPool(Vertx vertx, Supplier<Future<O>> databases, PoolOptions opti
5378
protected Pool newPool(VertxInternal vertx, Handler<SqlConnection> connectHandler, Supplier<Future<O>> databases, PoolOptions poolOptions, NetClientOptions transportOptions, CloseFuture closeFuture) {
5479
ConnectionFactory<O> factory = createConnectionFactory(vertx, transportOptions);
5580
PoolImpl pool = new PoolImpl(vertx, this, false, poolOptions, afterAcquire, beforeRecycle,
56-
factory, databases, connectHandler, closeFuture);
81+
factory, databases, connectHandler, this::wrapConnection, closeFuture);
5782
pool.init();
5883
closeFuture.add(factory);
5984
return pool;

0 commit comments

Comments
 (0)