diff --git a/vertx-db2-client/src/main/java/io/vertx/db2client/DB2Builder.java b/vertx-db2-client/src/main/java/io/vertx/db2client/DB2Builder.java index aa0c2a549..eb35e4f38 100644 --- a/vertx-db2-client/src/main/java/io/vertx/db2client/DB2Builder.java +++ b/vertx-db2-client/src/main/java/io/vertx/db2client/DB2Builder.java @@ -86,8 +86,8 @@ public ClientBuilder with(PoolOptions options) { } @Override - protected SqlClient create(Vertx vertx, Supplier> databases, PoolOptions poolOptions, NetClientOptions transportOptions) { - return driver.createPool(vertx, databases, poolOptions, transportOptions); + protected SqlClient create(Vertx vertx, Supplier> databases, PoolOptions poolOptions, NetClientOptions transportOptions, Handler connectHandler) { + return driver.createPool(vertx, databases, poolOptions, transportOptions, connectHandler); } }; } diff --git a/vertx-db2-client/src/main/java/io/vertx/db2client/DB2Pool.java b/vertx-db2-client/src/main/java/io/vertx/db2client/DB2Pool.java deleted file mode 100644 index dda5fd1f8..000000000 --- a/vertx-db2-client/src/main/java/io/vertx/db2client/DB2Pool.java +++ /dev/null @@ -1,209 +0,0 @@ -/* - * Copyright (C) 2019,2020 IBM Corporation - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package io.vertx.db2client; - -import io.vertx.codegen.annotations.Fluent; -import io.vertx.codegen.annotations.VertxGen; -import io.vertx.core.Context; -import io.vertx.core.Future; -import io.vertx.core.Handler; -import io.vertx.core.Vertx; -import io.vertx.core.net.NetClientOptions; -import io.vertx.db2client.impl.Db2PoolOptions; -import io.vertx.db2client.spi.DB2Driver; -import io.vertx.sqlclient.Pool; -import io.vertx.sqlclient.PoolOptions; -import io.vertx.sqlclient.SqlClient; -import io.vertx.sqlclient.SqlConnection; -import io.vertx.sqlclient.impl.Utils; - -import java.util.Collections; -import java.util.List; -import java.util.function.Function; -import java.util.function.Supplier; - -import static io.vertx.db2client.DB2ConnectOptions.fromUri; - -/** - * A pool of DB2 connections. - */ -@VertxGen -public interface DB2Pool extends Pool { - - /** - * Like {@link #pool(String, PoolOptions)} with default options. - */ - static DB2Pool pool(String connectionUri) { - return pool(connectionUri, new PoolOptions()); - } - - /** - * Like {@link #pool(DB2ConnectOptions, PoolOptions)} with - * {@code database} build from {@code connectionUri}. - */ - static DB2Pool pool(String connectionUri, PoolOptions options) { - return pool(fromUri(connectionUri), options); - } - - /** - * Like {@link #pool(Vertx, String,PoolOptions)} with default options. - */ - static DB2Pool pool(Vertx vertx, String connectionUri) { - return pool(vertx, fromUri(connectionUri), new PoolOptions()); - } - - /** - * Like {@link #pool(Vertx, DB2ConnectOptions, PoolOptions)} with - * {@code database} build from {@code connectionUri}. - */ - static DB2Pool pool(Vertx vertx, String connectionUri, PoolOptions options) { - return pool(vertx, fromUri(connectionUri), options); - } - - /** - * Create a connection pool to the DB2 {@code database} configured with the given {@code options}. - * - * @param database the options for the connection - * @param options the options for creating the pool - * @return the connection pool - */ - static DB2Pool pool(DB2ConnectOptions database, PoolOptions options) { - return pool(null, database, options); - } - - /** - * Like {@link #pool(DB2ConnectOptions, PoolOptions)} with a specific - * {@link Vertx} instance. - */ - static DB2Pool pool(Vertx vertx, DB2ConnectOptions database, PoolOptions options) { - return pool(vertx, Collections.singletonList(database), options); - } - - /** - * Create a connection pool to the DB2 {@code databases} with round-robin selection. - * Round-robin is applied when a new connection is created by the pool. - * - * @param databases the list of servers - * @param options the options for creating the pool - * @return the connection pool - */ - static DB2Pool pool(List databases, PoolOptions options) { - return pool(null, databases, options); - } - - /** - * Like {@link #pool(List, PoolOptions)} with a specific - * {@link Vertx} instance. - */ - static DB2Pool pool(Vertx vertx, List databases, PoolOptions options) { - return (DB2Pool) DB2Driver.INSTANCE.createPool(vertx, Utils.roundRobinSupplier(databases), options, new NetClientOptions()); - } - - /** - * Create a connection pool to the DB2 {@code databases}. The supplier is called - * to provide the options when a new connection is created by the pool. - * - * @param databases the databases supplier - * @param poolOptions the options for creating the pool - * @return the connection pool - */ - static DB2Pool pool(Supplier> databases, PoolOptions poolOptions) { - return pool(null, databases, poolOptions); - } - - - /** - * Like {@link #pool(Supplier, PoolOptions)} with a specific {@link Vertx} instance. - */ - static DB2Pool pool(Vertx vertx, Supplier> databases, PoolOptions poolOptions) { - return (DB2Pool) DB2Driver.INSTANCE.createPool(vertx, databases, poolOptions, new NetClientOptions()); - } - - /** - * Like {@link #client(String, PoolOptions)} with default options. - */ - static SqlClient client(String connectionUri) { - return client(connectionUri, new PoolOptions()); - } - - /** - * Like {@link #client(DB2ConnectOptions, PoolOptions)} with - * {@code database} build from {@code connectionUri}. - */ - static SqlClient client(String connectionUri, PoolOptions options) { - return client(fromUri(connectionUri), options); - } - - /** - * Like {@link #client(Vertx, String, PoolOptions)} with default options. - */ - static SqlClient client(Vertx vertx, String connectionUri) { - return client(vertx, fromUri(connectionUri), new PoolOptions()); - } - - /** - * Like {@link #client(Vertx, DB2ConnectOptions, PoolOptions)} with - * {@code database} build from {@code connectionUri}. - */ - static SqlClient client(Vertx vertx, String connectionUri, PoolOptions options) { - return client(vertx, fromUri(connectionUri), options); - } - - /** - * Create a pooled client to the DB2 {@code database} configured with the given {@code options}. - * - * @param database the options for the connection - * @param options the options for creating the pool - * @return the connection pool - */ - static SqlClient client(DB2ConnectOptions database, PoolOptions options) { - return client(null, database, options); - } - - /** - * Like {@link #client(DB2ConnectOptions, PoolOptions)} with a specific - * {@link Vertx} instance. - */ - static SqlClient client(Vertx vertx, DB2ConnectOptions database, PoolOptions options) { - return client(vertx, Collections.singletonList(database), options); - } - - /** - * Create a client backed by a connection pool to the DB2 {@code databases} with round-robin selection. - * Round-robin is applied when a new connection is created by the pool. - * - * @param databases the list of servers - * @param options the options for creating the pool - * @return the pooled client - */ - static SqlClient client(List databases, PoolOptions options) { - return client(null, databases, options); - } - - /** - * Like {@link #client(List, PoolOptions)} with a specific - * {@link Vertx} instance. - */ - static SqlClient client(Vertx vertx, List databases, PoolOptions options) { - return DB2Driver.INSTANCE.createPool(vertx, Utils.roundRobinSupplier(databases), new Db2PoolOptions(options).setPipelined(true), new NetClientOptions()); - } - - @Override - DB2Pool connectHandler(Handler handler); - - @Fluent - DB2Pool connectionProvider(Function> provider); -} diff --git a/vertx-db2-client/src/main/java/io/vertx/db2client/impl/DB2PoolImpl.java b/vertx-db2-client/src/main/java/io/vertx/db2client/impl/DB2PoolImpl.java deleted file mode 100644 index 5cafc6901..000000000 --- a/vertx-db2-client/src/main/java/io/vertx/db2client/impl/DB2PoolImpl.java +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright (C) 2019,2020 IBM Corporation - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package io.vertx.db2client.impl; - -import io.vertx.core.impl.CloseFuture; -import io.vertx.core.impl.VertxInternal; -import io.vertx.db2client.DB2Pool; -import io.vertx.sqlclient.Pool; -import io.vertx.sqlclient.impl.PoolBase; - -public class DB2PoolImpl extends PoolBase implements DB2Pool { - - public DB2PoolImpl(VertxInternal vertx, CloseFuture closeFuture, Pool delegate) { - super(vertx, closeFuture, delegate); - } -} diff --git a/vertx-db2-client/src/main/java/io/vertx/db2client/spi/DB2Driver.java b/vertx-db2-client/src/main/java/io/vertx/db2client/spi/DB2Driver.java index 5f60b9a6e..4b3020111 100644 --- a/vertx-db2-client/src/main/java/io/vertx/db2client/spi/DB2Driver.java +++ b/vertx-db2-client/src/main/java/io/vertx/db2client/spi/DB2Driver.java @@ -16,6 +16,7 @@ package io.vertx.db2client.spi; import io.vertx.core.Future; +import io.vertx.core.Handler; import io.vertx.core.Vertx; import io.vertx.core.impl.CloseFuture; import io.vertx.core.impl.ContextInternal; @@ -27,7 +28,9 @@ import io.vertx.sqlclient.Pool; import io.vertx.sqlclient.PoolOptions; import io.vertx.sqlclient.SqlConnectOptions; +import io.vertx.sqlclient.SqlConnection; import io.vertx.sqlclient.impl.Connection; +import io.vertx.sqlclient.impl.CloseablePool; import io.vertx.sqlclient.impl.PoolImpl; import io.vertx.sqlclient.impl.SqlConnectionInternal; import io.vertx.sqlclient.spi.ConnectionFactory; @@ -47,22 +50,21 @@ public DB2ConnectOptions downcast(SqlConnectOptions connectOptions) { } @Override - public Pool newPool(Vertx vertx, Supplier> databases, PoolOptions poolOptions, NetClientOptions transportOptions, CloseFuture closeFuture) { + public Pool newPool(Vertx vertx, Supplier> databases, PoolOptions poolOptions, NetClientOptions transportOptions, Handler connectHandler, CloseFuture closeFuture) { VertxInternal vx = (VertxInternal) vertx; PoolImpl pool; if (poolOptions.isShared()) { - pool = vx.createSharedResource(SHARED_CLIENT_KEY, poolOptions.getName(), closeFuture, cf -> newPoolImpl(vx, databases, poolOptions, transportOptions, cf)); + pool = vx.createSharedResource(SHARED_CLIENT_KEY, poolOptions.getName(), closeFuture, cf -> newPoolImpl(vx, connectHandler, databases, poolOptions, transportOptions, cf)); } else { - pool = newPoolImpl(vx, databases, poolOptions, transportOptions, closeFuture); + pool = newPoolImpl(vx, connectHandler, databases, poolOptions, transportOptions, closeFuture); } - return new DB2PoolImpl(vx, closeFuture, pool); + return new CloseablePool<>(vx, closeFuture, pool); } - private PoolImpl newPoolImpl(VertxInternal vertx, Supplier> databases, PoolOptions options, NetClientOptions transportOptions, CloseFuture closeFuture) { + private PoolImpl newPoolImpl(VertxInternal vertx, Handler connectHandler, Supplier> databases, PoolOptions options, NetClientOptions transportOptions, CloseFuture closeFuture) { boolean pipelinedPool = options instanceof Db2PoolOptions && ((Db2PoolOptions) options).isPipelined(); - PoolImpl pool = new PoolImpl(vertx, this, pipelinedPool, options, null, null, closeFuture); ConnectionFactory factory = createConnectionFactory(vertx, transportOptions); - pool.connectionProvider(context -> factory.connect(context, databases.get())); + PoolImpl pool = new PoolImpl(vertx, this, pipelinedPool, options, null, null, context -> factory.connect(context, databases.get()), connectHandler, closeFuture); pool.init(); closeFuture.add(factory); return pool; diff --git a/vertx-mssql-client/src/main/java/io/vertx/mssqlclient/MSSQLPool.java b/vertx-mssql-client/src/main/java/io/vertx/mssqlclient/MSSQLPool.java deleted file mode 100644 index 173569983..000000000 --- a/vertx-mssql-client/src/main/java/io/vertx/mssqlclient/MSSQLPool.java +++ /dev/null @@ -1,128 +0,0 @@ -/* - * Copyright (c) 2011-2019 Contributors to the Eclipse Foundation - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 - * which is available at https://www.apache.org/licenses/LICENSE-2.0. - * - * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 - */ - -package io.vertx.mssqlclient; - -import io.vertx.codegen.annotations.Fluent; -import io.vertx.codegen.annotations.VertxGen; -import io.vertx.core.Context; -import io.vertx.core.Future; -import io.vertx.core.Handler; -import io.vertx.core.Vertx; -import io.vertx.core.net.NetClientOptions; -import io.vertx.mssqlclient.spi.MSSQLDriver; -import io.vertx.sqlclient.*; -import io.vertx.sqlclient.impl.SingletonSupplier; -import io.vertx.sqlclient.impl.Utils; - -import java.util.List; -import java.util.function.Function; -import java.util.function.Supplier; - -import static io.vertx.mssqlclient.MSSQLConnectOptions.fromUri; - -/** - * A {@link Pool pool} of {@link MSSQLConnection SQL Server connections}. - */ -@VertxGen -@Deprecated -public interface MSSQLPool extends Pool { - - /** - * Like {@link #pool(String, PoolOptions)} with default options. - */ - static MSSQLPool pool(String connectionUri) { - return pool(connectionUri, new PoolOptions()); - } - - /** - * Like {@link #pool(MSSQLConnectOptions, PoolOptions)} with {@code database} built from {@code connectionUri}. - */ - static MSSQLPool pool(String connectionUri, PoolOptions options) { - return pool(fromUri(connectionUri), options); - } - - /** - * Like {@link #pool(Vertx, String, PoolOptions)} with default options. - */ - static MSSQLPool pool(Vertx vertx, String connectionUri) { - return pool(vertx, fromUri(connectionUri), new PoolOptions()); - } - - /** - * Like {@link #pool(Vertx, MSSQLConnectOptions, PoolOptions)} with {@code database} built from {@code connectionUri}. - */ - static MSSQLPool pool(Vertx vertx, String connectionUri, PoolOptions options) { - return pool(vertx, fromUri(connectionUri), options); - } - - /** - * Create a connection pool to the SQL server {@code database} configured with the given {@code options}. - * - * @param database the options for the connection - * @param options the options for creating the pool - * @return the connection pool - */ - static MSSQLPool pool(MSSQLConnectOptions database, PoolOptions options) { - return pool(null, database, options); - } - - /** - * Like {@link #pool(MSSQLConnectOptions, PoolOptions)} with a specific {@link Vertx} instance. - */ - static MSSQLPool pool(Vertx vertx, MSSQLConnectOptions database, PoolOptions options) { - return pool(vertx, SingletonSupplier.wrap(database), options); - } - - /** - * Create a connection pool to the SQL Server {@code databases} with round-robin selection. - * Round-robin is applied when a new connection is created by the pool. - * - * @param databases the list of databases - * @param options the options for creating the pool - * @return the connection pool - */ - static MSSQLPool pool(List databases, PoolOptions options) { - return pool(null, databases, options); - } - - /** - * Like {@link #pool(List, PoolOptions)} with a specific {@link Vertx} instance. - */ - static MSSQLPool pool(Vertx vertx, List databases, PoolOptions options) { - return (MSSQLPool) MSSQLDriver.INSTANCE.createPool(vertx, Utils.roundRobinSupplier(databases), options, new NetClientOptions()); - } - - /** - * Create a connection pool to the SQL Server {@code databases}. The supplier is called - * to provide the options when a new connection is created by the pool. - * - * @param databases the databases supplier - * @param options the options for creating the pool - * @return the connection pool - */ - static MSSQLPool pool(Supplier> databases, PoolOptions options) { - return pool(null, databases, options); - } - - /** - * Like {@link #pool(Supplier, PoolOptions)} with a specific {@link Vertx} instance. - */ - static MSSQLPool pool(Vertx vertx, Supplier> databases, PoolOptions options) { - return (MSSQLPool) MSSQLDriver.INSTANCE.createPool(vertx, databases, options, new NetClientOptions()); - } - - @Override - MSSQLPool connectHandler(Handler handler); - - @Fluent - MSSQLPool connectionProvider(Function> provider); -} diff --git a/vertx-mssql-client/src/main/java/io/vertx/mssqlclient/impl/MSSQLPoolImpl.java b/vertx-mssql-client/src/main/java/io/vertx/mssqlclient/impl/MSSQLPoolImpl.java deleted file mode 100644 index 177d3b4d1..000000000 --- a/vertx-mssql-client/src/main/java/io/vertx/mssqlclient/impl/MSSQLPoolImpl.java +++ /dev/null @@ -1,25 +0,0 @@ -/* - * Copyright (c) 2011-2020 Contributors to the Eclipse Foundation - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 - * which is available at https://www.apache.org/licenses/LICENSE-2.0. - * - * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 - */ - -package io.vertx.mssqlclient.impl; - -import io.vertx.core.impl.CloseFuture; -import io.vertx.core.impl.VertxInternal; -import io.vertx.mssqlclient.MSSQLPool; -import io.vertx.sqlclient.Pool; -import io.vertx.sqlclient.impl.PoolBase; - -public class MSSQLPoolImpl extends PoolBase implements MSSQLPool { - - public MSSQLPoolImpl(VertxInternal vertx, CloseFuture closeFuture, Pool delegate) { - super(vertx, closeFuture, delegate); - } -} diff --git a/vertx-mssql-client/src/main/java/io/vertx/mssqlclient/spi/MSSQLDriver.java b/vertx-mssql-client/src/main/java/io/vertx/mssqlclient/spi/MSSQLDriver.java index 8cdc4bd6c..3dceb991f 100644 --- a/vertx-mssql-client/src/main/java/io/vertx/mssqlclient/spi/MSSQLDriver.java +++ b/vertx-mssql-client/src/main/java/io/vertx/mssqlclient/spi/MSSQLDriver.java @@ -16,6 +16,7 @@ package io.vertx.mssqlclient.spi; import io.vertx.core.Future; +import io.vertx.core.Handler; import io.vertx.core.Vertx; import io.vertx.core.impl.CloseFuture; import io.vertx.core.impl.ContextInternal; @@ -26,11 +27,12 @@ import io.vertx.mssqlclient.impl.MSSQLConnectionFactory; import io.vertx.mssqlclient.impl.MSSQLConnectionImpl; import io.vertx.mssqlclient.impl.MSSQLConnectionUriParser; -import io.vertx.mssqlclient.impl.MSSQLPoolImpl; import io.vertx.sqlclient.Pool; import io.vertx.sqlclient.PoolOptions; import io.vertx.sqlclient.SqlConnectOptions; +import io.vertx.sqlclient.SqlConnection; import io.vertx.sqlclient.impl.Connection; +import io.vertx.sqlclient.impl.CloseablePool; import io.vertx.sqlclient.impl.PoolImpl; import io.vertx.sqlclient.impl.SqlConnectionInternal; import io.vertx.sqlclient.spi.ConnectionFactory; @@ -50,21 +52,20 @@ public MSSQLConnectOptions downcast(SqlConnectOptions connectOptions) { } @Override - public Pool newPool(Vertx vertx, Supplier> databases, PoolOptions options, NetClientOptions transportOptions, CloseFuture closeFuture) { + public Pool newPool(Vertx vertx, Supplier> databases, PoolOptions options, NetClientOptions transportOptions, Handler connectHandler, CloseFuture closeFuture) { VertxInternal vx = (VertxInternal) vertx; PoolImpl pool; if (options.isShared()) { - pool = vx.createSharedResource(SHARED_CLIENT_KEY, options.getName(), closeFuture, cf -> newPoolImpl(vx, databases, options, transportOptions, cf)); + pool = vx.createSharedResource(SHARED_CLIENT_KEY, options.getName(), closeFuture, cf -> newPoolImpl(vx, connectHandler, databases, options, transportOptions, cf)); } else { - pool = newPoolImpl(vx, databases, options, transportOptions, closeFuture); + pool = newPoolImpl(vx, connectHandler, databases, options, transportOptions, closeFuture); } - return new MSSQLPoolImpl(vx, closeFuture, pool); + return new CloseablePool<>(vx, closeFuture, pool); } - private PoolImpl newPoolImpl(VertxInternal vertx, Supplier> databases, PoolOptions poolOptions, NetClientOptions transportOptions, CloseFuture closeFuture) { - PoolImpl pool = new PoolImpl(vertx, this, false, poolOptions, null, null, closeFuture); + private PoolImpl newPoolImpl(VertxInternal vertx, Handler connectHandler, Supplier> databases, PoolOptions poolOptions, NetClientOptions transportOptions, CloseFuture closeFuture) { ConnectionFactory factory = createConnectionFactory(vertx, transportOptions); - pool.connectionProvider(context -> factory.connect(context, databases.get())); + PoolImpl pool = new PoolImpl(vertx, this, false, poolOptions, null, null, context -> factory.connect(context, databases.get()), connectHandler, closeFuture); pool.init(); closeFuture.add(factory); return pool; diff --git a/vertx-mysql-client/src/main/java/io/vertx/mysqlclient/MySQLBuilder.java b/vertx-mysql-client/src/main/java/io/vertx/mysqlclient/MySQLBuilder.java index 0aa5fa16c..726934268 100644 --- a/vertx-mysql-client/src/main/java/io/vertx/mysqlclient/MySQLBuilder.java +++ b/vertx-mysql-client/src/main/java/io/vertx/mysqlclient/MySQLBuilder.java @@ -85,8 +85,8 @@ public ClientBuilder with(PoolOptions options) { return super.with(options); } @Override - protected SqlClient create(Vertx vertx, Supplier> databases, PoolOptions poolOptions, NetClientOptions transportOptions) { - return driver.createPool(vertx, databases, poolOptions, transportOptions); + protected SqlClient create(Vertx vertx, Supplier> databases, PoolOptions poolOptions, NetClientOptions transportOptions, Handler connectHandler) { + return driver.createPool(vertx, databases, poolOptions, transportOptions, connectHandler); } }; } diff --git a/vertx-mysql-client/src/main/java/io/vertx/mysqlclient/MySQLPool.java b/vertx-mysql-client/src/main/java/io/vertx/mysqlclient/MySQLPool.java deleted file mode 100644 index fa58176fe..000000000 --- a/vertx-mysql-client/src/main/java/io/vertx/mysqlclient/MySQLPool.java +++ /dev/null @@ -1,214 +0,0 @@ -/* - * Copyright (c) 2011-2020 Contributors to the Eclipse Foundation - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 - * which is available at https://www.apache.org/licenses/LICENSE-2.0. - * - * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 - */ - -package io.vertx.mysqlclient; - -import io.vertx.codegen.annotations.Fluent; -import io.vertx.codegen.annotations.VertxGen; -import io.vertx.core.Context; -import io.vertx.core.Future; -import io.vertx.core.Handler; -import io.vertx.core.Vertx; -import io.vertx.core.net.NetClientOptions; -import io.vertx.mysqlclient.impl.MySQLPoolOptions; -import io.vertx.mysqlclient.spi.MySQLDriver; -import io.vertx.sqlclient.Pool; -import io.vertx.sqlclient.PoolOptions; -import io.vertx.sqlclient.SqlClient; -import io.vertx.sqlclient.SqlConnection; -import io.vertx.sqlclient.impl.SingletonSupplier; -import io.vertx.sqlclient.impl.Utils; - -import java.util.List; -import java.util.function.Function; -import java.util.function.Supplier; - -import static io.vertx.mysqlclient.MySQLConnectOptions.fromUri; - -/** - * A {@link Pool pool} of {@link MySQLConnection MySQL Connections}. - */ -@Deprecated -@VertxGen -public interface MySQLPool extends Pool { - - /** - * Like {@link #pool(String, PoolOptions)} with default options. - */ - static MySQLPool pool(String connectionUri) { - return pool(connectionUri, new PoolOptions()); - } - - /** - * Like {@link #pool(MySQLConnectOptions, PoolOptions)} with {@code database} built from {@code connectionUri}. - */ - static MySQLPool pool(String connectionUri, PoolOptions options) { - return pool(fromUri(connectionUri), options); - } - - /** - * Like {@link #pool(Vertx, String, PoolOptions)} with default options. - */ - static MySQLPool pool(Vertx vertx, String connectionUri) { - return pool(vertx, fromUri(connectionUri), new PoolOptions()); - } - - /** - * Like {@link #pool(Vertx, MySQLConnectOptions, PoolOptions)} with {@code database} built from {@code connectionUri}. - */ - static MySQLPool pool(Vertx vertx, String connectionUri, PoolOptions options) { - return pool(vertx, fromUri(connectionUri), options); - } - - /** - * Create a connection pool to the MySQL {@code server} configured with the given {@code options}. - * - * @param database the options for the connection - * @param options the options for creating the pool - * @return the connection pool - */ - static MySQLPool pool(MySQLConnectOptions database, PoolOptions options) { - return pool(null, database, options); - } - - /** - * Like {@link #pool(MySQLConnectOptions, PoolOptions)} with a specific {@link Vertx} instance. - */ - static MySQLPool pool(Vertx vertx, MySQLConnectOptions database, PoolOptions options) { - return pool(vertx, SingletonSupplier.wrap(database), options); - } - - /** - * Create a connection pool to the MySQL {@code databases} with round-robin selection. - * Round-robin is applied when a new connection is created by the pool. - * - * @param databases the list of servers - * @param options the options for creating the pool - * @return the connection pool - */ - static MySQLPool pool(List databases, PoolOptions options) { - return pool(null, databases, options); - } - - /** - * Like {@link #pool(List, PoolOptions)} with a specific {@link Vertx} instance. - */ - static MySQLPool pool(Vertx vertx, List databases, PoolOptions options) { - return (MySQLPool) MySQLDriver.INSTANCE.createPool(vertx, Utils.roundRobinSupplier(databases), options, new NetClientOptions()); - } - - /** - * Create a connection pool to the MySQL {@code databases}. The supplier is called - * to provide the options when a new connection is created by the pool. - * - * @param databases the server supplier - * @param options the options for creating the pool - * @return the connection pool - */ - static MySQLPool pool(Supplier> databases, PoolOptions options) { - return pool(null, databases, options); - } - - /** - * Like {@link #pool(Supplier, PoolOptions)} with a specific {@link Vertx} instance. - */ - static MySQLPool pool(Vertx vertx, Supplier> databases, PoolOptions options) { - return (MySQLPool) MySQLDriver.INSTANCE.createPool(vertx, databases, options, new NetClientOptions()); - } - /** - * Like {@link #client(String, PoolOptions)} with a default {@code poolOptions}. - */ - static SqlClient client(String connectionUri) { - return client(connectionUri, new PoolOptions()); - } - - /** - * Like {@link #client(MySQLConnectOptions, PoolOptions)} with {@code connectOptions} build from {@code connectionUri}. - */ - static SqlClient client(String connectionUri, PoolOptions poolOptions) { - return client(MySQLConnectOptions.fromUri(connectionUri), poolOptions); - } - - /** - * Like {@link #client(Vertx, String,PoolOptions)} with a default {@code poolOptions}. - */ - static SqlClient client(Vertx vertx, String connectionUri) { - return client(vertx, MySQLConnectOptions.fromUri(connectionUri), new PoolOptions()); - } - - /** - * Like {@link #client(Vertx, MySQLConnectOptions, PoolOptions)} with {@code connectOptions} build from {@code connectionUri}. - */ - static SqlClient client(Vertx vertx, String connectionUri, PoolOptions poolOptions) { - return client(vertx, MySQLConnectOptions.fromUri(connectionUri), poolOptions); - } - - /** - * Create a client backed by a connection pool to the database configured with the given {@code connectOptions} and {@code poolOptions}. - * - * @param poolOptions the options for creating the backing pool - * @return the client - */ - static SqlClient client(MySQLConnectOptions connectOptions, PoolOptions poolOptions) { - return client(null, SingletonSupplier.wrap(connectOptions), poolOptions); - } - - /** - * Like {@link #client(MySQLConnectOptions, PoolOptions)} with a specific {@link Vertx} instance. - */ - static SqlClient client(Vertx vertx, MySQLConnectOptions connectOptions, PoolOptions poolOptions) { - return client(vertx, SingletonSupplier.wrap(connectOptions), poolOptions); - } - - /** - * Like {@link #client(List, PoolOptions)} with a specific {@link Vertx} instance. - */ - static SqlClient client(Vertx vertx, List mySQLConnectOptions, PoolOptions options) { - return MySQLDriver.INSTANCE.createPool(vertx, Utils.roundRobinSupplier(mySQLConnectOptions), new MySQLPoolOptions(options).setPipelined(true), new NetClientOptions()); - } - - /** - * Create a client backed by a connection pool to the MySQL {@code databases} with round-robin selection. - * Round-robin is applied when a new connection is created by the pool. - * - * @param databases the list of databases - * @param options the options for creating the pool - * @return the pooled client - */ - static SqlClient client(List databases, PoolOptions options) { - return client(null, databases, options); - } - - /** - * Like {@link #client(Supplier, PoolOptions)} with a specific {@link Vertx} instance. - */ - static SqlClient client(Vertx vertx, Supplier> mySQLConnectOptions, PoolOptions options) { - return MySQLDriver.INSTANCE.createPool(vertx, mySQLConnectOptions, new MySQLPoolOptions(options).setPipelined(true), new NetClientOptions()); - } - - /** - * Create a client backed by a connection pool to the MySQL {@code databases}. The supplier is called - * to provide the options when a new connection is created by the pool. - * - * @param databases the databases supplier - * @param options the options for creating the pool - * @return the pooled client - */ - static SqlClient client(Supplier> databases, PoolOptions options) { - return client(null, databases, options); - } - - @Override - MySQLPool connectHandler(Handler handler); - - @Fluent - MySQLPool connectionProvider(Function> provider); -} diff --git a/vertx-mysql-client/src/main/java/io/vertx/mysqlclient/impl/MySQLPoolImpl.java b/vertx-mysql-client/src/main/java/io/vertx/mysqlclient/impl/MySQLPoolImpl.java deleted file mode 100644 index a5f25437f..000000000 --- a/vertx-mysql-client/src/main/java/io/vertx/mysqlclient/impl/MySQLPoolImpl.java +++ /dev/null @@ -1,26 +0,0 @@ -/* - * Copyright (c) 2011-2019 Contributors to the Eclipse Foundation - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 - * which is available at https://www.apache.org/licenses/LICENSE-2.0. - * - * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 - */ - -package io.vertx.mysqlclient.impl; - -import io.vertx.core.impl.CloseFuture; -import io.vertx.core.impl.VertxInternal; - -import io.vertx.mysqlclient.MySQLBuilder; -import io.vertx.sqlclient.Pool; -import io.vertx.sqlclient.impl.PoolBase; - -public class MySQLPoolImpl extends PoolBase implements MySQLBuilder { - - public MySQLPoolImpl(VertxInternal vertx, CloseFuture closeFuture, Pool delegate) { - super(vertx, closeFuture, delegate); - } -} diff --git a/vertx-mysql-client/src/main/java/io/vertx/mysqlclient/spi/MySQLDriver.java b/vertx-mysql-client/src/main/java/io/vertx/mysqlclient/spi/MySQLDriver.java index bc63c79a6..140528e8a 100644 --- a/vertx-mysql-client/src/main/java/io/vertx/mysqlclient/spi/MySQLDriver.java +++ b/vertx-mysql-client/src/main/java/io/vertx/mysqlclient/spi/MySQLDriver.java @@ -16,6 +16,7 @@ package io.vertx.mysqlclient.spi; import io.vertx.core.Future; +import io.vertx.core.Handler; import io.vertx.core.Vertx; import io.vertx.core.impl.CloseFuture; import io.vertx.core.impl.ContextInternal; @@ -27,7 +28,9 @@ import io.vertx.sqlclient.Pool; import io.vertx.sqlclient.PoolOptions; import io.vertx.sqlclient.SqlConnectOptions; +import io.vertx.sqlclient.SqlConnection; import io.vertx.sqlclient.impl.Connection; +import io.vertx.sqlclient.impl.CloseablePool; import io.vertx.sqlclient.impl.PoolImpl; import io.vertx.sqlclient.impl.SqlConnectionInternal; import io.vertx.sqlclient.spi.ConnectionFactory; @@ -47,22 +50,21 @@ public MySQLConnectOptions downcast(SqlConnectOptions connectOptions) { } @Override - public Pool newPool(Vertx vertx, Supplier> databases, PoolOptions options, NetClientOptions transportOptions, CloseFuture closeFuture) { + public Pool newPool(Vertx vertx, Supplier> databases, PoolOptions options, NetClientOptions transportOptions, Handler connectHandler, CloseFuture closeFuture) { VertxInternal vx = (VertxInternal) vertx; PoolImpl pool; if (options.isShared()) { - pool = vx.createSharedResource(SHARED_CLIENT_KEY, options.getName(), closeFuture, cf -> newPoolImpl(vx, databases, options, transportOptions, cf)); + pool = vx.createSharedResource(SHARED_CLIENT_KEY, options.getName(), closeFuture, cf -> newPoolImpl(vx, connectHandler, databases, options, transportOptions, cf)); } else { - pool = newPoolImpl(vx, databases, options, transportOptions, closeFuture); + pool = newPoolImpl(vx, connectHandler, databases, options, transportOptions, closeFuture); } - return new MySQLPoolImpl(vx, closeFuture, pool); + return new CloseablePool<>(vx, closeFuture, pool); } - private PoolImpl newPoolImpl(VertxInternal vertx, Supplier> databases, PoolOptions poolOptions, NetClientOptions transportOptions, CloseFuture closeFuture) { + private PoolImpl newPoolImpl(VertxInternal vertx, Handler connectHandler, Supplier> databases, PoolOptions poolOptions, NetClientOptions transportOptions, CloseFuture closeFuture) { boolean pipelinedPool = poolOptions instanceof MySQLPoolOptions && ((MySQLPoolOptions) poolOptions).isPipelined(); - PoolImpl pool = new PoolImpl(vertx, this, pipelinedPool, poolOptions, null, null, closeFuture); ConnectionFactory factory = createConnectionFactory(vertx, transportOptions); - pool.connectionProvider(context -> factory.connect(context, databases.get())); + PoolImpl pool = new PoolImpl(vertx, this, pipelinedPool, poolOptions, null, null, context -> factory.connect(context, databases.get()), connectHandler, closeFuture); pool.init(); closeFuture.add(factory); return pool; diff --git a/vertx-oracle-client/src/main/java/io/vertx/oracleclient/OraclePool.java b/vertx-oracle-client/src/main/java/io/vertx/oracleclient/OraclePool.java deleted file mode 100644 index 257436160..000000000 --- a/vertx-oracle-client/src/main/java/io/vertx/oracleclient/OraclePool.java +++ /dev/null @@ -1,85 +0,0 @@ -/* - * Copyright (c) 2011-2022 Contributors to the Eclipse Foundation - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 - * which is available at https://www.apache.org/licenses/LICENSE-2.0. - * - * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 - */ -package io.vertx.oracleclient; - -import io.vertx.codegen.annotations.VertxGen; -import io.vertx.core.Context; -import io.vertx.core.Future; -import io.vertx.core.Handler; -import io.vertx.core.Vertx; -import io.vertx.core.net.NetClientOptions; -import io.vertx.oracleclient.spi.OracleDriver; -import io.vertx.sqlclient.Pool; -import io.vertx.sqlclient.PoolOptions; -import io.vertx.sqlclient.SqlConnection; -import io.vertx.sqlclient.impl.Utils; - -import java.util.Collections; -import java.util.function.Function; -import java.util.function.Supplier; - -/** - * Represents a pool of connection to interact with an Oracle database. - */ -@VertxGen -public interface OraclePool extends Pool { - - static OraclePool pool(OracleConnectOptions connectOptions, PoolOptions poolOptions) { - return pool(null, connectOptions, poolOptions); - } - - /** - * Like {@link #pool(OracleConnectOptions, PoolOptions)} with a specific {@link Vertx} instance. - */ - static OraclePool pool(Vertx vertx, OracleConnectOptions connectOptions, PoolOptions poolOptions) { - return (OraclePool) OracleDriver.INSTANCE.createPool(vertx, Utils.singletonSupplier(connectOptions), poolOptions, new NetClientOptions()); - } - - /** - * Like {@link #pool(OracleConnectOptions, PoolOptions)} but connection options are created from the provided {@code connectionUri}. - */ - static OraclePool pool(String connectionUri, PoolOptions poolOptions) { - return pool(OracleConnectOptions.fromUri(connectionUri), poolOptions); - } - - /** - * Like {@link #pool(String, PoolOptions)} with a specific {@link Vertx} instance. - */ - static OraclePool pool(Vertx vertx, String connectionUri, PoolOptions poolOptions) { - return pool(vertx, OracleConnectOptions.fromUri(connectionUri), poolOptions); - } - - /** - * Create a connection pool to the Oracle {@code databases}. The supplier is called - * to provide the options when a new connection is created by the pool. - * - * @param databases the databases supplier - * @param poolOptions the options for creating the pool - * @return the connection pool - */ - static OraclePool pool(Supplier> databases, PoolOptions poolOptions) { - return pool(null, databases, poolOptions); - } - - - /** - * Like {@link #pool(Supplier, PoolOptions)} with a specific {@link Vertx} instance. - */ - static OraclePool pool(Vertx vertx, Supplier> databases, PoolOptions poolOptions) { - return (OraclePool) OracleDriver.INSTANCE.createPool(vertx, databases, poolOptions, new NetClientOptions()); - } - - @Override - OraclePool connectHandler(Handler handler); - - @Override - OraclePool connectionProvider(Function> provider); -} diff --git a/vertx-oracle-client/src/main/java/io/vertx/oracleclient/impl/OraclePoolImpl.java b/vertx-oracle-client/src/main/java/io/vertx/oracleclient/impl/OraclePoolImpl.java deleted file mode 100644 index cc0f03e1b..000000000 --- a/vertx-oracle-client/src/main/java/io/vertx/oracleclient/impl/OraclePoolImpl.java +++ /dev/null @@ -1,24 +0,0 @@ -/* - * Copyright (c) 2011-2022 Contributors to the Eclipse Foundation - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 - * which is available at https://www.apache.org/licenses/LICENSE-2.0. - * - * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 - */ -package io.vertx.oracleclient.impl; - -import io.vertx.core.impl.CloseFuture; -import io.vertx.core.impl.VertxInternal; -import io.vertx.oracleclient.OraclePool; -import io.vertx.sqlclient.Pool; -import io.vertx.sqlclient.impl.PoolBase; - -public class OraclePoolImpl extends PoolBase implements OraclePool { - - public OraclePoolImpl(VertxInternal vertx, CloseFuture closeFuture, Pool delegate) { - super(vertx, closeFuture, delegate); - } -} diff --git a/vertx-oracle-client/src/main/java/io/vertx/oracleclient/spi/OracleDriver.java b/vertx-oracle-client/src/main/java/io/vertx/oracleclient/spi/OracleDriver.java index 534c59aa5..96a0a77c7 100644 --- a/vertx-oracle-client/src/main/java/io/vertx/oracleclient/spi/OracleDriver.java +++ b/vertx-oracle-client/src/main/java/io/vertx/oracleclient/spi/OracleDriver.java @@ -11,6 +11,7 @@ package io.vertx.oracleclient.spi; import io.vertx.core.Future; +import io.vertx.core.Handler; import io.vertx.core.Vertx; import io.vertx.core.impl.CloseFuture; import io.vertx.core.impl.ContextInternal; @@ -22,7 +23,9 @@ import io.vertx.sqlclient.Pool; import io.vertx.sqlclient.PoolOptions; import io.vertx.sqlclient.SqlConnectOptions; +import io.vertx.sqlclient.SqlConnection; import io.vertx.sqlclient.impl.Connection; +import io.vertx.sqlclient.impl.CloseablePool; import io.vertx.sqlclient.impl.PoolImpl; import io.vertx.sqlclient.impl.SqlConnectionInternal; import io.vertx.sqlclient.spi.ConnectionFactory; @@ -43,23 +46,22 @@ public OracleConnectOptions downcast(SqlConnectOptions connectOptions) { } @Override - public Pool newPool(Vertx vertx, Supplier> databases, PoolOptions options, NetClientOptions transportOptions, CloseFuture closeFuture) { + public Pool newPool(Vertx vertx, Supplier> databases, PoolOptions options, NetClientOptions transportOptions, Handler connectHandler, CloseFuture closeFuture) { VertxInternal vx = (VertxInternal) vertx; PoolImpl pool; if (options.isShared()) { - pool = vx.createSharedResource(SHARED_CLIENT_KEY, options.getName(), closeFuture, cf -> newPoolImpl(vx, databases, options, cf)); + pool = vx.createSharedResource(SHARED_CLIENT_KEY, options.getName(), closeFuture, cf -> newPoolImpl(vx, connectHandler, databases, options, cf)); } else { - pool = newPoolImpl(vx, databases, options, closeFuture); + pool = newPoolImpl(vx, connectHandler, databases, options, closeFuture); } - return new OraclePoolImpl(vx, closeFuture, pool); + return new CloseablePool<>(vx, closeFuture, pool); } - private PoolImpl newPoolImpl(VertxInternal vertx, Supplier> databases, PoolOptions options, CloseFuture closeFuture) { + private PoolImpl newPoolImpl(VertxInternal vertx, Handler connectHandler, Supplier> databases, PoolOptions options, CloseFuture closeFuture) { Function> afterAcquire = conn -> ((OracleJdbcConnection) conn).afterAcquire(); Function> beforeRecycle = conn -> ((OracleJdbcConnection) conn).beforeRecycle(); - PoolImpl pool = new PoolImpl(vertx, this, false, options, afterAcquire, beforeRecycle, closeFuture); ConnectionFactory factory = createConnectionFactory(vertx, null); - pool.connectionProvider(context -> factory.connect(context, databases.get())); + PoolImpl pool = new PoolImpl(vertx, this, false, options, afterAcquire, beforeRecycle, context -> factory.connect(context, databases.get()), connectHandler, closeFuture); pool.init(); closeFuture.add(factory); return pool; diff --git a/vertx-pg-client/src/main/java/io/vertx/pgclient/PgBuilder.java b/vertx-pg-client/src/main/java/io/vertx/pgclient/PgBuilder.java index 93ffeaafd..041030ee7 100644 --- a/vertx-pg-client/src/main/java/io/vertx/pgclient/PgBuilder.java +++ b/vertx-pg-client/src/main/java/io/vertx/pgclient/PgBuilder.java @@ -85,8 +85,8 @@ public ClientBuilder with(PoolOptions options) { return super.with(options); } @Override - protected SqlClient create(Vertx vertx, Supplier> databases, PoolOptions poolOptions, NetClientOptions transportOptions) { - return driver.createPool(vertx, databases, poolOptions, transportOptions); + protected SqlClient create(Vertx vertx, Supplier> databases, PoolOptions poolOptions, NetClientOptions transportOptions, Handler connectHandler) { + return driver.createPool(vertx, databases, poolOptions, transportOptions, connectHandler); } }; } diff --git a/vertx-pg-client/src/main/java/io/vertx/pgclient/PgPool.java b/vertx-pg-client/src/main/java/io/vertx/pgclient/PgPool.java deleted file mode 100644 index 77853ae91..000000000 --- a/vertx-pg-client/src/main/java/io/vertx/pgclient/PgPool.java +++ /dev/null @@ -1,266 +0,0 @@ -/* - * Copyright (C) 2017 Julien Viet - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -package io.vertx.pgclient; - -import io.vertx.codegen.annotations.Fluent; -import io.vertx.core.Context; -import io.vertx.core.Future; -import io.vertx.core.Handler; -import io.vertx.pgclient.impl.PgPoolOptions; -import io.vertx.sqlclient.*; -import io.vertx.codegen.annotations.VertxGen; -import io.vertx.core.Vertx; -import io.vertx.sqlclient.impl.SingletonSupplier; - -import java.util.List; -import java.util.function.Function; -import java.util.function.Supplier; -import java.util.stream.Collectors; - -/** - * A {@link Pool pool} of {@link PgConnection PostgreSQL connections}. - * - * @author Julien Viet - */ -@Deprecated -@VertxGen -public interface PgPool extends Pool { - - /** - * Like {@link #pool(PoolOptions)} with a default {@code poolOptions}. - */ - static Pool pool() { - return pool(new PoolOptions()); - } - - /** - * Like {@link #pool(PgConnectOptions, PoolOptions)} with {@code connectOptions} build from the environment variables. - */ - static Pool pool(PoolOptions options) { - return PgBuilder.pool().connectingTo(PgConnectOptions.fromEnv()).with(options).build(); - } - - /** - * Like {@link #pool(String, PoolOptions)} with a default {@code poolOptions}. - */ - static Pool pool(String connectionUri) { - return pool(connectionUri, new PoolOptions()); - } - - /** - * Like {@link #pool(PgConnectOptions, PoolOptions)} with {@code connectOptions} build from {@code connectionUri}. - */ - static Pool pool(String connectionUri, PoolOptions options) { - return pool(PgConnectOptions.fromUri(connectionUri), options); - } - - /** - * Like {@link #pool(Vertx, String,PoolOptions)} with default options. - */ - static Pool pool(Vertx vertx, String connectionUri) { - return pool(vertx, PgConnectOptions.fromUri(connectionUri), new PoolOptions()); - } - - /** - * Like {@link #pool(Vertx, PgConnectOptions, PoolOptions)} with the {@code database} retrieved from the environment variables. - */ - static Pool pool(Vertx vertx, PoolOptions options) { - return pool(vertx, PgConnectOptions.fromEnv(), options); - } - - /** - * Like {@link #pool(Vertx, PgConnectOptions, PoolOptions)} with {@code database} retrieved from the given {@code connectionUri}. - */ - static Pool pool(Vertx vertx, String connectionUri, PoolOptions poolOptions) { - return pool(vertx, PgConnectOptions.fromUri(connectionUri), poolOptions); - } - - /** - * Create a connection pool to the PostgreSQL {@code database} configured with the given {@code options}. - * - * @param database the database - * @param options the options for creating the pool - * @return the connection pool - */ - static Pool pool(PgConnectOptions database, PoolOptions options) { - return pool(null, database, options); - } - - /** - * Like {@link #pool(PgConnectOptions, PoolOptions)} with a specific {@link Vertx} instance. - */ - static Pool pool(Vertx vertx, PgConnectOptions database, PoolOptions options) { - return pool(vertx, SingletonSupplier.wrap(database), options); - } - - /** - * Create a connection pool to the PostgreSQL {@code databases} with round-robin selection. - * Round-robin is applied when a new connection is created by the pool. - * - * @param databases the list of databases - * @param poolOptions the options for creating the pool - * @return the connection pool - */ - static Pool pool(List databases, PoolOptions poolOptions) { - return pool(null, databases, poolOptions); - } - - /** - * Like {@link #pool(List, PoolOptions)} with a specific {@link Vertx} instance. - */ - static Pool pool(Vertx vertx, List databases, PoolOptions poolOptions) { - return PgBuilder - .pool() - .connectingTo(databases.stream().map(SqlConnectOptions.class::cast).collect(Collectors.toList())) - .with(poolOptions) - .using(vertx) - .build(); - } - - /** - * Create a connection pool to the PostgreSQL {@code databases}. The supplier is called - * to provide the options when a new connection is created by the pool. - * - * @param databases the databases supplier - * @param poolOptions the options for creating the pool - * @return the connection pool - */ - static Pool pool(Supplier> databases, PoolOptions poolOptions) { - return pool(null, databases, poolOptions); - } - - /** - * Like {@link #pool(Supplier, PoolOptions)} with a specific {@link Vertx} instance. - */ - static Pool pool(Vertx vertx, Supplier> databases, PoolOptions poolOptions) { - return PgBuilder.pool().connectingTo(() -> databases.get().map(c -> c)).with(poolOptions).using(vertx).build(); - } - - /** - * Like {@link #client(PoolOptions)} with default options. - */ - static SqlClient client() { - return client(PgConnectOptions.fromEnv(), new PoolOptions()); - } - - /** - * Like {@link #client(PgConnectOptions, PoolOptions)} with {@code database} retrieved from the environment variables. - */ - static SqlClient client(PoolOptions options) { - return client(PgConnectOptions.fromEnv(), options); - } - - /** - * Like {@link #pool(String, PoolOptions)} with default options. - */ - static SqlClient client(String connectionUri) { - return client(connectionUri, new PoolOptions()); - } - - /** - * Like {@link #client(PgConnectOptions, PoolOptions)} with {@code database} retrieved from the {@code connectionUri}. - */ - static SqlClient client(String connectionUri, PoolOptions options) { - return client(PgConnectOptions.fromUri(connectionUri), options); - } - - /** - * Like {@link #client(Vertx, String,PoolOptions)} with default options. - */ - static SqlClient client(Vertx vertx, String connectionUri) { - return client(vertx, PgConnectOptions.fromUri(connectionUri), new PoolOptions()); - } - - /** - * Like {@link #client(Vertx, PgConnectOptions, PoolOptions)} with {@code database} retrieved from the environment variables. - */ - static SqlClient client(Vertx vertx, PoolOptions poolOptions) { - return client(vertx, PgConnectOptions.fromEnv(), poolOptions); - } - - /** - * Like {@link #client(Vertx, PgConnectOptions, PoolOptions)} with {@code database} build from {@code connectionUri}. - */ - static SqlClient client(Vertx vertx, String connectionUri, PoolOptions options) { - return client(vertx, PgConnectOptions.fromUri(connectionUri), options); - } - - /** - * Create a client backed by a connection pool to the PostgreSQL {@code database} configured with the given {@code options}. - * - * @param options the options for creating the backing pool - * @return the pooled client - */ - static SqlClient client(PgConnectOptions database, PoolOptions options) { - return client(null, database, options); - } - - /** - * Like {@link #client(PgConnectOptions, PoolOptions)} with a specific {@link Vertx} instance. - */ - static SqlClient client(Vertx vertx, PgConnectOptions database, PoolOptions options) { - return client(vertx, SingletonSupplier.wrap(database), options); - } - - /** - * Like {@link #client(List, PoolOptions)} with a specific {@link Vertx} instance. - */ - static SqlClient client(Vertx vertx, List databases, PoolOptions options) { - return PgBuilder.pool(b -> b - .connectingTo(databases.stream().map(SqlConnectOptions.class::cast).collect(Collectors.toList())) - .with(new PgPoolOptions(options).setPipelined(true)) - .using(vertx)); - } - - /** - * Create a client backed by a connection pool to the PostgreSQL {@code databases} with round-robin selection. - * Round-robin is applied when a new connection is created by the pool. - * - * @param databases the list of databases - * @param options the options for creating the pool - * @return the pooled client - */ - static SqlClient client(List databases, PoolOptions options) { - return client(null, databases, options); - } - - /** - * Like {@link #client(Supplier, PoolOptions)} with a specific {@link Vertx} instance. - */ - static SqlClient client(Vertx vertx, Supplier> databases, PoolOptions options) { - return PgBuilder.pool().connectingTo(() -> databases.get().map(c -> c)).with(new PgPoolOptions(options).setPipelined(true)).using(vertx).build(); - } - - /** - * Create a client backed by a connection pool to the PostgreSQL {@code databases}. The supplier is called - * to provide the options when a new connection is created by the pool. - * - * @param databases the databases supplier - * @param options the options for creating the pool - * @return the pooled client - */ - static SqlClient client(Supplier> databases, PoolOptions options) { - return client(null, databases, options); - } - - @Override - PgPool connectHandler(Handler handler); - - @Fluent - PgPool connectionProvider(Function> provider); -} diff --git a/vertx-pg-client/src/main/java/io/vertx/pgclient/impl/PgPoolImpl.java b/vertx-pg-client/src/main/java/io/vertx/pgclient/impl/PgPoolImpl.java deleted file mode 100644 index eede2ebf9..000000000 --- a/vertx-pg-client/src/main/java/io/vertx/pgclient/impl/PgPoolImpl.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Copyright (C) 2017 Julien Viet - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -package io.vertx.pgclient.impl; - -import io.vertx.core.impl.CloseFuture; -import io.vertx.core.impl.VertxInternal; -import io.vertx.pgclient.*; -import io.vertx.sqlclient.Pool; -import io.vertx.sqlclient.impl.PoolBase; - -/** - * Todo : - * - * - handle timeout when acquiring a connection - * - for per statement pooling, have several physical connection and use the less busy one to avoid head of line blocking effect - * - * @author Julien Viet - * @author Emad Alblueshi - */ -public class PgPoolImpl extends PoolBase implements PgPool { - - public PgPoolImpl(VertxInternal vertx, CloseFuture closeFuture, Pool delegate) { - super(vertx, closeFuture, delegate); - } -} diff --git a/vertx-pg-client/src/main/java/io/vertx/pgclient/spi/PgDriver.java b/vertx-pg-client/src/main/java/io/vertx/pgclient/spi/PgDriver.java index 04aef4b1b..a9a944c0d 100644 --- a/vertx-pg-client/src/main/java/io/vertx/pgclient/spi/PgDriver.java +++ b/vertx-pg-client/src/main/java/io/vertx/pgclient/spi/PgDriver.java @@ -1,6 +1,8 @@ package io.vertx.pgclient.spi; +import io.vertx.core.Context; import io.vertx.core.Future; +import io.vertx.core.Handler; import io.vertx.core.Vertx; import io.vertx.core.impl.CloseFuture; import io.vertx.core.impl.ContextInternal; @@ -12,12 +14,15 @@ import io.vertx.sqlclient.Pool; import io.vertx.sqlclient.PoolOptions; import io.vertx.sqlclient.SqlConnectOptions; +import io.vertx.sqlclient.SqlConnection; import io.vertx.sqlclient.impl.Connection; +import io.vertx.sqlclient.impl.CloseablePool; import io.vertx.sqlclient.impl.PoolImpl; import io.vertx.sqlclient.impl.SqlConnectionInternal; import io.vertx.sqlclient.spi.ConnectionFactory; import io.vertx.sqlclient.spi.Driver; +import java.util.function.Function; import java.util.function.Supplier; public class PgDriver implements Driver { @@ -27,22 +32,21 @@ public class PgDriver implements Driver { public static final PgDriver INSTANCE = new PgDriver(); @Override - public Pool newPool(Vertx vertx, Supplier> databases, PoolOptions poolOptions, NetClientOptions transportOptions, CloseFuture closeFuture) { + public Pool newPool(Vertx vertx, Supplier> databases, PoolOptions poolOptions, NetClientOptions transportOptions, Handler connectHandler, CloseFuture closeFuture) { VertxInternal vx = (VertxInternal) vertx; PoolImpl pool; if (poolOptions.isShared()) { - pool = vx.createSharedResource(SHARED_CLIENT_KEY, poolOptions.getName(), closeFuture, cf -> newPoolImpl(vx, databases, poolOptions, transportOptions, cf)); + pool = vx.createSharedResource(SHARED_CLIENT_KEY, poolOptions.getName(), closeFuture, cf -> newPoolImpl(vx, connectHandler, databases, poolOptions, transportOptions, cf)); } else { - pool = newPoolImpl(vx, databases, poolOptions, transportOptions, closeFuture); + pool = newPoolImpl(vx, connectHandler, databases, poolOptions, transportOptions, closeFuture); } - return new PgPoolImpl(vx, closeFuture, pool); + return new CloseablePool(vx, closeFuture, pool); } - private PoolImpl newPoolImpl(VertxInternal vertx, Supplier> databases, PoolOptions poolOptions, NetClientOptions transportOptions, CloseFuture closeFuture) { + private PoolImpl newPoolImpl(VertxInternal vertx, Handler connectHandler, Supplier> databases, PoolOptions poolOptions, NetClientOptions transportOptions, CloseFuture closeFuture) { boolean pipelinedPool = poolOptions instanceof PgPoolOptions && ((PgPoolOptions) poolOptions).isPipelined(); - PoolImpl pool = new PoolImpl(vertx, this, pipelinedPool, poolOptions, null, null, closeFuture); ConnectionFactory factory = createConnectionFactory(vertx, transportOptions); - pool.connectionProvider(context -> factory.connect(context, databases.get())); + PoolImpl pool = new PoolImpl(vertx, this, pipelinedPool, poolOptions, null, null, context -> factory.connect(context, databases.get()), connectHandler, closeFuture); pool.init(); closeFuture.add(factory); return pool; diff --git a/vertx-pg-client/src/test/java/io/vertx/pgclient/CloseConnectionTest.java b/vertx-pg-client/src/test/java/io/vertx/pgclient/CloseConnectionTest.java index 304ec59b4..98c9b1662 100644 --- a/vertx-pg-client/src/test/java/io/vertx/pgclient/CloseConnectionTest.java +++ b/vertx-pg-client/src/test/java/io/vertx/pgclient/CloseConnectionTest.java @@ -1,26 +1,17 @@ package io.vertx.pgclient; import io.netty.buffer.ByteBufUtil; -import io.vertx.core.Context; -import io.vertx.core.Future; -import io.vertx.core.Promise; import io.vertx.core.Vertx; import io.vertx.core.buffer.Buffer; -import io.vertx.core.net.NetClientOptions; import io.vertx.ext.unit.Async; import io.vertx.ext.unit.TestContext; -import io.vertx.pgclient.spi.PgDriver; import io.vertx.sqlclient.Pool; import io.vertx.sqlclient.PoolOptions; import io.vertx.sqlclient.ProxyServer; -import io.vertx.sqlclient.SqlConnection; -import io.vertx.sqlclient.spi.ConnectionFactory; import org.junit.After; import org.junit.Before; import org.junit.Test; -import java.util.function.Function; - public class CloseConnectionTest extends PgTestBase { protected Vertx vertx; @@ -57,25 +48,6 @@ public void testClosePooledConnection(TestContext ctx) { }); } - @Test - public void testCloseNetSocket(TestContext ctx) { - testCloseConnection(ctx, () -> { - Pool pool = PgBuilder.pool().connectingTo(options).with(new PoolOptions().setMaxSize(1)).using(vertx).build(); - ConnectionFactory factory = PgDriver.INSTANCE.createConnectionFactory(vertx, new NetClientOptions()); - pool.connectionProvider(new Function>() { - @Override - public Future apply(Context context) { - return factory.connect(context, options); - } - }); - pool.getConnection().onComplete(ctx.asyncAssertSuccess(conn -> { - conn.close().onComplete(ctx.asyncAssertSuccess(v -> { - factory.close(Promise.promise()); - })); - })); - }); - } - private void testCloseConnection(TestContext ctx, Runnable test) { Async async = ctx.async(); ProxyServer proxy = ProxyServer.create(vertx, options.getPort(), options.getHost()); diff --git a/vertx-pg-client/src/test/java/io/vertx/pgclient/PgPoolTest.java b/vertx-pg-client/src/test/java/io/vertx/pgclient/PgPoolTest.java index 50962dab5..4987733c6 100644 --- a/vertx-pg-client/src/test/java/io/vertx/pgclient/PgPoolTest.java +++ b/vertx-pg-client/src/test/java/io/vertx/pgclient/PgPoolTest.java @@ -18,20 +18,16 @@ package io.vertx.pgclient; import io.netty.channel.EventLoop; -import io.vertx.core.Future; import io.vertx.core.Handler; import io.vertx.core.VertxOptions; import io.vertx.core.impl.ContextInternal; -import io.vertx.core.net.NetClientOptions; import io.vertx.ext.unit.Async; import io.vertx.ext.unit.TestContext; import io.vertx.ext.unit.junit.Repeat; import io.vertx.ext.unit.junit.RepeatRule; import io.vertx.pgclient.impl.PgSocketConnection; -import io.vertx.pgclient.spi.PgDriver; import io.vertx.sqlclient.*; import io.vertx.sqlclient.impl.SqlConnectionInternal; -import io.vertx.sqlclient.spi.ConnectionFactory; import org.junit.Rule; import org.junit.Test; @@ -619,48 +615,4 @@ public void testConnectionClosedInHook(TestContext ctx) { })); })); } - - @Test - public void testConnectionClosedInProvider1(TestContext ctx) { - testConnectionClosedInProvider(ctx, true); - } - - @Test - public void testConnectionClosedInProvider2(TestContext ctx) { - testConnectionClosedInProvider(ctx, false); - } - - private void testConnectionClosedInProvider(TestContext ctx, boolean immediately) { - Async async = ctx.async(2); - ProxyServer proxy = ProxyServer.create(vertx, options.getPort(), options.getHost()); - AtomicReference proxyConn = new AtomicReference<>(); - proxy.proxyHandler(conn -> { - proxyConn.set(conn); - conn.connect(); - }); - proxy.listen(8080, "localhost", ctx.asyncAssertSuccess(v1 -> { - PgConnectOptions options = new PgConnectOptions(this.options).setPort(8080).setHost("localhost"); - ConnectionFactory factory = PgDriver.INSTANCE.createConnectionFactory(vertx, new NetClientOptions()); - Pool pool = createPool(options, new PoolOptions().setMaxSize(1)); - pool.connectionProvider(context -> { - Future fut = factory.connect(context, options); - if (immediately) { - return fut.map(conn -> { - conn.close(); - return conn; - }); - } else { - return fut.flatMap(conn -> conn.close().map(conn)); - } - }); - pool - .getConnection() - .onComplete(ctx.asyncAssertFailure(conn -> { - vertx.runOnContext(v -> { - ctx.assertEquals(0, pool.size()); - async.complete(); - }); - })); - })); - } } diff --git a/vertx-pg-client/src/test/java/io/vertx/pgclient/PoolMultiTest.java b/vertx-pg-client/src/test/java/io/vertx/pgclient/PoolMultiTest.java index c25b09d29..68380782f 100644 --- a/vertx-pg-client/src/test/java/io/vertx/pgclient/PoolMultiTest.java +++ b/vertx-pg-client/src/test/java/io/vertx/pgclient/PoolMultiTest.java @@ -11,6 +11,7 @@ import io.vertx.pgclient.spi.PgDriver; import io.vertx.sqlclient.Pool; import io.vertx.sqlclient.PoolOptions; +import io.vertx.sqlclient.SqlConnectOptions; import io.vertx.sqlclient.SqlConnection; import io.vertx.sqlclient.spi.ConnectionFactory; import org.junit.After; @@ -24,6 +25,7 @@ import java.util.Collections; import java.util.List; import java.util.function.Function; +import java.util.function.Supplier; import static org.junit.Assert.assertEquals; @@ -55,15 +57,18 @@ public void testListLoadBalancing(TestContext ctx) { @Test public void testAsyncLoadBalancing(TestContext ctx) { - Pool pool = PgBuilder.pool().with(new PoolOptions().setMaxSize(5)).using(vertx).build(); - ConnectionFactory provider = PgDriver.INSTANCE.createConnectionFactory(vertx, new NetClientOptions()); - pool.connectionProvider(new Function>() { - int idx = 0; - @Override - public Future apply(Context context) { - return provider.connect(context, idx++ % 2 == 0 ? db1.options() : db2.options()); - } - }); + Pool pool = PgBuilder.pool() + .with(new PoolOptions().setMaxSize(5)) + .connectingTo(new Supplier>() { + int idx = 0; + @Override + public Future get() { + SqlConnectOptions connectOptions = idx++ % 2 == 0 ? db1.options() : db2.options(); + return Future.succeededFuture(connectOptions); + } + }) + .using(vertx) + .build(); testLoadBalancing(ctx, pool); } diff --git a/vertx-sql-client-templates/src/test/java/io/vertx/sqlclient/templates/TemplateBuilderTest.java b/vertx-sql-client-templates/src/test/java/io/vertx/sqlclient/templates/TemplateBuilderTest.java index 2edcf7d48..3eed21ff8 100644 --- a/vertx-sql-client-templates/src/test/java/io/vertx/sqlclient/templates/TemplateBuilderTest.java +++ b/vertx-sql-client-templates/src/test/java/io/vertx/sqlclient/templates/TemplateBuilderTest.java @@ -5,16 +5,7 @@ import io.vertx.core.Vertx; import io.vertx.core.impl.CloseFuture; import io.vertx.core.net.NetClientOptions; -import io.vertx.sqlclient.Pool; -import io.vertx.sqlclient.PoolOptions; -import io.vertx.sqlclient.PrepareOptions; -import io.vertx.sqlclient.PreparedQuery; -import io.vertx.sqlclient.Query; -import io.vertx.sqlclient.Row; -import io.vertx.sqlclient.RowSet; -import io.vertx.sqlclient.SqlClient; -import io.vertx.sqlclient.SqlConnectOptions; -import io.vertx.sqlclient.Tuple; +import io.vertx.sqlclient.*; import io.vertx.sqlclient.impl.SqlClientInternal; import io.vertx.sqlclient.spi.ConnectionFactory; import io.vertx.sqlclient.spi.Driver; @@ -47,7 +38,7 @@ public int appendQueryPlaceholder(StringBuilder queryBuilder, int index, int cur return FakeClient.this.appendQueryPlaceholder(queryBuilder, index, current); } @Override - public Pool newPool(Vertx vertx, Supplier> databases, PoolOptions options, NetClientOptions transportOptions, CloseFuture closeFuture) { + public Pool newPool(Vertx vertx, Supplier> databases, PoolOptions options, NetClientOptions transportOptions, Handler connectHandler, CloseFuture closeFuture) { throw new UnsupportedOperationException(); } @Override diff --git a/vertx-sql-client/src/main/java/io/vertx/sqlclient/ClientBuilder.java b/vertx-sql-client/src/main/java/io/vertx/sqlclient/ClientBuilder.java index 210ca0f32..687b7457c 100644 --- a/vertx-sql-client/src/main/java/io/vertx/sqlclient/ClientBuilder.java +++ b/vertx-sql-client/src/main/java/io/vertx/sqlclient/ClientBuilder.java @@ -38,8 +38,8 @@ public interface ClientBuilder { static ClientBuilder pool(Driver driver) { return new ClientBuilderBase(driver) { @Override - protected Pool create(Vertx vertx, Supplier> databases, PoolOptions poolOptions, NetClientOptions transportOptions) { - return driver.createPool(vertx, databases, poolOptions, transportOptions); + protected Pool create(Vertx vertx, Supplier> databases, PoolOptions poolOptions, NetClientOptions transportOptions, Handler connectHandler) { + return driver.createPool(vertx, databases, poolOptions, transportOptions, connectHandler); } }; } diff --git a/vertx-sql-client/src/main/java/io/vertx/sqlclient/Pool.java b/vertx-sql-client/src/main/java/io/vertx/sqlclient/Pool.java index ffca6178f..b308ac4ed 100644 --- a/vertx-sql-client/src/main/java/io/vertx/sqlclient/Pool.java +++ b/vertx-sql-client/src/main/java/io/vertx/sqlclient/Pool.java @@ -18,17 +18,13 @@ package io.vertx.sqlclient; import java.util.ArrayList; -import java.util.Collections; import java.util.List; import java.util.ServiceConfigurationError; import java.util.ServiceLoader; -import io.vertx.codegen.annotations.Fluent; import io.vertx.codegen.annotations.Nullable; import io.vertx.codegen.annotations.VertxGen; -import io.vertx.core.Context; import io.vertx.core.Future; -import io.vertx.core.Handler; import io.vertx.core.Vertx; import io.vertx.core.impl.ContextInternal; import io.vertx.core.net.NetClientOptions; @@ -37,7 +33,6 @@ import io.vertx.sqlclient.spi.Driver; import java.util.function.Function; -import java.util.function.Supplier; import static io.vertx.sqlclient.impl.PoolImpl.startPropagatableConnection; @@ -89,7 +84,7 @@ static Pool pool(Vertx vertx, SqlConnectOptions database, PoolOptions options) { throw new ServiceConfigurationError("Multiple implementations of " + Driver.class + " found: " + candidates); } else { Driver driver = candidates.get(0); - return driver.createPool(vertx, Utils.singletonSupplier(driver.downcast(database)), options, new NetClientOptions()); + return driver.createPool(vertx, Utils.singletonSupplier(driver.downcast(database)), options, new NetClientOptions(), null); } } @@ -193,36 +188,6 @@ static Pool pool(Vertx vertx, SqlConnectOptions database, PoolOptions options) { return getConnection().flatMap(conn -> function.apply(conn).onComplete(ar -> conn.close())); } - /** - * Set an handler called when the pool has established a connection to the database. - * - *

This handler allows interactions with the database before the connection is added to the pool. - * - *

When the handler has finished, it must call {@link SqlConnection#close()} to release the connection - * to the pool. - * - * @param handler the handler - * @return a reference to this, so the API can be used fluently - * @deprecated instead use {@link ClientBuilder#withConnectHandler(Handler)} - */ - @Deprecated - @Fluent - Pool connectHandler(Handler handler); - - /** - * Replace the default pool connection provider, the new {@code provider} returns a future connection for a - * given {@link Context}. - * - *

A {@link io.vertx.sqlclient.spi.ConnectionFactory} can be used as connection provider. - * - * @param provider the new connection provider - * @return a reference to this, so the API can be used fluently - * @deprecated instead use {@link ClientBuilder#connectingTo(Supplier)} - */ - @Deprecated - @Fluent - Pool connectionProvider(Function> provider); - /** * @return the current pool size approximation */ diff --git a/vertx-sql-client/src/main/java/io/vertx/sqlclient/impl/ClientBuilderBase.java b/vertx-sql-client/src/main/java/io/vertx/sqlclient/impl/ClientBuilderBase.java index f9d67a041..a9a515b12 100644 --- a/vertx-sql-client/src/main/java/io/vertx/sqlclient/impl/ClientBuilderBase.java +++ b/vertx-sql-client/src/main/java/io/vertx/sqlclient/impl/ClientBuilderBase.java @@ -32,7 +32,7 @@ public abstract class ClientBuilderBase implements ClientBuilder { private PoolOptions poolOptions; private NetClientOptions transportOptions; private Supplier> database; - private Handler connectionHandler; + private Handler connectHandler; private Vertx vertx; public ClientBuilderBase(Driver driver) { @@ -74,7 +74,7 @@ public ClientBuilder connectingTo(List databases) { @Override public ClientBuilder withConnectHandler(Handler handler) { - this.connectionHandler = handler; + this.connectHandler = handler; return this; } @@ -94,13 +94,10 @@ public final C build() { if (transportOptions == null) { transportOptions = new NetClientOptions(); } - C c = create(vertx, database, poolOptions, transportOptions); - if (c instanceof Pool) { - ((Pool)c).connectHandler(connectionHandler); - } + C c = create(vertx, database, poolOptions, transportOptions, connectHandler); return c; } - protected abstract C create(Vertx vertx, Supplier> databases, PoolOptions poolOptions, NetClientOptions transportOptions); + protected abstract C create(Vertx vertx, Supplier> databases, PoolOptions poolOptions, NetClientOptions transportOptions, Handler connectHandler); } diff --git a/vertx-sql-client/src/main/java/io/vertx/sqlclient/impl/PoolBase.java b/vertx-sql-client/src/main/java/io/vertx/sqlclient/impl/CloseablePool.java similarity index 83% rename from vertx-sql-client/src/main/java/io/vertx/sqlclient/impl/PoolBase.java rename to vertx-sql-client/src/main/java/io/vertx/sqlclient/impl/CloseablePool.java index 414e8ea23..269a0eab6 100644 --- a/vertx-sql-client/src/main/java/io/vertx/sqlclient/impl/PoolBase.java +++ b/vertx-sql-client/src/main/java/io/vertx/sqlclient/impl/CloseablePool.java @@ -16,7 +16,6 @@ package io.vertx.sqlclient.impl; import io.vertx.codegen.annotations.Nullable; -import io.vertx.core.Context; import io.vertx.core.Future; import io.vertx.core.Handler; import io.vertx.core.impl.CloseFuture; @@ -28,13 +27,13 @@ import java.util.function.Function; -public class PoolBase

implements Pool, SqlClientInternal { +public class CloseablePool

implements Pool, SqlClientInternal { private final VertxInternal vertx; private final CloseFuture closeFuture; private final Pool delegate; - public PoolBase(VertxInternal vertx, CloseFuture closeFuture, Pool delegate) { + public CloseablePool(VertxInternal vertx, CloseFuture closeFuture, Pool delegate) { this.vertx = vertx; this.closeFuture = closeFuture; this.delegate = delegate; @@ -71,18 +70,6 @@ public PreparedQuery> preparedQuery(String sql) { return delegate.withTransaction(txPropagation, function); } - @Override - public P connectHandler(Handler handler) { - delegate.connectHandler(handler); - return (P) this; - } - - @Override - public P connectionProvider(Function> provider) { - delegate.connectionProvider(provider); - return (P) this; - } - @Override public int size() { return delegate.size(); diff --git a/vertx-sql-client/src/main/java/io/vertx/sqlclient/impl/PoolImpl.java b/vertx-sql-client/src/main/java/io/vertx/sqlclient/impl/PoolImpl.java index 7b28da062..8803dc46f 100644 --- a/vertx-sql-client/src/main/java/io/vertx/sqlclient/impl/PoolImpl.java +++ b/vertx-sql-client/src/main/java/io/vertx/sqlclient/impl/PoolImpl.java @@ -46,9 +46,8 @@ public class PoolImpl extends SqlClientBase implements Pool, Closeable { private final long maxLifetime; private final long cleanerPeriod; private final boolean pipelined; - private volatile Handler connectionInitializer; + private final Handler connectionInitializer; private long timerID; - private volatile Function> connectionProvider; public static final String PROPAGATABLE_CONNECTION = "propagatable_connection"; @@ -58,9 +57,13 @@ public PoolImpl(VertxInternal vertx, PoolOptions poolOptions, Function> afterAcquire, Function> beforeRecycle, + Function> connectionProvider, + Handler connectionInitializer, CloseFuture closeFuture) { super(driver); + Handler hook = connectionInitializer != null ? this::initializeConnection : null; + this.idleTimeout = MILLISECONDS.convert(poolOptions.getIdleTimeout(), poolOptions.getIdleTimeoutUnit()); this.connectionTimeout = MILLISECONDS.convert(poolOptions.getConnectionTimeout(), poolOptions.getConnectionTimeoutUnit()); this.maxLifetime = MILLISECONDS.convert(poolOptions.getMaxLifetime(), poolOptions.getMaxLifetimeUnit()); @@ -68,8 +71,18 @@ public PoolImpl(VertxInternal vertx, this.timerID = -1L; this.pipelined = pipelined; this.vertx = vertx; - this.pool = new SqlConnectionPool(ctx -> connectionProvider.apply(ctx), () -> connectionInitializer, afterAcquire, beforeRecycle, vertx, idleTimeout, maxLifetime, poolOptions.getMaxSize(), pipelined, poolOptions.getMaxWaitQueueSize(), poolOptions.getEventLoopSize()); + this.pool = new SqlConnectionPool(connectionProvider, hook, afterAcquire, beforeRecycle, vertx, idleTimeout, maxLifetime, poolOptions.getMaxSize(), pipelined, poolOptions.getMaxWaitQueueSize(), poolOptions.getEventLoopSize()); this.closeFuture = closeFuture; + this.connectionInitializer = connectionInitializer; + } + + private void initializeConnection(SqlConnectionPool.PooledConnection conn) { + if (connectionInitializer != null) { + ContextInternal current = vertx.getContext(); + SqlConnectionInternal wrapper = driver.wrapConnection(current, conn.factory(), conn); + conn.init(wrapper); + current.dispatch(wrapper, connectionInitializer); + } } public Pool init() { @@ -84,14 +97,6 @@ public Pool init() { return this; } - public Pool connectionProvider(Function> connectionProvider) { - if (connectionProvider == null) { - throw new NullPointerException(); - } - this.connectionProvider = connectionProvider; - return this; - } - private void runEviction() { synchronized (this) { if (timerID == -1) { @@ -173,21 +178,6 @@ public Future close() { return promise.future(); } - @Override - public Pool connectHandler(Handler handler) { - if (handler != null) { - connectionInitializer = conn -> { - ContextInternal current = vertx.getContext(); - SqlConnectionInternal wrapper = driver.wrapConnection(current, conn.factory(), conn); - conn.init(wrapper); - current.dispatch(wrapper, handler); - }; - } else { - connectionInitializer = null; - } - return this; - } - private Future doClose() { synchronized (this) { if (timerID >= 0) { diff --git a/vertx-sql-client/src/main/java/io/vertx/sqlclient/impl/pool/SqlConnectionPool.java b/vertx-sql-client/src/main/java/io/vertx/sqlclient/impl/pool/SqlConnectionPool.java index 87b73861a..aea65b190 100644 --- a/vertx-sql-client/src/main/java/io/vertx/sqlclient/impl/pool/SqlConnectionPool.java +++ b/vertx-sql-client/src/main/java/io/vertx/sqlclient/impl/pool/SqlConnectionPool.java @@ -47,7 +47,7 @@ public class SqlConnectionPool { private final Function> connectionProvider; private final VertxInternal vertx; private final ConnectionPool pool; - private final Supplier> hook; + private final Handler hook; private final Function> afterAcquire; private final Function> beforeRecycle; private final boolean pipelined; @@ -56,7 +56,7 @@ public class SqlConnectionPool { private final int maxSize; public SqlConnectionPool(Function> connectionProvider, - Supplier> hook, + Handler hook, Function> afterAcquire, Function> beforeRecycle, VertxInternal vertx, @@ -115,11 +115,10 @@ public Future> connect(ContextInternal context, if (conn.isValid()) { PooledConnection pooled = new PooledConnection(connBase.factory(), conn, listener); conn.init(pooled); - Handler connectionHandler = hook.get(); - if (connectionHandler != null) { + if (hook != null) { Promise> p = Promise.promise(); pooled.poolCallback = p; - connectionHandler.handle(pooled); + hook.handle(pooled); return p.future(); } else { return Future.succeededFuture(new ConnectResult<>(pooled, pipelined ? conn.pipeliningLimit() : 1, 0)); diff --git a/vertx-sql-client/src/main/java/io/vertx/sqlclient/spi/Driver.java b/vertx-sql-client/src/main/java/io/vertx/sqlclient/spi/Driver.java index 8ffee4d7e..1cf7d8d8f 100644 --- a/vertx-sql-client/src/main/java/io/vertx/sqlclient/spi/Driver.java +++ b/vertx-sql-client/src/main/java/io/vertx/sqlclient/spi/Driver.java @@ -17,6 +17,7 @@ package io.vertx.sqlclient.spi; import io.vertx.core.Future; +import io.vertx.core.Handler; import io.vertx.core.Vertx; import io.vertx.core.VertxOptions; import io.vertx.core.impl.CloseFuture; @@ -26,9 +27,9 @@ import io.vertx.sqlclient.Pool; import io.vertx.sqlclient.PoolOptions; import io.vertx.sqlclient.SqlConnectOptions; +import io.vertx.sqlclient.SqlConnection; import io.vertx.sqlclient.impl.*; -import java.util.List; import java.util.function.Supplier; /** @@ -43,13 +44,14 @@ public interface Driver { *

The returned pool will automatically closed when {@code vertx} is not {@code null} and is closed or when the creating * context is closed (e.g verticle undeployment). * - * @param vertx the Vertx instance to be used with the connection pool or {@code null} to create an auto closed Vertx instance - * @param databases the list of databases - * @param poolOptions the options for creating the pool - * @param transportOptions the options to configure the TCP client + * @param vertx the Vertx instance to be used with the connection pool or {@code null} to create an auto closed Vertx instance + * @param databases the list of databases + * @param poolOptions the options for creating the pool + * @param transportOptions the options to configure the TCP client + * @param connectHandler * @return the connection pool */ - default Pool createPool(Vertx vertx, Supplier> databases, PoolOptions poolOptions, NetClientOptions transportOptions) { + default Pool createPool(Vertx vertx, Supplier> databases, PoolOptions poolOptions, NetClientOptions transportOptions, Handler connectHandler) { VertxInternal vx; if (vertx == null) { if (Vertx.currentContext() != null) { @@ -62,7 +64,7 @@ default Pool createPool(Vertx vertx, Supplier> databases, PoolOptions CloseFuture closeFuture = new CloseFuture(); Pool pool; try { - pool = newPool(vx, databases, poolOptions, transportOptions, closeFuture); + pool = newPool(vx, databases, poolOptions, transportOptions, connectHandler, closeFuture); } catch (Exception e) { if (vertx == null) { vx.close(); @@ -85,16 +87,17 @@ default Pool createPool(Vertx vertx, Supplier> databases, PoolOptions /** * Create a connection pool to the database configured with the given {@code connectOptions} and {@code poolOptions}. *

- * This method is not meant to be used directly by users, instead they should use {@link #createPool(Vertx, Supplier, PoolOptions, NetClientOptions)}. + * This method is not meant to be used directly by users, instead they should use {@link #createPool(Vertx, Supplier, PoolOptions, NetClientOptions, Handler)}. * * @param vertx the Vertx instance to be used with the connection pool * @param databases the list of databases * @param options the options for creating the pool * @param transportOptions the options to configure the TCP client + * @param connectHandler the connect handler * @param closeFuture the close future * @return the connection pool */ - Pool newPool(Vertx vertx, Supplier> databases, PoolOptions options, NetClientOptions transportOptions, CloseFuture closeFuture); + Pool newPool(Vertx vertx, Supplier> databases, PoolOptions options, NetClientOptions transportOptions, Handler connectHandler, CloseFuture closeFuture); /** * Create a connection factory to the given {@code database}. diff --git a/vertx-sql-client/src/test/java/io/vertx/sqlclient/tck/DriverTestBase.java b/vertx-sql-client/src/test/java/io/vertx/sqlclient/tck/DriverTestBase.java index 7f713cd50..13d15d151 100644 --- a/vertx-sql-client/src/test/java/io/vertx/sqlclient/tck/DriverTestBase.java +++ b/vertx-sql-client/src/test/java/io/vertx/sqlclient/tck/DriverTestBase.java @@ -5,7 +5,6 @@ import static org.junit.Assert.assertTrue; import java.util.ArrayList; -import java.util.Collections; import java.util.List; import java.util.ServiceConfigurationError; import java.util.ServiceLoader; @@ -54,7 +53,7 @@ public void testRejectsOtherOptions() { @Test public void testCreatePoolFromDriver(TestContext ctx) { - testCreatePoolWithVertx(ctx, vertx -> getDriver().createPool(vertx, () -> Future.succeededFuture(defaultOptions()), new PoolOptions().setMaxSize(1), new NetClientOptions())); + testCreatePoolWithVertx(ctx, vertx -> getDriver().createPool(vertx, () -> Future.succeededFuture(defaultOptions()), new PoolOptions().setMaxSize(1), new NetClientOptions(), null)); } @Test