diff --git a/vertx-db2-client/src/main/asciidoc/index.adoc b/vertx-db2-client/src/main/asciidoc/index.adoc index d9ab57a35..3ead12266 100644 --- a/vertx-db2-client/src/main/asciidoc/index.adoc +++ b/vertx-db2-client/src/main/asciidoc/index.adoc @@ -99,7 +99,7 @@ Once you are done with the connection you must close it to release it to the poo == Pool versus pooled client -The {@link io.vertx.db2client.DB2Pool} allows you to create a pool or a pooled client +The {@link io.vertx.db2client.DB2Builder} allows you to create a pool or a pooled client [source,$lang] ---- diff --git a/vertx-db2-client/src/main/java/examples/DB2ClientExamples.java b/vertx-db2-client/src/main/java/examples/DB2ClientExamples.java index 62c94a971..6517da50a 100644 --- a/vertx-db2-client/src/main/java/examples/DB2ClientExamples.java +++ b/vertx-db2-client/src/main/java/examples/DB2ClientExamples.java @@ -23,9 +23,9 @@ import io.vertx.core.Vertx; import io.vertx.core.net.ClientSSLOptions; import io.vertx.core.net.JksOptions; +import io.vertx.db2client.DB2Builder; import io.vertx.db2client.DB2ConnectOptions; import io.vertx.db2client.DB2Connection; -import io.vertx.db2client.DB2Pool; import io.vertx.docgen.Source; import io.vertx.sqlclient.Pool; import io.vertx.sqlclient.PoolOptions; @@ -53,7 +53,10 @@ public void gettingStarted() { .setMaxSize(5); // Create the client pool - DB2Pool client = DB2Pool.pool(connectOptions, poolOptions); + Pool client = DB2Builder.pool() + .with(poolOptions) + .connectingTo(connectOptions) + .build(); // A simple query client @@ -86,7 +89,11 @@ public void configureFromDataObject(Vertx vertx) { PoolOptions poolOptions = new PoolOptions().setMaxSize(5); // Create the pool from the data object - DB2Pool pool = DB2Pool.pool(vertx, connectOptions, poolOptions); + Pool pool = DB2Builder.pool() + .with(poolOptions) + .connectingTo(connectOptions) + .using(vertx) + .build(); pool.getConnection() .onComplete(ar -> { @@ -100,7 +107,10 @@ public void configureFromUri(Vertx vertx) { String connectionUri = "db2://dbuser:secretpassword@database.server.com:50000/mydb"; // Create the pool from the connection URI - DB2Pool pool = DB2Pool.pool(connectionUri); + Pool pool = DB2Builder.pool() + .connectingTo(connectionUri) + .using(vertx) + .build(); // Create the connection from the connection URI DB2Connection.connect(vertx, connectionUri) @@ -109,7 +119,7 @@ public void configureFromUri(Vertx vertx) { }); } - public void connecting01() { + public void connecting01(Vertx vertx) { // Connect options DB2ConnectOptions connectOptions = new DB2ConnectOptions() @@ -124,7 +134,11 @@ public void connecting01() { .setMaxSize(5); // Create the pooled client - SqlClient client = DB2Pool.client(connectOptions, poolOptions); + SqlClient client = DB2Builder.client() + .with(poolOptions) + .connectingTo(connectOptions) + .using(vertx) + .build(); } public void connecting02(Vertx vertx) { @@ -141,7 +155,11 @@ public void connecting02(Vertx vertx) { PoolOptions poolOptions = new PoolOptions() .setMaxSize(5); // Create the pooled client - SqlClient client = DB2Pool.client(vertx, connectOptions, poolOptions); + SqlClient client = DB2Builder.client() + .with(poolOptions) + .connectingTo(connectOptions) + .using(vertx) + .build(); } public void connecting03(SqlClient client) { @@ -165,7 +183,11 @@ public void connecting04(Vertx vertx) { .setMaxSize(5); // Create the pooled client - DB2Pool client = DB2Pool.pool(vertx, connectOptions, poolOptions); + Pool client = DB2Builder.pool() + .with(poolOptions) + .connectingTo(connectOptions) + .using(vertx) + .build(); // Get a connection from the pool client.getConnection().compose(conn -> { @@ -195,13 +217,21 @@ public void connecting04(Vertx vertx) { public void poolVersusPooledClient(Vertx vertx, String sql, DB2ConnectOptions connectOptions, PoolOptions poolOptions) { // Pooled client - SqlClient client = DB2Pool.client(vertx, connectOptions, poolOptions); + SqlClient client = DB2Builder.client() + .with(poolOptions) + .connectingTo(connectOptions) + .using(vertx) + .build(); // Pipelined Future> res1 = client.query(sql).execute(); // Connection pool - DB2Pool pool = DB2Pool.pool(vertx, connectOptions, poolOptions); + Pool pool = DB2Builder.pool() + .with(poolOptions) + .connectingTo(connectOptions) + .using(vertx) + .build(); // Not pipelined Future> res2 = pool.query(sql).execute(); diff --git a/vertx-db2-client/src/main/java/examples/SqlClientExamples.java b/vertx-db2-client/src/main/java/examples/SqlClientExamples.java index ac3115ec1..3dc28bef3 100644 --- a/vertx-db2-client/src/main/java/examples/SqlClientExamples.java +++ b/vertx-db2-client/src/main/java/examples/SqlClientExamples.java @@ -20,12 +20,10 @@ import io.vertx.core.Future; import io.vertx.core.Vertx; import io.vertx.core.tracing.TracingPolicy; +import io.vertx.db2client.DB2Builder; import io.vertx.db2client.DB2ConnectOptions; -import io.vertx.db2client.DB2Pool; -import io.vertx.db2client.spi.DB2Driver; import io.vertx.docgen.Source; import io.vertx.sqlclient.*; -import io.vertx.sqlclient.spi.ConnectionFactory; import java.util.ArrayList; import java.util.Arrays; @@ -360,12 +358,16 @@ public void tracing01(DB2ConnectOptions options) { options.setTracingPolicy(TracingPolicy.ALWAYS); } - public void poolConfig01(DB2ConnectOptions server1, DB2ConnectOptions server2, DB2ConnectOptions server3, PoolOptions options) { - DB2Pool pool = DB2Pool.pool(Arrays.asList(server1, server2, server3), options); + public void poolConfig01(Vertx vertx, DB2ConnectOptions server1, DB2ConnectOptions server2, DB2ConnectOptions server3, PoolOptions options) { + Pool pool = DB2Builder.pool() + .with(options) + .connectingTo(Arrays.asList(server1, server2, server3)) + .using(vertx) + .build(); } - public void poolConfig02(DB2Pool pool, String sql) { - pool.connectHandler(conn -> { + public void poolConfig02(ClientBuilder builder, String sql) { + builder.withConnectHandler(conn -> { conn.query(sql).execute().onSuccess(res -> { // Release the connection to the pool, ready to be used by the application conn.close(); @@ -374,7 +376,11 @@ public void poolConfig02(DB2Pool pool, String sql) { } public void poolSharing1(Vertx vertx, DB2ConnectOptions database, int maxSize) { - DB2Pool pool = DB2Pool.pool(database, new PoolOptions().setMaxSize(maxSize)); + Pool pool = DB2Builder.pool() + .with(new PoolOptions().setMaxSize(maxSize)) + .connectingTo(database) + .using(vertx) + .build(); vertx.deployVerticle(() -> new AbstractVerticle() { @Override public void start() throws Exception { @@ -385,36 +391,48 @@ public void start() throws Exception { public void poolSharing2(Vertx vertx, DB2ConnectOptions database, int maxSize) { vertx.deployVerticle(() -> new AbstractVerticle() { - DB2Pool pool; + Pool pool; @Override public void start() { // Get or create a shared pool // this actually creates a lease to the pool // when the verticle is undeployed, the lease will be released automaticaly - pool = DB2Pool.pool(database, new PoolOptions() - .setMaxSize(maxSize) - .setShared(true) - .setName("my-pool")); + pool = DB2Builder.pool() + .with(new PoolOptions() + .setMaxSize(maxSize) + .setShared(true) + .setName("my-pool")) + .connectingTo(database) + .using(vertx) + .build(); } }, new DeploymentOptions().setInstances(4)); } public static void poolSharing3(Vertx vertx, DB2ConnectOptions database, int maxSize) { - DB2Pool pool = DB2Pool.pool(database, new PoolOptions() - .setMaxSize(maxSize) - .setShared(true) - .setName("my-pool") - .setEventLoopSize(4)); + Pool pool = DB2Builder.pool() + .with(new PoolOptions() + .setMaxSize(maxSize) + .setShared(true) + .setName("my-pool") + .setEventLoopSize(4)) + .connectingTo(database) + .using(vertx) + .build(); } public void dynamicPoolConfig(Vertx vertx, PoolOptions poolOptions) { - DB2Pool pool = DB2Pool.pool(vertx, () -> { - Future connectOptions = retrieveOptions(); - return connectOptions; - }, poolOptions); + Pool pool = DB2Builder.pool() + .with(poolOptions) + .connectingTo(() -> { + Future connectOptions = retrieveOptions(); + return connectOptions; + }) + .using(vertx) + .build(); } - private Future retrieveOptions() { + private Future retrieveOptions() { return null; } } 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 new file mode 100644 index 000000000..aa0c2a549 --- /dev/null +++ b/vertx-db2-client/src/main/java/io/vertx/db2client/DB2Builder.java @@ -0,0 +1,94 @@ +/* + * 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.db2client; + +import io.vertx.codegen.annotations.VertxGen; +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.*; +import io.vertx.sqlclient.impl.ClientBuilderBase; + +import java.util.function.Supplier; + +/** + * Entry point for building DB2 clients. + */ +@VertxGen +public interface DB2Builder { + + /** + * Build a pool with the specified {@code block} argument. + * The {@code block} argument is usually a lambda that configures the provided builder + *

+ * Example usage: {@code Pool pool = PgBuilder.pool(builder -> builder.connectingTo(connectOptions));} + * + * @return the pool as configured by the code {@code block} + */ + static Pool pool(Handler> block) { + return ClientBuilder.pool(DB2Driver.INSTANCE, block); + } + + /** + * Provide a builder for DB2 pool of connections + *

+ * Example usage: {@code Pool pool = PgBuilder.pool().connectingTo(connectOptions).build()} + */ + static ClientBuilder pool() { + return ClientBuilder.pool(DB2Driver.INSTANCE); + } + + /** + * Build a client backed by a connection pool with the specified {@code block} argument. + * The {@code block} argument is usually a lambda that configures the provided builder + *

+ * Example usage: {@code SqlClient client = PgBuilder.client(builder -> builder.connectingTo(connectOptions));} + * + * @return the client as configured by the code {@code block} + */ + static SqlClient client(Handler> handler) { + ClientBuilder builder = client(); + handler.handle(builder); + return builder.build(); + } + + /** + * Provide a builder for DB2 client backed by a connection pool. + *

+ * Example usage: {@code SqlClient client = PgBuilder.client().connectingTo(connectOptions).build()} + */ + static ClientBuilder client() { + return new ClientBuilderBase(DB2Driver.INSTANCE) { + @Override + public ClientBuilder with(PoolOptions options) { + if (options != null) { + options = new Db2PoolOptions(options).setPipelined(true); + } + return super.with(options); + } + + @Override + protected SqlClient create(Vertx vertx, Supplier> databases, PoolOptions poolOptions, NetClientOptions transportOptions) { + return driver.createPool(vertx, databases, poolOptions, transportOptions); + } + }; + } +} diff --git a/vertx-db2-client/src/main/java/io/vertx/db2client/DB2ConnectOptions.java b/vertx-db2-client/src/main/java/io/vertx/db2client/DB2ConnectOptions.java index fde8993f4..01eb82e95 100644 --- a/vertx-db2-client/src/main/java/io/vertx/db2client/DB2ConnectOptions.java +++ b/vertx-db2-client/src/main/java/io/vertx/db2client/DB2ConnectOptions.java @@ -19,7 +19,6 @@ import java.util.HashMap; import java.util.Map; import java.util.Objects; -import java.util.concurrent.TimeUnit; import java.util.function.Predicate; import io.vertx.codegen.annotations.DataObject; @@ -33,7 +32,7 @@ import io.vertx.sqlclient.SqlConnectOptions; /** - * Connect options for configuring {@link DB2Connection} or {@link DB2Pool}. + * Connect options for configuring {@link DB2Connection} or {@link DB2Builder}. */ @DataObject(generateConverter = true) public class DB2ConnectOptions extends SqlConnectOptions { 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 index 81c1d93d2..dda5fd1f8 100644 --- a/vertx-db2-client/src/main/java/io/vertx/db2client/DB2Pool.java +++ b/vertx-db2-client/src/main/java/io/vertx/db2client/DB2Pool.java @@ -21,12 +21,14 @@ 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; @@ -107,7 +109,7 @@ static DB2Pool pool(List databases, PoolOptions options) { * {@link Vertx} instance. */ static DB2Pool pool(Vertx vertx, List databases, PoolOptions options) { - return (DB2Pool) DB2Driver.INSTANCE.createPool(vertx, databases, options); + return (DB2Pool) DB2Driver.INSTANCE.createPool(vertx, Utils.roundRobinSupplier(databases), options, new NetClientOptions()); } /** @@ -127,7 +129,7 @@ static DB2Pool pool(Supplier> databases, PoolOptions p * 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); + return (DB2Pool) DB2Driver.INSTANCE.createPool(vertx, databases, poolOptions, new NetClientOptions()); } /** @@ -196,7 +198,7 @@ static SqlClient client(List databases, PoolOptions options) * {@link Vertx} instance. */ static SqlClient client(Vertx vertx, List databases, PoolOptions options) { - return DB2Driver.INSTANCE.createPool(vertx, databases, new Db2PoolOptions(options).setPipelined(true)); + return DB2Driver.INSTANCE.createPool(vertx, Utils.roundRobinSupplier(databases), new Db2PoolOptions(options).setPipelined(true), new NetClientOptions()); } @Override 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 40ed784d9..5f60b9a6e 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 @@ -21,6 +21,7 @@ import io.vertx.core.impl.ContextInternal; import io.vertx.core.impl.VertxInternal; import io.vertx.core.json.JsonObject; +import io.vertx.core.net.NetClientOptions; import io.vertx.db2client.DB2ConnectOptions; import io.vertx.db2client.impl.*; import io.vertx.sqlclient.Pool; @@ -46,21 +47,21 @@ public DB2ConnectOptions downcast(SqlConnectOptions connectOptions) { } @Override - public Pool newPool(Vertx vertx, Supplier> databases, PoolOptions options, CloseFuture closeFuture) { + public Pool newPool(Vertx vertx, Supplier> databases, PoolOptions poolOptions, NetClientOptions transportOptions, 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)); + if (poolOptions.isShared()) { + pool = vx.createSharedResource(SHARED_CLIENT_KEY, poolOptions.getName(), closeFuture, cf -> newPoolImpl(vx, databases, poolOptions, transportOptions, cf)); } else { - pool = newPoolImpl(vx, databases, options, closeFuture); + pool = newPoolImpl(vx, databases, poolOptions, transportOptions, closeFuture); } return new DB2PoolImpl(vx, closeFuture, pool); } - private PoolImpl newPoolImpl(VertxInternal vertx, Supplier> databases, PoolOptions options, CloseFuture closeFuture) { + private PoolImpl newPoolImpl(VertxInternal vertx, 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); + ConnectionFactory factory = createConnectionFactory(vertx, transportOptions); pool.connectionProvider(context -> factory.connect(context, databases.get())); pool.init(); closeFuture.add(factory); @@ -79,7 +80,7 @@ public boolean acceptsOptions(SqlConnectOptions options) { } @Override - public ConnectionFactory createConnectionFactory(Vertx vertx) { + public ConnectionFactory createConnectionFactory(Vertx vertx, NetClientOptions transportOptions) { return new DB2ConnectionFactory((VertxInternal) vertx); } diff --git a/vertx-db2-client/src/test/java/io/vertx/db2client/tck/ClientConfig.java b/vertx-db2-client/src/test/java/io/vertx/db2client/tck/ClientConfig.java index 6f25c7902..c0c9b0ab0 100644 --- a/vertx-db2-client/src/test/java/io/vertx/db2client/tck/ClientConfig.java +++ b/vertx-db2-client/src/test/java/io/vertx/db2client/tck/ClientConfig.java @@ -20,13 +20,10 @@ import io.vertx.core.Future; import io.vertx.core.Handler; import io.vertx.core.Vertx; +import io.vertx.db2client.DB2Builder; import io.vertx.db2client.DB2ConnectOptions; import io.vertx.db2client.DB2Connection; -import io.vertx.db2client.DB2Pool; -import io.vertx.sqlclient.PoolOptions; -import io.vertx.sqlclient.SqlClient; -import io.vertx.sqlclient.SqlConnectOptions; -import io.vertx.sqlclient.SqlConnection; +import io.vertx.sqlclient.*; import io.vertx.sqlclient.tck.Connector; @SuppressWarnings("unchecked") @@ -57,7 +54,11 @@ public void close() { POOLED() { @Override public Connector connect(Vertx vertx, SqlConnectOptions options) { - DB2Pool pool = DB2Pool.pool(vertx, new DB2ConnectOptions(options), new PoolOptions().setMaxSize(1)); + Pool pool = DB2Builder.pool() + .with(new PoolOptions().setMaxSize(1)) + .connectingTo(new DB2ConnectOptions(options)) + .using(vertx) + .build(); return new Connector() { @Override public void connect(Handler> handler) { diff --git a/vertx-db2-client/src/test/java/io/vertx/db2client/tck/DB2TracingTest.java b/vertx-db2-client/src/test/java/io/vertx/db2client/tck/DB2TracingTest.java index 08891df7c..4f63c3a03 100644 --- a/vertx-db2-client/src/test/java/io/vertx/db2client/tck/DB2TracingTest.java +++ b/vertx-db2-client/src/test/java/io/vertx/db2client/tck/DB2TracingTest.java @@ -12,11 +12,10 @@ package io.vertx.db2client.tck; import io.vertx.core.Vertx; -import io.vertx.db2client.DB2Pool; +import io.vertx.db2client.DB2Builder; import io.vertx.db2client.junit.DB2Resource; import io.vertx.ext.unit.junit.VertxUnitRunner; import io.vertx.sqlclient.Pool; -import io.vertx.sqlclient.PoolOptions; import io.vertx.sqlclient.tck.TracingTestBase; import org.junit.ClassRule; import org.junit.runner.RunWith; @@ -29,7 +28,7 @@ public class DB2TracingTest extends TracingTestBase { @Override protected Pool createPool(Vertx vertx) { - return DB2Pool.pool(vertx, rule.options(), new PoolOptions()); + return DB2Builder.pool(builder -> builder.connectingTo(rule.options()).using(vertx)); } @Override diff --git a/vertx-db2-client/src/test/java/io/vertx/db2client/tck/DB2TransactionTest.java b/vertx-db2-client/src/test/java/io/vertx/db2client/tck/DB2TransactionTest.java index c868bf8ef..f27c1f91e 100644 --- a/vertx-db2-client/src/test/java/io/vertx/db2client/tck/DB2TransactionTest.java +++ b/vertx-db2-client/src/test/java/io/vertx/db2client/tck/DB2TransactionTest.java @@ -17,6 +17,7 @@ import static org.junit.Assume.assumeFalse; +import io.vertx.db2client.DB2Builder; import org.junit.Before; import org.junit.ClassRule; import org.junit.Rule; @@ -25,7 +26,6 @@ import org.junit.runner.RunWith; import io.vertx.db2client.DB2ConnectOptions; -import io.vertx.db2client.DB2Pool; import io.vertx.db2client.junit.DB2Resource; import io.vertx.ext.unit.TestContext; import io.vertx.ext.unit.junit.VertxUnitRunner; @@ -49,12 +49,20 @@ public void printTestName(TestContext ctx) throws Exception { @Override protected Pool createPool() { - return DB2Pool.pool(vertx, new DB2ConnectOptions(rule.options()), new PoolOptions().setMaxSize(1)); + return DB2Builder.pool() + .with(new PoolOptions().setMaxSize(1)) + .connectingTo(new DB2ConnectOptions(rule.options())) + .using(vertx) + .build(); } @Override protected Pool nonTxPool() { - return DB2Pool.pool(vertx, new DB2ConnectOptions(rule.options()), new PoolOptions().setMaxSize(1)); + return DB2Builder.pool() + .with(new PoolOptions().setMaxSize(1)) + .connectingTo(new DB2ConnectOptions(rule.options())) + .using(vertx) + .build(); } @Override diff --git a/vertx-mssql-client/src/main/java/examples/MSSQLClientExamples.java b/vertx-mssql-client/src/main/java/examples/MSSQLClientExamples.java index 78962d2b6..94e2fd9bb 100644 --- a/vertx-mssql-client/src/main/java/examples/MSSQLClientExamples.java +++ b/vertx-mssql-client/src/main/java/examples/MSSQLClientExamples.java @@ -15,9 +15,9 @@ import io.vertx.core.net.ClientSSLOptions; import io.vertx.core.net.PemTrustOptions; import io.vertx.docgen.Source; +import io.vertx.mssqlclient.MSSQLBuilder; import io.vertx.mssqlclient.MSSQLConnectOptions; import io.vertx.mssqlclient.MSSQLConnection; -import io.vertx.mssqlclient.MSSQLPool; import io.vertx.sqlclient.*; import io.vertx.sqlclient.data.NullValue; @@ -42,7 +42,10 @@ public void gettingStarted() { .setMaxSize(5); // Create the client pool - MSSQLPool client = MSSQLPool.pool(connectOptions, poolOptions); + Pool client = MSSQLBuilder.pool() + .with(poolOptions) + .connectingTo(connectOptions) + .build(); // A simple query client @@ -75,7 +78,11 @@ public void configureFromDataObject(Vertx vertx) { PoolOptions poolOptions = new PoolOptions().setMaxSize(5); // Create the pool from the data object - MSSQLPool pool = MSSQLPool.pool(vertx, connectOptions, poolOptions); + Pool pool = MSSQLBuilder.pool() + .with(poolOptions) + .connectingTo(connectOptions) + .using(vertx) + .build(); pool.getConnection() .onComplete(ar -> { @@ -89,7 +96,10 @@ public void configureFromUri(Vertx vertx) { String connectionUri = "sqlserver://dbuser:secretpassword@database.server.com:1433/mydb"; // Create the pool from the connection URI - MSSQLPool pool = MSSQLPool.pool(connectionUri); + Pool pool = MSSQLBuilder.pool() + .connectingTo(connectionUri) + .using(vertx) + .build(); // Create the connection from the connection URI MSSQLConnection.connect(vertx, connectionUri) @@ -98,7 +108,7 @@ public void configureFromUri(Vertx vertx) { }); } - public void connecting01() { + public void connecting01(Vertx vertx) { // Connect options MSSQLConnectOptions connectOptions = new MSSQLConnectOptions() @@ -113,7 +123,11 @@ public void connecting01() { .setMaxSize(5); // Create the pooled client - MSSQLPool client = MSSQLPool.pool(connectOptions, poolOptions); + Pool client = MSSQLBuilder.pool() + .with(poolOptions) + .connectingTo(connectOptions) + .using(vertx) + .build(); } @@ -131,7 +145,11 @@ public void connecting02(Vertx vertx) { PoolOptions poolOptions = new PoolOptions() .setMaxSize(5); // Create the pooled client - MSSQLPool client = MSSQLPool.pool(vertx, connectOptions, poolOptions); + Pool client = MSSQLBuilder.pool() + .with(poolOptions) + .connectingTo(connectOptions) + .using(vertx) + .build(); } public void connecting03(Pool pool) { @@ -155,7 +173,11 @@ public void connecting04(Vertx vertx) { .setMaxSize(5); // Create the pooled client - MSSQLPool client = MSSQLPool.pool(vertx, connectOptions, poolOptions); + Pool client = MSSQLBuilder.pool() + .with(poolOptions) + .connectingTo(connectOptions) + .using(vertx) + .build(); // Get a connection from the pool client.getConnection().compose(conn -> { diff --git a/vertx-mssql-client/src/main/java/examples/SqlClientExamples.java b/vertx-mssql-client/src/main/java/examples/SqlClientExamples.java index db5c6a90d..615488c67 100644 --- a/vertx-mssql-client/src/main/java/examples/SqlClientExamples.java +++ b/vertx-mssql-client/src/main/java/examples/SqlClientExamples.java @@ -22,11 +22,9 @@ import io.vertx.core.Vertx; import io.vertx.core.tracing.TracingPolicy; import io.vertx.docgen.Source; +import io.vertx.mssqlclient.MSSQLBuilder; import io.vertx.mssqlclient.MSSQLConnectOptions; -import io.vertx.mssqlclient.MSSQLPool; -import io.vertx.mssqlclient.spi.MSSQLDriver; import io.vertx.sqlclient.*; -import io.vertx.sqlclient.spi.ConnectionFactory; import java.util.ArrayList; import java.util.Arrays; @@ -369,12 +367,16 @@ public void tracing01(MSSQLConnectOptions options) { options.setTracingPolicy(TracingPolicy.ALWAYS); } - public void poolConfig01(MSSQLConnectOptions server1, MSSQLConnectOptions server2, MSSQLConnectOptions server3, PoolOptions options) { - MSSQLPool pool = MSSQLPool.pool(Arrays.asList(server1, server2, server3), options); + public void poolConfig01(Vertx vertx, MSSQLConnectOptions server1, MSSQLConnectOptions server2, MSSQLConnectOptions server3, PoolOptions options) { + Pool pool = MSSQLBuilder.pool() + .with(options) + .connectingTo(Arrays.asList(server1, server2, server3)) + .using(vertx) + .build(); } - public void poolConfig02(MSSQLPool pool, String sql) { - pool.connectHandler(conn -> { + public void poolConfig02(ClientBuilder builder, String sql) { + builder.withConnectHandler(conn -> { conn.query(sql).execute().onSuccess(res -> { // Release the connection to the pool, ready to be used by the application conn.close(); @@ -383,7 +385,11 @@ public void poolConfig02(MSSQLPool pool, String sql) { } public void poolSharing1(Vertx vertx, MSSQLConnectOptions database, int maxSize) { - MSSQLPool pool = MSSQLPool.pool(database, new PoolOptions().setMaxSize(maxSize)); + Pool pool = MSSQLBuilder.pool() + .with(new PoolOptions().setMaxSize(maxSize)) + .connectingTo(database) + .using(vertx) + .build(); vertx.deployVerticle(() -> new AbstractVerticle() { @Override public void start() throws Exception { @@ -394,36 +400,47 @@ public void start() throws Exception { public void poolSharing2(Vertx vertx, MSSQLConnectOptions database, int maxSize) { vertx.deployVerticle(() -> new AbstractVerticle() { - MSSQLPool pool; + Pool pool; @Override public void start() { // Get or create a shared pool // this actually creates a lease to the pool // when the verticle is undeployed, the lease will be released automaticaly - pool = MSSQLPool.pool(database, new PoolOptions() - .setMaxSize(maxSize) - .setShared(true) - .setName("my-pool")); + pool = MSSQLBuilder.pool() + .with(new PoolOptions() + .setMaxSize(maxSize) + .setShared(true) + .setName("my-pool")) + .using(vertx) + .build(); } }, new DeploymentOptions().setInstances(4)); } public static void poolSharing3(Vertx vertx, MSSQLConnectOptions database, int maxSize) { - MSSQLPool pool = MSSQLPool.pool(database, new PoolOptions() - .setMaxSize(maxSize) - .setShared(true) - .setName("my-pool") - .setEventLoopSize(4)); + Pool pool = MSSQLBuilder.pool() + .with(new PoolOptions() + .setMaxSize(maxSize) + .setShared(true) + .setName("my-pool") + .setEventLoopSize(4)) + .connectingTo(database) + .using(vertx) + .build(); } public void dynamicPoolConfig(Vertx vertx, PoolOptions poolOptions) { - MSSQLPool pool = MSSQLPool.pool(vertx, () -> { - Future connectOptions = retrieveOptions(); - return connectOptions; - }, poolOptions); + Pool pool = MSSQLBuilder.pool() + .with(poolOptions) + .connectingTo(() -> { + Future connectOptions = retrieveOptions(); + return connectOptions; + }) + .using(vertx) + .build(); } - private Future retrieveOptions() { + private Future retrieveOptions() { return null; } } diff --git a/vertx-mssql-client/src/main/java/io/vertx/mssqlclient/MSSQLBuilder.java b/vertx-mssql-client/src/main/java/io/vertx/mssqlclient/MSSQLBuilder.java new file mode 100644 index 000000000..85e97f2f2 --- /dev/null +++ b/vertx-mssql-client/src/main/java/io/vertx/mssqlclient/MSSQLBuilder.java @@ -0,0 +1,54 @@ +/* + * 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.mssqlclient; + +import io.vertx.codegen.annotations.VertxGen; +import io.vertx.core.Handler; +import io.vertx.mssqlclient.spi.MSSQLDriver; +import io.vertx.sqlclient.ClientBuilder; +import io.vertx.sqlclient.Pool; +import io.vertx.sqlclient.PoolOptions; +import io.vertx.sqlclient.impl.ClientBuilderBase; + +/** + * Entry point for building MSSQL clients. + */ +@VertxGen +public interface MSSQLBuilder { + + /** + * Build a pool with the specified {@code block} argument. + * The {@code block} argument is usually a lambda that configures the provided builder + *

+ * Example usage: {@code Pool pool = PgBuilder.pool(builder -> builder.connectingTo(connectOptions));} + * + * @return the pool as configured by the code {@code block} + */ + static Pool pool(Handler> block) { + return ClientBuilder.pool(MSSQLDriver.INSTANCE, block); + } + + /** + * Provide a builder for MSSQL pool of connections + *

+ * Example usage: {@code Pool pool = PgBuilder.pool().connectingTo(connectOptions).build()} + */ + static ClientBuilder pool() { + return ClientBuilder.pool(MSSQLDriver.INSTANCE); + } +} 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 index 97ec28e95..61324e43b 100644 --- a/vertx-mssql-client/src/main/java/io/vertx/mssqlclient/MSSQLPool.java +++ b/vertx-mssql-client/src/main/java/io/vertx/mssqlclient/MSSQLPool.java @@ -17,9 +17,11 @@ 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; @@ -31,6 +33,7 @@ * A {@link Pool pool} of {@link MSSQLConnection SQL Server connections}. */ @VertxGen +@Deprecated public interface MSSQLPool extends Pool { /** @@ -95,7 +98,7 @@ static MSSQLPool pool(List databases, PoolOptions 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, databases, options); + return (MSSQLPool) MSSQLDriver.INSTANCE.createPool(vertx, Utils.roundRobinSupplier(databases), options, new NetClientOptions()); } /** @@ -114,7 +117,7 @@ static MSSQLPool pool(Supplier> databases, PoolOptio * 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); + return (MSSQLPool) MSSQLDriver.INSTANCE.createPool(vertx, databases, options, null); } @Override 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 68757f574..8cdc4bd6c 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 @@ -21,6 +21,7 @@ import io.vertx.core.impl.ContextInternal; import io.vertx.core.impl.VertxInternal; import io.vertx.core.json.JsonObject; +import io.vertx.core.net.NetClientOptions; import io.vertx.mssqlclient.MSSQLConnectOptions; import io.vertx.mssqlclient.impl.MSSQLConnectionFactory; import io.vertx.mssqlclient.impl.MSSQLConnectionImpl; @@ -49,20 +50,20 @@ public MSSQLConnectOptions downcast(SqlConnectOptions connectOptions) { } @Override - public Pool newPool(Vertx vertx, Supplier> databases, PoolOptions options, CloseFuture closeFuture) { + public Pool newPool(Vertx vertx, Supplier> databases, PoolOptions options, NetClientOptions transportOptions, 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, databases, options, transportOptions, cf)); } else { - pool = newPoolImpl(vx, databases, options, closeFuture); + pool = newPoolImpl(vx, databases, options, transportOptions, closeFuture); } return new MSSQLPoolImpl(vx, closeFuture, pool); } - private PoolImpl newPoolImpl(VertxInternal vertx, Supplier> databases, PoolOptions options, CloseFuture closeFuture) { - PoolImpl pool = new PoolImpl(vertx, this, false, options, null, null, closeFuture); - ConnectionFactory factory = createConnectionFactory(vertx); + private PoolImpl newPoolImpl(VertxInternal vertx, Supplier> databases, PoolOptions poolOptions, NetClientOptions transportOptions, CloseFuture closeFuture) { + PoolImpl pool = new PoolImpl(vertx, this, false, poolOptions, null, null, closeFuture); + ConnectionFactory factory = createConnectionFactory(vertx, transportOptions); pool.connectionProvider(context -> factory.connect(context, databases.get())); pool.init(); closeFuture.add(factory); @@ -70,7 +71,7 @@ private PoolImpl newPoolImpl(VertxInternal vertx, Supplier createConnectionFactory(Vertx vertx) { + public ConnectionFactory createConnectionFactory(Vertx vertx, NetClientOptions transportOptions) { return new MSSQLConnectionFactory((VertxInternal) vertx); } diff --git a/vertx-mssql-client/src/test/java/io/vertx/mssqlclient/tck/ClientConfig.java b/vertx-mssql-client/src/test/java/io/vertx/mssqlclient/tck/ClientConfig.java index 573a5ce09..63cc9defc 100644 --- a/vertx-mssql-client/src/test/java/io/vertx/mssqlclient/tck/ClientConfig.java +++ b/vertx-mssql-client/src/test/java/io/vertx/mssqlclient/tck/ClientConfig.java @@ -11,17 +11,14 @@ package io.vertx.mssqlclient.tck; +import io.vertx.mssqlclient.MSSQLBuilder; import io.vertx.mssqlclient.MSSQLConnectOptions; import io.vertx.mssqlclient.MSSQLConnection; -import io.vertx.mssqlclient.MSSQLPool; import io.vertx.core.AsyncResult; import io.vertx.core.Future; import io.vertx.core.Handler; import io.vertx.core.Vertx; -import io.vertx.sqlclient.PoolOptions; -import io.vertx.sqlclient.SqlClient; -import io.vertx.sqlclient.SqlConnectOptions; -import io.vertx.sqlclient.SqlConnection; +import io.vertx.sqlclient.*; import io.vertx.sqlclient.tck.Connector; public enum ClientConfig { @@ -52,7 +49,10 @@ public void close() { POOLED() { @Override Connector connect(Vertx vertx, SqlConnectOptions options) { - MSSQLPool pool = MSSQLPool.pool(vertx, new MSSQLConnectOptions(options), new PoolOptions().setMaxSize(1)); + Pool pool = MSSQLBuilder.pool(builder -> builder + .with(new PoolOptions().setMaxSize(1)) + .connectingTo(new MSSQLConnectOptions(options)) + .using(vertx)); return new Connector() { @Override public void connect(Handler> handler) { diff --git a/vertx-mssql-client/src/test/java/io/vertx/mssqlclient/tck/MSSQLMetricsTest.java b/vertx-mssql-client/src/test/java/io/vertx/mssqlclient/tck/MSSQLMetricsTest.java index b77dd5d26..c8be77ff4 100644 --- a/vertx-mssql-client/src/test/java/io/vertx/mssqlclient/tck/MSSQLMetricsTest.java +++ b/vertx-mssql-client/src/test/java/io/vertx/mssqlclient/tck/MSSQLMetricsTest.java @@ -12,10 +12,9 @@ package io.vertx.mssqlclient.tck; import io.vertx.core.Vertx; -import io.vertx.mssqlclient.MSSQLPool; +import io.vertx.mssqlclient.MSSQLBuilder; import io.vertx.mssqlclient.junit.MSSQLRule; import io.vertx.sqlclient.Pool; -import io.vertx.sqlclient.PoolOptions; import io.vertx.sqlclient.tck.MetricsTestBase; import org.junit.ClassRule; @@ -26,7 +25,7 @@ public class MSSQLMetricsTest extends MetricsTestBase { @Override protected Pool createPool(Vertx vertx) { - return MSSQLPool.pool(vertx, rule.options(), new PoolOptions()); + return MSSQLBuilder.pool(builder -> builder.connectingTo(rule.options()).using(vertx)); } @Override diff --git a/vertx-mssql-client/src/test/java/io/vertx/mssqlclient/tck/MSSQLTracingTest.java b/vertx-mssql-client/src/test/java/io/vertx/mssqlclient/tck/MSSQLTracingTest.java index b887b3e0e..aa1139107 100644 --- a/vertx-mssql-client/src/test/java/io/vertx/mssqlclient/tck/MSSQLTracingTest.java +++ b/vertx-mssql-client/src/test/java/io/vertx/mssqlclient/tck/MSSQLTracingTest.java @@ -12,16 +12,12 @@ package io.vertx.mssqlclient.tck; import io.vertx.core.Vertx; -import io.vertx.ext.unit.TestContext; import io.vertx.ext.unit.junit.VertxUnitRunner; -import io.vertx.mssqlclient.MSSQLPool; +import io.vertx.mssqlclient.MSSQLBuilder; import io.vertx.mssqlclient.junit.MSSQLRule; import io.vertx.sqlclient.Pool; -import io.vertx.sqlclient.PoolOptions; import io.vertx.sqlclient.tck.TracingTestBase; import org.junit.ClassRule; -import org.junit.Ignore; -import org.junit.Test; import org.junit.runner.RunWith; @RunWith(VertxUnitRunner.class) @@ -32,7 +28,7 @@ public class MSSQLTracingTest extends TracingTestBase { @Override protected Pool createPool(Vertx vertx) { - return MSSQLPool.pool(vertx, rule.options(), new PoolOptions()); + return MSSQLBuilder.pool(builder -> builder.connectingTo(rule.options()).using(vertx)); } @Override diff --git a/vertx-mssql-client/src/test/java/io/vertx/mssqlclient/tck/MSSQLTransactionTest.java b/vertx-mssql-client/src/test/java/io/vertx/mssqlclient/tck/MSSQLTransactionTest.java index e09ec5fe5..e8ac5d699 100644 --- a/vertx-mssql-client/src/test/java/io/vertx/mssqlclient/tck/MSSQLTransactionTest.java +++ b/vertx-mssql-client/src/test/java/io/vertx/mssqlclient/tck/MSSQLTransactionTest.java @@ -12,8 +12,8 @@ import io.vertx.ext.unit.TestContext; import io.vertx.ext.unit.junit.VertxUnitRunner; +import io.vertx.mssqlclient.MSSQLBuilder; import io.vertx.mssqlclient.MSSQLConnectOptions; -import io.vertx.mssqlclient.MSSQLPool; import io.vertx.mssqlclient.junit.MSSQLRule; import io.vertx.sqlclient.Pool; import io.vertx.sqlclient.PoolOptions; @@ -31,12 +31,12 @@ public class MSSQLTransactionTest extends TransactionTestBase { @Override protected Pool createPool() { - return MSSQLPool.pool(vertx, new MSSQLConnectOptions(rule.options()), new PoolOptions().setMaxSize(1)); + return MSSQLBuilder.pool(builder -> builder.with(new PoolOptions().setMaxSize(1)).connectingTo(new MSSQLConnectOptions(rule.options())).using(vertx)); } @Override protected Pool nonTxPool() { - return MSSQLPool.pool(vertx, new MSSQLConnectOptions(rule.options()), new PoolOptions().setMaxSize(1)); + return MSSQLBuilder.pool(builder -> builder.with(new PoolOptions().setMaxSize(1)).connectingTo(new MSSQLConnectOptions(rule.options())).using(vertx)); } @Override diff --git a/vertx-mysql-client/src/main/asciidoc/index.adoc b/vertx-mysql-client/src/main/asciidoc/index.adoc index 38c89d346..4c577040c 100644 --- a/vertx-mysql-client/src/main/asciidoc/index.adoc +++ b/vertx-mysql-client/src/main/asciidoc/index.adoc @@ -116,7 +116,7 @@ Otherwise, the proxy might close client connections abruptly. == Pool versus pooled client -The {@link io.vertx.mysqlclient.MySQLPool} allows you to create a pool or a pooled client +The {@link io.vertx.mysqlclient.MySQLBuilder} allows you to create a pool or a pooled client [source,$lang] ---- diff --git a/vertx-mysql-client/src/main/java/examples/MySQLClientExamples.java b/vertx-mysql-client/src/main/java/examples/MySQLClientExamples.java index 66a06c621..97c3d5381 100644 --- a/vertx-mysql-client/src/main/java/examples/MySQLClientExamples.java +++ b/vertx-mysql-client/src/main/java/examples/MySQLClientExamples.java @@ -17,7 +17,6 @@ import io.vertx.core.json.JsonObject; import io.vertx.core.net.ClientSSLOptions; import io.vertx.core.net.PemTrustOptions; -import io.vertx.core.net.SSLOptions; import io.vertx.docgen.Source; import io.vertx.mysqlclient.*; import io.vertx.mysqlclient.data.spatial.Point; @@ -48,7 +47,11 @@ public void gettingStarted() { .setMaxSize(5); // Create the client pool - SqlClient client = MySQLPool.client(connectOptions, poolOptions); + SqlClient client = MySQLBuilder + .client() + .with(poolOptions) + .connectingTo(connectOptions) + .build(); // A simple query client @@ -81,7 +84,12 @@ public void configureFromDataObject(Vertx vertx) { PoolOptions poolOptions = new PoolOptions().setMaxSize(5); // Create the pool from the data object - MySQLPool pool = MySQLPool.pool(vertx, connectOptions, poolOptions); + Pool pool = MySQLBuilder + .pool() + .with(poolOptions) + .connectingTo(connectOptions) + .using(vertx) + .build(); pool.getConnection() .onComplete(ar -> { @@ -125,7 +133,11 @@ public void configureFromUri(Vertx vertx) { String connectionUri = "mysql://dbuser:secretpassword@database.server.com:3306/mydb"; // Create the pool from the connection URI - MySQLPool pool = MySQLPool.pool(connectionUri); + Pool pool = MySQLBuilder + .pool() + .connectingTo(connectionUri) + .using(vertx) + .build(); // Create the connection from the connection URI MySQLConnection.connect(vertx, connectionUri) @@ -149,7 +161,10 @@ public void connecting01() { .setMaxSize(5); // Create the pooled client - MySQLPool client = MySQLPool.pool(connectOptions, poolOptions); + Pool client = MySQLBuilder.pool() + .with(poolOptions) + .connectingTo(connectOptions) + .build(); } @@ -167,7 +182,11 @@ public void connecting02(Vertx vertx) { PoolOptions poolOptions = new PoolOptions() .setMaxSize(5); // Create the pooled client - MySQLPool client = MySQLPool.pool(vertx, connectOptions, poolOptions); + Pool client = MySQLBuilder.pool() + .with(poolOptions) + .connectingTo(connectOptions) + .using(vertx) + .build(); } public void connecting03(Pool pool) { @@ -191,7 +210,11 @@ public void connecting04(Vertx vertx) { .setMaxSize(5); // Create the pooled client - MySQLPool pool = MySQLPool.pool(vertx, connectOptions, poolOptions); + Pool pool = MySQLBuilder.pool() + .with(poolOptions) + .connectingTo(connectOptions) + .using(vertx) + .build(); // Get a connection from the pool pool.getConnection().compose(conn -> { @@ -219,20 +242,32 @@ public void connecting04(Vertx vertx) { } public void clientPipelining(Vertx vertx, MySQLConnectOptions connectOptions, PoolOptions poolOptions) { - MySQLPool pool = MySQLPool.pool(vertx, connectOptions.setPipeliningLimit(16), poolOptions); + Pool pool = MySQLBuilder.pool() + .with(poolOptions) + .connectingTo(connectOptions.setPipeliningLimit(16)) + .using(vertx) + .build(); } public void poolVersusPooledClient(Vertx vertx, String sql, MySQLConnectOptions connectOptions, PoolOptions poolOptions) { // Pooled client connectOptions.setPipeliningLimit(64); - SqlClient client = MySQLPool.client(vertx, connectOptions, poolOptions); + SqlClient client = MySQLBuilder.client() + .with(poolOptions) + .connectingTo(connectOptions) + .using(vertx) + .build(); // Pipelined Future> res1 = client.query(sql).execute(); // Connection pool - MySQLPool pool = MySQLPool.pool(vertx, connectOptions, poolOptions); + Pool pool = MySQLBuilder.pool() + .with(poolOptions) + .connectingTo(connectOptions) + .using(vertx) + .build(); // Not pipelined Future> res2 = pool.query(sql).execute(); @@ -250,12 +285,20 @@ public void connectWithUnixDomainSocket(Vertx vertx) { .setMaxSize(5); // Create the pooled client - MySQLPool client = MySQLPool.pool(connectOptions, poolOptions); + Pool client = MySQLBuilder.pool() + .with(poolOptions) + .connectingTo(connectOptions) + .using(vertx) + .build(); // Create the pooled client with a vertx instance // Make sure the vertx instance has enabled native transports // vertxOptions.setPreferNativeTransport(true); - MySQLPool client2 = MySQLPool.pool(vertx, connectOptions, poolOptions); + Pool client2 = MySQLBuilder.pool() + .with(poolOptions) + .connectingTo(connectOptions) + .using(vertx) + .build(); } public void reconnectAttempts(MySQLConnectOptions options) { diff --git a/vertx-mysql-client/src/main/java/examples/SqlClientExamples.java b/vertx-mysql-client/src/main/java/examples/SqlClientExamples.java index c55696a68..ba97b8155 100644 --- a/vertx-mysql-client/src/main/java/examples/SqlClientExamples.java +++ b/vertx-mysql-client/src/main/java/examples/SqlClientExamples.java @@ -22,11 +22,9 @@ import io.vertx.core.Vertx; import io.vertx.core.tracing.TracingPolicy; import io.vertx.docgen.Source; +import io.vertx.mysqlclient.MySQLBuilder; import io.vertx.mysqlclient.MySQLConnectOptions; -import io.vertx.mysqlclient.MySQLPool; -import io.vertx.mysqlclient.spi.MySQLDriver; import io.vertx.sqlclient.*; -import io.vertx.sqlclient.spi.ConnectionFactory; import java.util.ArrayList; import java.util.Arrays; @@ -340,12 +338,16 @@ public void tracing01(MySQLConnectOptions options) { options.setTracingPolicy(TracingPolicy.ALWAYS); } - public void poolConfig01(MySQLConnectOptions server1, MySQLConnectOptions server2, MySQLConnectOptions server3, PoolOptions options) { - MySQLPool pool = MySQLPool.pool(Arrays.asList(server1, server2, server3), options); + public void poolConfig01(Vertx vertx, MySQLConnectOptions server1, MySQLConnectOptions server2, MySQLConnectOptions server3, PoolOptions options) { + Pool pool = MySQLBuilder.pool() + .with(options) + .connectingTo(Arrays.asList(server1, server2, server3)) + .using(vertx) + .build(); } - public void poolConfig02(MySQLPool pool, String sql) { - pool.connectHandler(conn -> { + public void poolConfig02(ClientBuilder builder, String sql) { + builder.withConnectHandler(conn -> { conn.query(sql).execute().onSuccess(res -> { // Release the connection to the pool, ready to be used by the application conn.close(); @@ -354,7 +356,11 @@ public void poolConfig02(MySQLPool pool, String sql) { } public void poolSharing1(Vertx vertx, MySQLConnectOptions database, int maxSize) { - MySQLPool pool = MySQLPool.pool(database, new PoolOptions().setMaxSize(maxSize)); + Pool pool = MySQLBuilder.pool() + .with(new PoolOptions().setMaxSize(maxSize)) + .connectingTo(database) + .using(vertx) + .build(); vertx.deployVerticle(() -> new AbstractVerticle() { @Override public void start() throws Exception { @@ -365,36 +371,45 @@ public void start() throws Exception { public void poolSharing2(Vertx vertx, MySQLConnectOptions database, int maxSize) { vertx.deployVerticle(() -> new AbstractVerticle() { - MySQLPool pool; + Pool pool; @Override public void start() { // Get or create a shared pool // this actually creates a lease to the pool // when the verticle is undeployed, the lease will be released automaticaly - pool = MySQLPool.pool(database, new PoolOptions() - .setMaxSize(maxSize) - .setShared(true) - .setName("my-pool")); + pool = MySQLBuilder.pool() + .with(new PoolOptions() + .setMaxSize(maxSize) + .setShared(true) + .setName("my-pool")) + .using(vertx) + .build(); } }, new DeploymentOptions().setInstances(4)); } public static void poolSharing3(Vertx vertx, MySQLConnectOptions database, int maxSize) { - MySQLPool pool = MySQLPool.pool(database, new PoolOptions() - .setMaxSize(maxSize) - .setShared(true) - .setName("my-pool") - .setEventLoopSize(4)); + Pool pool = MySQLBuilder.pool() + .with(new PoolOptions() + .setMaxSize(maxSize) + .setShared(true) + .setName("my-pool") + .setEventLoopSize(4)) + .using(vertx) + .build(); } public void dynamicPoolConfig(Vertx vertx, PoolOptions poolOptions) { - MySQLPool pool = MySQLPool.pool(vertx, () -> { - Future connectOptions = retrieveOptions(); - return connectOptions; - }, poolOptions); + Pool pool = MySQLBuilder.pool() + .connectingTo(() -> { + Future connectOptions = retrieveOptions(); + return connectOptions; + }) + .using(vertx) + .build(); } - private Future retrieveOptions() { + private Future retrieveOptions() { return null; } } 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 new file mode 100644 index 000000000..0aa5fa16c --- /dev/null +++ b/vertx-mysql-client/src/main/java/io/vertx/mysqlclient/MySQLBuilder.java @@ -0,0 +1,93 @@ +/* + * 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.mysqlclient; + +import io.vertx.codegen.annotations.VertxGen; +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.*; +import io.vertx.sqlclient.impl.ClientBuilderBase; + +import java.util.function.Supplier; + +/** + * Entry point for building MySQL clients. + */ +@VertxGen +public interface MySQLBuilder { + + /** + * Build a pool with the specified {@code block} argument. + * The {@code block} argument is usually a lambda that configures the provided builder + *

+ * Example usage: {@code Pool pool = PgBuilder.pool(builder -> builder.connectingTo(connectOptions));} + * + * @return the pool as configured by the code {@code block} + */ + static Pool pool(Handler> block) { + return ClientBuilder.pool(MySQLDriver.INSTANCE, block); + } + + /** + * Provide a builder for MySQL pool of connections + *

+ * Example usage: {@code Pool pool = PgBuilder.pool().connectingTo(connectOptions).build()} + */ + static ClientBuilder pool() { + return ClientBuilder.pool(MySQLDriver.INSTANCE); + } + + /** + * Build a client backed by a connection pool with the specified {@code block} argument. + * The {@code block} argument is usually a lambda that configures the provided builder + *

+ * Example usage: {@code SqlClient client = PgBuilder.client(builder -> builder.connectingTo(connectOptions));} + * + * @return the client as configured by the code {@code block} + */ + static SqlClient client(Handler> handler) { + ClientBuilder builder = client(); + handler.handle(builder); + return builder.build(); + } + + /** + * Provide a builder for MySQL client backed by a connection pool. + *

+ * Example usage: {@code SqlClient client = PgBuilder.client().connectingTo(connectOptions).build()} + */ + static ClientBuilder client() { + return new ClientBuilderBase(MySQLDriver.INSTANCE) { + @Override + public ClientBuilder with(PoolOptions options) { + if (options != null) { + options = new MySQLPoolOptions(options).setPipelined(true); + } + return super.with(options); + } + @Override + protected SqlClient create(Vertx vertx, Supplier> databases, PoolOptions poolOptions, NetClientOptions transportOptions) { + return driver.createPool(vertx, databases, poolOptions, transportOptions); + } + }; + } +} diff --git a/vertx-mysql-client/src/main/java/io/vertx/mysqlclient/MySQLConnectOptions.java b/vertx-mysql-client/src/main/java/io/vertx/mysqlclient/MySQLConnectOptions.java index 9af09ddc3..94b3756dd 100644 --- a/vertx-mysql-client/src/main/java/io/vertx/mysqlclient/MySQLConnectOptions.java +++ b/vertx-mysql-client/src/main/java/io/vertx/mysqlclient/MySQLConnectOptions.java @@ -27,7 +27,7 @@ import java.util.function.Predicate; /** - * Connect options for configuring {@link MySQLConnection} or {@link MySQLPool}. + * Connect options for configuring {@link MySQLConnection} or {@link MySQLBuilder}. */ @DataObject(generateConverter = true) public class MySQLConnectOptions extends SqlConnectOptions { 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 index 464e3c0ee..fa58176fe 100644 --- a/vertx-mysql-client/src/main/java/io/vertx/mysqlclient/MySQLPool.java +++ b/vertx-mysql-client/src/main/java/io/vertx/mysqlclient/MySQLPool.java @@ -17,6 +17,7 @@ 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; @@ -24,6 +25,7 @@ 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; @@ -34,6 +36,7 @@ /** * A {@link Pool pool} of {@link MySQLConnection MySQL Connections}. */ +@Deprecated @VertxGen public interface MySQLPool extends Pool { @@ -99,7 +102,7 @@ static MySQLPool pool(List databases, PoolOptions 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, databases, options); + return (MySQLPool) MySQLDriver.INSTANCE.createPool(vertx, Utils.roundRobinSupplier(databases), options, new NetClientOptions()); } /** @@ -118,7 +121,7 @@ static MySQLPool pool(Supplier> databases, PoolOptio * 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); + return (MySQLPool) MySQLDriver.INSTANCE.createPool(vertx, databases, options, new NetClientOptions()); } /** * Like {@link #client(String, PoolOptions)} with a default {@code poolOptions}. @@ -169,7 +172,7 @@ static SqlClient client(Vertx vertx, MySQLConnectOptions connectOptions, PoolOpt * 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, mySQLConnectOptions, new MySQLPoolOptions(options).setPipelined(true)); + return MySQLDriver.INSTANCE.createPool(vertx, Utils.roundRobinSupplier(mySQLConnectOptions), new MySQLPoolOptions(options).setPipelined(true), new NetClientOptions()); } /** @@ -188,7 +191,7 @@ static SqlClient client(List databases, PoolOptions 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)); + return MySQLDriver.INSTANCE.createPool(vertx, mySQLConnectOptions, new MySQLPoolOptions(options).setPipelined(true), new NetClientOptions()); } /** 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 index c2700975e..a5f25437f 100644 --- 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 @@ -13,11 +13,12 @@ import io.vertx.core.impl.CloseFuture; import io.vertx.core.impl.VertxInternal; -import io.vertx.mysqlclient.MySQLPool; + +import io.vertx.mysqlclient.MySQLBuilder; import io.vertx.sqlclient.Pool; import io.vertx.sqlclient.impl.PoolBase; -public class MySQLPoolImpl extends PoolBase implements MySQLPool { +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 7304017ad..bc63c79a6 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 @@ -21,6 +21,7 @@ import io.vertx.core.impl.ContextInternal; import io.vertx.core.impl.VertxInternal; import io.vertx.core.json.JsonObject; +import io.vertx.core.net.NetClientOptions; import io.vertx.mysqlclient.MySQLConnectOptions; import io.vertx.mysqlclient.impl.*; import io.vertx.sqlclient.Pool; @@ -46,21 +47,21 @@ public MySQLConnectOptions downcast(SqlConnectOptions connectOptions) { } @Override - public Pool newPool(Vertx vertx, Supplier> databases, PoolOptions options, CloseFuture closeFuture) { + public Pool newPool(Vertx vertx, Supplier> databases, PoolOptions options, NetClientOptions transportOptions, 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, databases, options, transportOptions, cf)); } else { - pool = newPoolImpl(vx, databases, options, closeFuture); + pool = newPoolImpl(vx, databases, options, transportOptions, closeFuture); } return new MySQLPoolImpl(vx, closeFuture, pool); } - private PoolImpl newPoolImpl(VertxInternal vertx, Supplier> databases, PoolOptions options, CloseFuture closeFuture) { - boolean pipelinedPool = options instanceof MySQLPoolOptions && ((MySQLPoolOptions) options).isPipelined(); - PoolImpl pool = new PoolImpl(vertx, this, pipelinedPool, options, null, null, closeFuture); - ConnectionFactory factory = createConnectionFactory(vertx); + private PoolImpl newPoolImpl(VertxInternal vertx, 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())); pool.init(); closeFuture.add(factory); @@ -79,7 +80,7 @@ public boolean acceptsOptions(SqlConnectOptions options) { } @Override - public ConnectionFactory createConnectionFactory(Vertx vertx) { + public ConnectionFactory createConnectionFactory(Vertx vertx, NetClientOptions transportOptions) { return new MySQLConnectionFactory((VertxInternal) vertx); } diff --git a/vertx-mysql-client/src/test/java/io/vertx/mysqlclient/MySQLBatchInsertExceptionTestBase.java b/vertx-mysql-client/src/test/java/io/vertx/mysqlclient/MySQLBatchInsertExceptionTestBase.java index 2c793a61c..819a60a95 100644 --- a/vertx-mysql-client/src/test/java/io/vertx/mysqlclient/MySQLBatchInsertExceptionTestBase.java +++ b/vertx-mysql-client/src/test/java/io/vertx/mysqlclient/MySQLBatchInsertExceptionTestBase.java @@ -57,7 +57,7 @@ public void testBatchInsertExceptionConn(TestContext ctx) { @Test public void testBatchInsertExceptionPool(TestContext ctx) { - SqlClient client = MySQLPool.client(vertx, options, new PoolOptions().setMaxSize(8)); + SqlClient client = MySQLBuilder.client(builder -> builder.with(new PoolOptions().setMaxSize(8)).connectingTo(options).using(vertx));; testBatchInsertException(ctx, client); } diff --git a/vertx-mysql-client/src/test/java/io/vertx/mysqlclient/MySQLPoolTest.java b/vertx-mysql-client/src/test/java/io/vertx/mysqlclient/MySQLPoolTest.java index 4da203ce0..8e81125ad 100644 --- a/vertx-mysql-client/src/test/java/io/vertx/mysqlclient/MySQLPoolTest.java +++ b/vertx-mysql-client/src/test/java/io/vertx/mysqlclient/MySQLPoolTest.java @@ -19,6 +19,7 @@ import io.vertx.ext.unit.junit.Repeat; import io.vertx.ext.unit.junit.RepeatRule; import io.vertx.ext.unit.junit.VertxUnitRunner; +import io.vertx.sqlclient.Pool; import io.vertx.sqlclient.PoolOptions; import io.vertx.sqlclient.Row; import io.vertx.sqlclient.Tuple; @@ -42,7 +43,7 @@ public class MySQLPoolTest extends MySQLTestBase { Vertx vertx; MySQLConnectOptions options; - MySQLPool pool; + Pool pool; @Rule public RepeatRule rule = new RepeatRule(); @@ -51,7 +52,7 @@ public class MySQLPoolTest extends MySQLTestBase { public void setup() { vertx = Vertx.vertx(); options = new MySQLConnectOptions(MySQLTestBase.options); - pool = MySQLPool.pool(vertx, options, new PoolOptions()); + pool = MySQLBuilder.pool(builder -> builder.connectingTo(options).using(vertx)); } @After @@ -109,7 +110,7 @@ public void testContinuouslyQuery(TestContext ctx) { @Test public void testConcurrentMultipleConnection(TestContext ctx) { PoolOptions poolOptions = new PoolOptions().setMaxSize(2); - MySQLPool pool = MySQLPool.pool(vertx, new MySQLConnectOptions(this.options).setCachePreparedStatements(false), poolOptions); + Pool pool = MySQLBuilder.pool(builder -> builder.with(poolOptions).connectingTo(new MySQLConnectOptions(this.options).setCachePreparedStatements(false)).using(vertx)); try { int numRequests = 1500; Async async = ctx.async(numRequests); @@ -135,7 +136,7 @@ public void testConcurrentMultipleConnection(TestContext ctx) { public void testBorrowedPooledConnectionClosedByServer(TestContext ctx) { Async async = ctx.async(); PoolOptions poolOptions = new PoolOptions().setMaxSize(1); - MySQLPool pool = MySQLPool.pool(vertx, new MySQLConnectOptions(this.options).setCachePreparedStatements(false), poolOptions); + Pool pool = MySQLBuilder.pool(builder -> builder.with(poolOptions).connectingTo(new MySQLConnectOptions(this.options).setCachePreparedStatements(false)).using(vertx)); pool .getConnection() .onComplete(ctx.asyncAssertSuccess(conn -> { @@ -170,7 +171,7 @@ public void testBorrowedPooledConnectionClosedByServer(TestContext ctx) { public void testPooledConnectionClosedByServer(TestContext ctx) { Async async = ctx.async(); PoolOptions poolOptions = new PoolOptions().setMaxSize(1); - MySQLPool pool = MySQLPool.pool(vertx, new MySQLConnectOptions(this.options).setCachePreparedStatements(false), poolOptions); + Pool pool = MySQLBuilder.pool(builder -> builder.with(poolOptions).connectingTo(new MySQLConnectOptions(this.options).setCachePreparedStatements(false)).using(vertx)); pool .getConnection() .onComplete(ctx.asyncAssertSuccess(conn -> { @@ -225,7 +226,7 @@ public void testNoConnectionLeaks(TestContext ctx) { .setIdleTimeout(idleTimeout) .setIdleTimeoutUnit(TimeUnit.MILLISECONDS) .setPoolCleanerPeriod(5); - pool = MySQLPool.pool(options, poolOptions); + pool = MySQLBuilder.pool(builder -> builder.with(poolOptions).connectingTo(options).using(vertx)); Async async = ctx.async(); AtomicInteger cid = new AtomicInteger(); diff --git a/vertx-mysql-client/src/test/java/io/vertx/mysqlclient/MySQLPooledConnectionTest.java b/vertx-mysql-client/src/test/java/io/vertx/mysqlclient/MySQLPooledConnectionTest.java index 6c7cfc234..2f92a07fa 100644 --- a/vertx-mysql-client/src/test/java/io/vertx/mysqlclient/MySQLPooledConnectionTest.java +++ b/vertx-mysql-client/src/test/java/io/vertx/mysqlclient/MySQLPooledConnectionTest.java @@ -17,10 +17,7 @@ import io.vertx.ext.unit.Async; import io.vertx.ext.unit.TestContext; import io.vertx.ext.unit.junit.VertxUnitRunner; -import io.vertx.sqlclient.Cursor; -import io.vertx.sqlclient.PoolOptions; -import io.vertx.sqlclient.Row; -import io.vertx.sqlclient.SqlConnection; +import io.vertx.sqlclient.*; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -33,17 +30,17 @@ public class MySQLPooledConnectionTest extends MySQLTestBase { Vertx vertx; MySQLConnectOptions options; - MySQLPool pool; + Pool pool; Consumer>> connector; @Before public void setup() { vertx = Vertx.vertx(); options = new MySQLConnectOptions(MySQLTestBase.options); - pool = MySQLPool.pool(vertx, options, new PoolOptions()); + pool = MySQLBuilder.pool(builder -> builder.connectingTo(options).using(vertx)); connector = handler -> { if (pool == null) { - pool = MySQLPool.pool(vertx, options, new PoolOptions().setMaxSize(1)); + pool = MySQLBuilder.pool(builder -> builder.with(new PoolOptions().setMaxSize(1)).connectingTo(options).using(vertx)); } pool .getConnection() diff --git a/vertx-mysql-client/src/test/java/io/vertx/mysqlclient/MySQLTLSTest.java b/vertx-mysql-client/src/test/java/io/vertx/mysqlclient/MySQLTLSTest.java index 680cac4ec..b908c975a 100644 --- a/vertx-mysql-client/src/test/java/io/vertx/mysqlclient/MySQLTLSTest.java +++ b/vertx-mysql-client/src/test/java/io/vertx/mysqlclient/MySQLTLSTest.java @@ -19,6 +19,7 @@ import io.vertx.ext.unit.TestContext; import io.vertx.ext.unit.junit.VertxUnitRunner; import io.vertx.mysqlclient.junit.MySQLRule; +import io.vertx.sqlclient.Pool; import io.vertx.sqlclient.PoolOptions; import io.vertx.sqlclient.Row; import org.junit.After; @@ -180,7 +181,7 @@ public void testPoolSuccessWithRequiredSslMode(TestContext ctx) { .setCertPath("tls/files/client-cert.pem") .setKeyPath("tls/files/client-key.pem")); - MySQLPool pool = MySQLPool.pool(vertx, options, new PoolOptions().setMaxSize(5)); + Pool pool = MySQLBuilder.pool(builder -> builder.with(new PoolOptions().setMaxSize(5)).connectingTo(options).using(vertx)); pool.withConnection(conn1 -> { return pool.withConnection(conn2 -> { @@ -274,7 +275,7 @@ public void testPoolFailWithVerifyCaSslMode(TestContext ctx) { .setKeyPath("tls/files/client-key.pem")); try { - MySQLPool.pool(vertx, options, new PoolOptions()); + MySQLBuilder.pool(builder -> builder.connectingTo(options).using(vertx)); } catch (IllegalArgumentException e) { ctx.assertEquals("Trust options must be specified under VERIFY_CA ssl-mode.", e.getMessage()); } diff --git a/vertx-mysql-client/src/test/java/io/vertx/mysqlclient/MySQLUnixDomainSocketTest.java b/vertx-mysql-client/src/test/java/io/vertx/mysqlclient/MySQLUnixDomainSocketTest.java index 340a00970..6c9acff43 100644 --- a/vertx-mysql-client/src/test/java/io/vertx/mysqlclient/MySQLUnixDomainSocketTest.java +++ b/vertx-mysql-client/src/test/java/io/vertx/mysqlclient/MySQLUnixDomainSocketTest.java @@ -15,7 +15,7 @@ import io.vertx.ext.unit.Async; import io.vertx.ext.unit.TestContext; import io.vertx.ext.unit.junit.VertxUnitRunner; -import io.vertx.sqlclient.PoolOptions; +import io.vertx.sqlclient.Pool; import io.vertx.sqlclient.SqlClient; import org.junit.After; import org.junit.Before; @@ -33,7 +33,7 @@ public class MySQLUnixDomainSocketTest extends MySQLTestBase { private static final String unixSocketFile = System.getProperty("unix.socket.file"); - private MySQLPool client; + private Pool client; private MySQLConnectOptions options; private Vertx vertx; @@ -74,7 +74,7 @@ public void uriSocketAttributeTest(TestContext context) throws UnsupportedEncodi } private void uriTest(TestContext context, String uri) throws UnsupportedEncodingException { - client = MySQLPool.pool(vertx, uri); + client = MySQLBuilder.pool(builder -> builder.connectingTo(uri).using(vertx)); client .getConnection() .onComplete(context.asyncAssertSuccess(SqlClient::close)); @@ -82,7 +82,7 @@ private void uriTest(TestContext context, String uri) throws UnsupportedEncoding @Test public void simpleConnect(TestContext context) { - client = MySQLPool.pool(vertx, new MySQLConnectOptions(options), new PoolOptions()); + client = MySQLBuilder.pool(builder -> builder.connectingTo(new MySQLConnectOptions(options)).using(vertx)); client .getConnection() .onComplete(context.asyncAssertSuccess(SqlClient::close)); @@ -92,7 +92,7 @@ public void simpleConnect(TestContext context) { public void connectWithVertxInstance(TestContext context) { Vertx vertx = Vertx.vertx(new VertxOptions().setPreferNativeTransport(true)); try { - client = MySQLPool.pool(vertx, new MySQLConnectOptions(options), new PoolOptions()); + client = MySQLBuilder.pool(builder -> builder.connectingTo(new MySQLConnectOptions(options)).using(vertx)); Async async = context.async(); client .getConnection() @@ -108,7 +108,7 @@ public void connectWithVertxInstance(TestContext context) { @Test public void testIgnoreSslMode(TestContext context) { - client = MySQLPool.pool(vertx, new MySQLConnectOptions(options).setSslMode(SslMode.REQUIRED), new PoolOptions()); + client = MySQLBuilder.pool(builder -> builder.connectingTo(new MySQLConnectOptions(options).setSslMode(SslMode.REQUIRED)).using(vertx)); client .getConnection() .onComplete(context.asyncAssertSuccess(conn -> { diff --git a/vertx-mysql-client/src/test/java/io/vertx/mysqlclient/tck/ClientConfig.java b/vertx-mysql-client/src/test/java/io/vertx/mysqlclient/tck/ClientConfig.java index 59527b43f..3f6c85fbf 100644 --- a/vertx-mysql-client/src/test/java/io/vertx/mysqlclient/tck/ClientConfig.java +++ b/vertx-mysql-client/src/test/java/io/vertx/mysqlclient/tck/ClientConfig.java @@ -20,14 +20,11 @@ import io.vertx.core.Future; import io.vertx.core.Handler; import io.vertx.core.Vertx; +import io.vertx.mysqlclient.MySQLBuilder; import io.vertx.mysqlclient.MySQLConnectOptions; import io.vertx.mysqlclient.MySQLConnection; -import io.vertx.mysqlclient.MySQLPool; -import io.vertx.sqlclient.PoolOptions; +import io.vertx.sqlclient.*; import io.vertx.sqlclient.tck.Connector; -import io.vertx.sqlclient.SqlClient; -import io.vertx.sqlclient.SqlConnectOptions; -import io.vertx.sqlclient.SqlConnection; public enum ClientConfig { @@ -56,7 +53,7 @@ public void close() { POOLED() { @Override Connector connect(Vertx vertx, SqlConnectOptions options) { - MySQLPool pool = MySQLPool.pool(vertx, new MySQLConnectOptions(options), new PoolOptions().setMaxSize(1)); + Pool pool = MySQLBuilder.pool(builder -> builder.with(new PoolOptions().setMaxSize(1)).connectingTo(options).using(vertx)); return new Connector() { @Override public void connect(Handler> handler) { diff --git a/vertx-mysql-client/src/test/java/io/vertx/mysqlclient/tck/MySQLPipeliningQueryTest.java b/vertx-mysql-client/src/test/java/io/vertx/mysqlclient/tck/MySQLPipeliningQueryTest.java index 50567f395..5dacf737a 100644 --- a/vertx-mysql-client/src/test/java/io/vertx/mysqlclient/tck/MySQLPipeliningQueryTest.java +++ b/vertx-mysql-client/src/test/java/io/vertx/mysqlclient/tck/MySQLPipeliningQueryTest.java @@ -1,8 +1,8 @@ package io.vertx.mysqlclient.tck; import io.vertx.ext.unit.junit.VertxUnitRunner; +import io.vertx.mysqlclient.MySQLBuilder; import io.vertx.mysqlclient.MySQLConnectOptions; -import io.vertx.mysqlclient.MySQLPool; import io.vertx.mysqlclient.junit.MySQLRule; import io.vertx.sqlclient.PoolOptions; import io.vertx.sqlclient.tck.PipeliningQueryTestBase; @@ -22,7 +22,7 @@ protected void init() { mySQLConnectOptions.setPipeliningLimit(64); connectionConnector = ClientConfig.CONNECT.connect(vertx, options); pooledConnectionConnector = ClientConfig.POOLED.connect(vertx, options); - pooledClientSupplier = () -> MySQLPool.client(vertx, (MySQLConnectOptions) options, new PoolOptions().setMaxSize(8)); + pooledClientSupplier = () -> MySQLBuilder.client(builder -> builder.with(new PoolOptions().setMaxSize(8)).connectingTo(options).using(vertx));; } @Override diff --git a/vertx-mysql-client/src/test/java/io/vertx/mysqlclient/tck/MySQLTracingTest.java b/vertx-mysql-client/src/test/java/io/vertx/mysqlclient/tck/MySQLTracingTest.java index 788ab15ce..bbede0eb6 100644 --- a/vertx-mysql-client/src/test/java/io/vertx/mysqlclient/tck/MySQLTracingTest.java +++ b/vertx-mysql-client/src/test/java/io/vertx/mysqlclient/tck/MySQLTracingTest.java @@ -13,10 +13,9 @@ import io.vertx.core.Vertx; import io.vertx.ext.unit.junit.VertxUnitRunner; -import io.vertx.mysqlclient.MySQLPool; +import io.vertx.mysqlclient.MySQLBuilder; import io.vertx.mysqlclient.junit.MySQLRule; import io.vertx.sqlclient.Pool; -import io.vertx.sqlclient.PoolOptions; import io.vertx.sqlclient.tck.TracingTestBase; import org.junit.ClassRule; import org.junit.runner.RunWith; @@ -29,7 +28,7 @@ public class MySQLTracingTest extends TracingTestBase { @Override protected Pool createPool(Vertx vertx) { - return MySQLPool.pool(vertx, rule.options(), new PoolOptions()); + return MySQLBuilder.pool(builder -> builder.connectingTo(rule.options()).using(vertx)); } @Override diff --git a/vertx-mysql-client/src/test/java/io/vertx/mysqlclient/tck/MySQLTransactionTest.java b/vertx-mysql-client/src/test/java/io/vertx/mysqlclient/tck/MySQLTransactionTest.java index ac18ce8ff..791cd07e6 100644 --- a/vertx-mysql-client/src/test/java/io/vertx/mysqlclient/tck/MySQLTransactionTest.java +++ b/vertx-mysql-client/src/test/java/io/vertx/mysqlclient/tck/MySQLTransactionTest.java @@ -16,8 +16,7 @@ package io.vertx.mysqlclient.tck; import io.vertx.ext.unit.junit.VertxUnitRunner; -import io.vertx.mysqlclient.MySQLConnectOptions; -import io.vertx.mysqlclient.MySQLPool; +import io.vertx.mysqlclient.MySQLBuilder; import io.vertx.mysqlclient.junit.MySQLRule; import io.vertx.sqlclient.Pool; import io.vertx.sqlclient.PoolOptions; @@ -33,12 +32,12 @@ public class MySQLTransactionTest extends TransactionTestBase { @Override protected Pool createPool() { - return MySQLPool.pool(vertx, new MySQLConnectOptions(rule.options()), new PoolOptions().setMaxSize(1)); + return MySQLBuilder.pool(builder -> builder.with(new PoolOptions().setMaxSize(1)).connectingTo(rule.options()).using(vertx)); } @Override protected Pool nonTxPool() { - return MySQLPool.pool(vertx, new MySQLConnectOptions(rule.options()), new PoolOptions().setMaxSize(1)); + return MySQLBuilder.pool(builder -> builder.with(new PoolOptions().setMaxSize(1)).connectingTo(rule.options()).using(vertx)); } @Override diff --git a/vertx-oracle-client/src/main/java/examples/OracleClientExamples.java b/vertx-oracle-client/src/main/java/examples/OracleClientExamples.java index de4afaaaa..944a6c119 100644 --- a/vertx-oracle-client/src/main/java/examples/OracleClientExamples.java +++ b/vertx-oracle-client/src/main/java/examples/OracleClientExamples.java @@ -16,10 +16,7 @@ import io.vertx.core.json.JsonArray; import io.vertx.core.json.JsonObject; import io.vertx.docgen.Source; -import io.vertx.oracleclient.OracleClient; -import io.vertx.oracleclient.OracleConnectOptions; -import io.vertx.oracleclient.OraclePool; -import io.vertx.oracleclient.OraclePrepareOptions; +import io.vertx.oracleclient.*; import io.vertx.oracleclient.data.Blob; import io.vertx.sqlclient.*; import io.vertx.sqlclient.data.Numeric; @@ -49,7 +46,10 @@ public void gettingStarted() { .setMaxSize(5); // Create the client pool - OraclePool client = OraclePool.pool(connectOptions, poolOptions); + Pool client = OracleBuilder.pool() + .with(poolOptions) + .connectingTo(connectOptions) + .build(); // A simple query client @@ -82,7 +82,11 @@ public void configureFromDataObject(Vertx vertx) { PoolOptions poolOptions = new PoolOptions().setMaxSize(5); // Create the pool from the data object - OraclePool pool = OraclePool.pool(vertx, connectOptions, poolOptions); + Pool pool = OracleBuilder.pool() + .with(poolOptions) + .connectingTo(connectOptions) + .using(vertx) + .build(); pool .getConnection() @@ -105,7 +109,11 @@ public void configureFromUri(Vertx vertx) { PoolOptions poolOptions = new PoolOptions().setMaxSize(5); // Create the pool from the connection URI - OraclePool pool = OraclePool.pool(vertx, connectOptions, poolOptions); + Pool pool = OracleBuilder.pool() + .with(poolOptions) + .connectingTo(connectOptions) + .using(vertx) + .build(); } public void configureFromTnsAliasUri(Vertx vertx) { @@ -122,10 +130,14 @@ public void configureFromTnsAliasUri(Vertx vertx) { PoolOptions poolOptions = new PoolOptions().setMaxSize(5); // Create the pool from the connection URI - OraclePool pool = OraclePool.pool(vertx, connectOptions, poolOptions); + Pool pool = OracleBuilder.pool() + .with(poolOptions) + .connectingTo(connectOptions) + .using(vertx) + .build(); } - public void connecting01() { + public void connecting01(Vertx vertx) { // Connect options OracleConnectOptions connectOptions = new OracleConnectOptions() @@ -140,7 +152,11 @@ public void connecting01() { .setMaxSize(5); // Create the pooled client - OraclePool client = OraclePool.pool(connectOptions, poolOptions); + Pool client = OracleBuilder.pool() + .with(poolOptions) + .connectingTo(connectOptions) + .using(vertx) + .build(); } @@ -158,7 +174,11 @@ public void connecting02(Vertx vertx) { PoolOptions poolOptions = new PoolOptions() .setMaxSize(5); // Create the pooled client - OraclePool client = OraclePool.pool(vertx, connectOptions, poolOptions); + Pool client = OracleBuilder.pool() + .with(poolOptions) + .connectingTo(connectOptions) + .using(vertx) + .build(); } public void connecting03(Pool pool) { @@ -182,7 +202,11 @@ public void connecting04(Vertx vertx) { .setMaxSize(5); // Create the pooled client - OraclePool client = OraclePool.pool(vertx, connectOptions, poolOptions); + Pool client = OracleBuilder.pool() + .with(poolOptions) + .connectingTo(connectOptions) + .using(vertx) + .build(); // Get a connection from the pool client.getConnection().compose(conn -> { diff --git a/vertx-oracle-client/src/main/java/examples/SqlClientExamples.java b/vertx-oracle-client/src/main/java/examples/SqlClientExamples.java index 00017ebb4..ed8e16ccf 100644 --- a/vertx-oracle-client/src/main/java/examples/SqlClientExamples.java +++ b/vertx-oracle-client/src/main/java/examples/SqlClientExamples.java @@ -20,8 +20,8 @@ import io.vertx.core.Vertx; import io.vertx.core.tracing.TracingPolicy; import io.vertx.docgen.Source; +import io.vertx.oracleclient.OracleBuilder; import io.vertx.oracleclient.OracleConnectOptions; -import io.vertx.oracleclient.OraclePool; import io.vertx.sqlclient.*; import java.util.ArrayList; @@ -337,13 +337,17 @@ public void tracing01(OracleConnectOptions options) { } public void dynamicPoolConfig(Vertx vertx, PoolOptions poolOptions) { - OraclePool pool = OraclePool.pool(vertx, () -> { - Future connectOptions = retrieveOptions(); - return connectOptions; - }, poolOptions); + Pool pool = OracleBuilder.pool() + .with(poolOptions) + .connectingTo(() -> { + Future connectOptions = retrieveOptions(); + return connectOptions; + }) + .using(vertx) + .build(); } - private Future retrieveOptions() { + private Future retrieveOptions() { return null; } } diff --git a/vertx-oracle-client/src/main/java/io/vertx/oracleclient/OracleBuilder.java b/vertx-oracle-client/src/main/java/io/vertx/oracleclient/OracleBuilder.java new file mode 100644 index 000000000..6cac30640 --- /dev/null +++ b/vertx-oracle-client/src/main/java/io/vertx/oracleclient/OracleBuilder.java @@ -0,0 +1,54 @@ +/* + * 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.oracleclient; + +import io.vertx.codegen.annotations.VertxGen; +import io.vertx.core.Handler; +import io.vertx.oracleclient.spi.OracleDriver; +import io.vertx.sqlclient.ClientBuilder; +import io.vertx.sqlclient.Pool; +import io.vertx.sqlclient.PoolOptions; +import io.vertx.sqlclient.impl.ClientBuilderBase; + +/** + * Entry point for building Oracle clients. + */ +@VertxGen +public interface OracleBuilder { + + /** + * Build a pool with the specified {@code block} argument. + * The {@code block} argument is usually a lambda that configures the provided builder + *

+ * Example usage: {@code Pool pool = PgBuilder.pool(builder -> builder.connectingTo(connectOptions));} + * + * @return the pool as configured by the code {@code block} + */ + static Pool pool(Handler> block) { + return ClientBuilder.pool(OracleDriver.INSTANCE, block); + } + + /** + * Provide a builder for Oracle pool of connections + *

+ * Example usage: {@code Pool pool = PgBuilder.pool().connectingTo(connectOptions).build()} + */ + static ClientBuilder pool() { + return ClientBuilder.pool(OracleDriver.INSTANCE); + } +} 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 index d323ba643..257436160 100644 --- a/vertx-oracle-client/src/main/java/io/vertx/oracleclient/OraclePool.java +++ b/vertx-oracle-client/src/main/java/io/vertx/oracleclient/OraclePool.java @@ -15,10 +15,12 @@ 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; @@ -38,7 +40,7 @@ static OraclePool pool(OracleConnectOptions connectOptions, PoolOptions poolOpti * 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, Collections.singletonList(connectOptions), poolOptions); + return (OraclePool) OracleDriver.INSTANCE.createPool(vertx, Utils.singletonSupplier(connectOptions), poolOptions, new NetClientOptions()); } /** @@ -72,7 +74,7 @@ static OraclePool pool(Supplier> databases, PoolOpt * 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); + return (OraclePool) OracleDriver.INSTANCE.createPool(vertx, databases, poolOptions, new NetClientOptions()); } @Override 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 dfcda55c2..534c59aa5 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 @@ -16,6 +16,7 @@ import io.vertx.core.impl.ContextInternal; import io.vertx.core.impl.VertxInternal; import io.vertx.core.json.JsonObject; +import io.vertx.core.net.NetClientOptions; import io.vertx.oracleclient.OracleConnectOptions; import io.vertx.oracleclient.impl.*; import io.vertx.sqlclient.Pool; @@ -42,7 +43,7 @@ public OracleConnectOptions downcast(SqlConnectOptions connectOptions) { } @Override - public Pool newPool(Vertx vertx, Supplier> databases, PoolOptions options, CloseFuture closeFuture) { + public Pool newPool(Vertx vertx, Supplier> databases, PoolOptions options, NetClientOptions transportOptions, CloseFuture closeFuture) { VertxInternal vx = (VertxInternal) vertx; PoolImpl pool; if (options.isShared()) { @@ -57,7 +58,7 @@ private PoolImpl newPoolImpl(VertxInternal vertx, Supplier> 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); + ConnectionFactory factory = createConnectionFactory(vertx, null); pool.connectionProvider(context -> factory.connect(context, databases.get())); pool.init(); closeFuture.add(factory); @@ -76,7 +77,7 @@ public boolean acceptsOptions(SqlConnectOptions options) { } @Override - public ConnectionFactory createConnectionFactory(Vertx vertx) { + public ConnectionFactory createConnectionFactory(Vertx vertx, NetClientOptions transportOptions) { return new OracleConnectionFactory((VertxInternal) vertx); } diff --git a/vertx-oracle-client/src/test/java/io/vertx/oracleclient/test/OracleBinaryDataTypesTest.java b/vertx-oracle-client/src/test/java/io/vertx/oracleclient/test/OracleBinaryDataTypesTest.java index 46de7e64a..831c429a5 100644 --- a/vertx-oracle-client/src/test/java/io/vertx/oracleclient/test/OracleBinaryDataTypesTest.java +++ b/vertx-oracle-client/src/test/java/io/vertx/oracleclient/test/OracleBinaryDataTypesTest.java @@ -14,10 +14,10 @@ import io.vertx.core.buffer.Buffer; import io.vertx.ext.unit.TestContext; import io.vertx.ext.unit.junit.VertxUnitRunner; -import io.vertx.oracleclient.OraclePool; +import io.vertx.oracleclient.OracleBuilder; import io.vertx.oracleclient.data.Blob; import io.vertx.oracleclient.test.junit.OracleRule; -import io.vertx.sqlclient.PoolOptions; +import io.vertx.sqlclient.Pool; import io.vertx.sqlclient.Row; import io.vertx.sqlclient.Tuple; import io.vertx.sqlclient.desc.ColumnDescriptor; @@ -36,11 +36,11 @@ public class OracleBinaryDataTypesTest extends OracleTestBase { @ClassRule public static OracleRule oracle = OracleRule.SHARED_INSTANCE; - OraclePool pool; + Pool pool; @Before public void setUp() throws Exception { - pool = OraclePool.pool(vertx, oracle.options(), new PoolOptions()); + pool = OracleBuilder.pool(builder -> builder.connectingTo(oracle.options()).using(vertx)); } @After diff --git a/vertx-oracle-client/src/test/java/io/vertx/oracleclient/test/OracleBrokenPooledConnectionTest.java b/vertx-oracle-client/src/test/java/io/vertx/oracleclient/test/OracleBrokenPooledConnectionTest.java index 53612fdb8..d37c045d6 100644 --- a/vertx-oracle-client/src/test/java/io/vertx/oracleclient/test/OracleBrokenPooledConnectionTest.java +++ b/vertx-oracle-client/src/test/java/io/vertx/oracleclient/test/OracleBrokenPooledConnectionTest.java @@ -14,9 +14,10 @@ import io.vertx.ext.unit.Async; import io.vertx.ext.unit.TestContext; import io.vertx.ext.unit.junit.VertxUnitRunner; +import io.vertx.oracleclient.OracleBuilder; import io.vertx.oracleclient.OracleConnectOptions; -import io.vertx.oracleclient.OraclePool; import io.vertx.oracleclient.test.junit.OracleRule; +import io.vertx.sqlclient.Pool; import io.vertx.sqlclient.PoolOptions; import io.vertx.sqlclient.ProxyServer; import org.junit.After; @@ -33,7 +34,7 @@ public class OracleBrokenPooledConnectionTest extends OracleTestBase { public static OracleRule oracle = OracleRule.SHARED_INSTANCE; OracleConnectOptions options = oracle.options(); - OraclePool pool; + Pool pool; @After public void tearDown(TestContext ctx) throws Exception { @@ -50,7 +51,10 @@ public void testBrokenConnectionEvicted(TestContext ctx) { proxyConn.set(conn); conn.connect(); }); - pool = OraclePool.pool(vertx, new OracleConnectOptions(options).setPort(8080), new PoolOptions().setMaxSize(1)); + pool = OracleBuilder.pool(builder -> builder + .with(new PoolOptions().setMaxSize(1)) + .connectingTo(new OracleConnectOptions(options).setPort(8080)) + .using(vertx)); proxy.listen(8080, options.getHost(), ctx.asyncAssertSuccess(listen -> { pool .query("SELECT 1 FROM DUAL") diff --git a/vertx-oracle-client/src/test/java/io/vertx/oracleclient/test/OracleColumnDescriptorTest.java b/vertx-oracle-client/src/test/java/io/vertx/oracleclient/test/OracleColumnDescriptorTest.java index fd03389df..1aa8c85c3 100644 --- a/vertx-oracle-client/src/test/java/io/vertx/oracleclient/test/OracleColumnDescriptorTest.java +++ b/vertx-oracle-client/src/test/java/io/vertx/oracleclient/test/OracleColumnDescriptorTest.java @@ -13,9 +13,9 @@ import io.vertx.ext.unit.TestContext; import io.vertx.ext.unit.junit.VertxUnitRunner; -import io.vertx.oracleclient.OraclePool; +import io.vertx.oracleclient.OracleBuilder; import io.vertx.oracleclient.test.junit.OracleRule; -import io.vertx.sqlclient.PoolOptions; +import io.vertx.sqlclient.Pool; import org.junit.After; import org.junit.Before; import org.junit.ClassRule; @@ -28,11 +28,13 @@ public class OracleColumnDescriptorTest extends OracleTestBase { @ClassRule public static OracleRule oracle = OracleRule.SHARED_INSTANCE; - OraclePool pool; + Pool pool; @Before public void setUp() throws Exception { - pool = OraclePool.pool(vertx, oracle.options(), new PoolOptions()); + pool = OracleBuilder.pool(builder -> builder + .connectingTo(oracle.options()) + .using(vertx)); } @Test diff --git a/vertx-oracle-client/src/test/java/io/vertx/oracleclient/test/OracleErrorSimpleTest.java b/vertx-oracle-client/src/test/java/io/vertx/oracleclient/test/OracleErrorSimpleTest.java index 2b43becd2..6def5f0bc 100644 --- a/vertx-oracle-client/src/test/java/io/vertx/oracleclient/test/OracleErrorSimpleTest.java +++ b/vertx-oracle-client/src/test/java/io/vertx/oracleclient/test/OracleErrorSimpleTest.java @@ -13,10 +13,10 @@ import io.vertx.ext.unit.TestContext; import io.vertx.ext.unit.junit.VertxUnitRunner; +import io.vertx.oracleclient.OracleBuilder; import io.vertx.oracleclient.OracleException; -import io.vertx.oracleclient.OraclePool; import io.vertx.oracleclient.test.junit.OracleRule; -import io.vertx.sqlclient.PoolOptions; +import io.vertx.sqlclient.Pool; import org.junit.After; import org.junit.Before; import org.junit.ClassRule; @@ -31,11 +31,13 @@ public class OracleErrorSimpleTest extends OracleTestBase { @ClassRule public static OracleRule oracle = OracleRule.SHARED_INSTANCE; - OraclePool pool; + Pool pool; @Before public void setUp() throws Exception { - pool = OraclePool.pool(vertx, oracle.options(), new PoolOptions()); + pool = OracleBuilder.pool(builder -> builder + .connectingTo(oracle.options()) + .using(vertx)); } @Test diff --git a/vertx-oracle-client/src/test/java/io/vertx/oracleclient/test/OracleGeneratedKeysTestBase.java b/vertx-oracle-client/src/test/java/io/vertx/oracleclient/test/OracleGeneratedKeysTestBase.java index 31051884a..a0cf03e80 100644 --- a/vertx-oracle-client/src/test/java/io/vertx/oracleclient/test/OracleGeneratedKeysTestBase.java +++ b/vertx-oracle-client/src/test/java/io/vertx/oracleclient/test/OracleGeneratedKeysTestBase.java @@ -17,8 +17,8 @@ import io.vertx.core.json.JsonArray; import io.vertx.ext.unit.TestContext; import io.vertx.ext.unit.junit.VertxUnitRunner; +import io.vertx.oracleclient.OracleBuilder; import io.vertx.oracleclient.OracleClient; -import io.vertx.oracleclient.OraclePool; import io.vertx.oracleclient.OraclePrepareOptions; import io.vertx.oracleclient.test.junit.OracleRule; import io.vertx.sqlclient.*; @@ -85,11 +85,13 @@ public abstract class OracleGeneratedKeysTestBase extends OracleTestBase { @ClassRule public static OracleRule oracle = OracleRule.SHARED_INSTANCE; - protected OraclePool pool; + protected Pool pool; @Before public void setUp(TestContext ctx) throws Exception { - pool = OraclePool.pool(vertx, oracle.options(), new PoolOptions()); + pool = OracleBuilder.pool(builder -> builder + .connectingTo(oracle.options()) + .using(vertx)); pool .withConnection(conn -> { return conn diff --git a/vertx-oracle-client/src/test/java/io/vertx/oracleclient/test/OraclePoolTest.java b/vertx-oracle-client/src/test/java/io/vertx/oracleclient/test/OraclePoolTest.java index 9698ea807..c09f5096e 100644 --- a/vertx-oracle-client/src/test/java/io/vertx/oracleclient/test/OraclePoolTest.java +++ b/vertx-oracle-client/src/test/java/io/vertx/oracleclient/test/OraclePoolTest.java @@ -17,8 +17,8 @@ import io.vertx.ext.unit.Async; import io.vertx.ext.unit.TestContext; import io.vertx.ext.unit.junit.VertxUnitRunner; +import io.vertx.oracleclient.OracleBuilder; import io.vertx.oracleclient.OracleConnectOptions; -import io.vertx.oracleclient.OraclePool; import io.vertx.oracleclient.test.junit.OracleRule; import io.vertx.sqlclient.*; import org.junit.After; @@ -40,7 +40,7 @@ public class OraclePoolTest extends OracleTestBase { public static OracleRule oracle = OracleRule.SHARED_INSTANCE; private OracleConnectOptions options; - private Set pools; + private Set pools; @Before public void setUp() throws Exception { @@ -52,7 +52,7 @@ public void setUp() throws Exception { public void tearDown(TestContext ctx) throws Exception { if (!pools.isEmpty()) { Async async = ctx.async(pools.size()); - for (OraclePool pool : pools) { + for (Pool pool : pools) { pool .close() .onComplete(ar -> { @@ -63,14 +63,22 @@ public void tearDown(TestContext ctx) throws Exception { } } - private OraclePool createPool(OracleConnectOptions connectOptions, int size) { + private Pool createPool(OracleConnectOptions connectOptions, int size) { return createPool(connectOptions, new PoolOptions().setMaxSize(size)); } - private OraclePool createPool(OracleConnectOptions connectOptions, PoolOptions poolOptions) { + private Pool createPool(OracleConnectOptions connectOptions, PoolOptions poolOptions) { + return createPool(connectOptions, poolOptions, null); + } + + private Pool createPool(OracleConnectOptions connectOptions, PoolOptions poolOptions, Handler connectHandler) { OracleConnectOptions co = new OracleConnectOptions(connectOptions); PoolOptions po = new PoolOptions(poolOptions); - OraclePool pool = OraclePool.pool(vertx, co, po); + Pool pool = OracleBuilder.pool(builder -> builder + .with(po) + .withConnectHandler(connectHandler) + .connectingTo(co) + .using(vertx)); pools.add(pool); return pool; } @@ -79,7 +87,7 @@ private OraclePool createPool(OracleConnectOptions connectOptions, PoolOptions p public void testPool(TestContext ctx) { int num = 1000; Async async = ctx.async(num); - OraclePool pool = createPool(options, 40); + Pool pool = createPool(options, 40); for (int i = 0; i < num; i++) { pool .getConnection() @@ -100,7 +108,7 @@ public void testPool(TestContext ctx) { public void testQuery(TestContext ctx) { int num = 1000; Async async = ctx.async(num); - OraclePool pool = createPool(options, 40); + Pool pool = createPool(options, 40); for (int i = 0; i < num; i++) { pool .query("SELECT id, randomnumber FROM WORLD") @@ -116,7 +124,7 @@ public void testQuery(TestContext ctx) { public void testQueryWithParams(TestContext ctx) { int num = 2; Async async = ctx.async(num); - OraclePool pool = createPool(options, 1); + Pool pool = createPool(options, 1); for (int i = 0; i < num; i++) { PreparedQuery> preparedQuery = pool.preparedQuery("SELECT id, randomnumber FROM WORLD WHERE id=?"); preparedQuery @@ -132,7 +140,7 @@ public void testQueryWithParams(TestContext ctx) { public void testUpdate(TestContext ctx) { int num = 1000; Async async = ctx.async(num); - OraclePool pool = createPool(options, 4); + Pool pool = createPool(options, 4); for (int i = 0; i < num; i++) { pool .query("UPDATE Fortune SET message = 'Whatever' WHERE id = 9") @@ -148,7 +156,7 @@ public void testUpdate(TestContext ctx) { public void testUpdateWithParams(TestContext ctx) { int num = 1000; Async async = ctx.async(num); - OraclePool pool = createPool(options, 4); + Pool pool = createPool(options, 4); for (int i = 0; i < num; i++) { pool .preparedQuery("UPDATE Fortune SET message = 'Whatever' WHERE id = ?") @@ -168,7 +176,7 @@ public void testUpdateWithParams(TestContext ctx) { @Test public void testWithConnection(TestContext ctx) { Async async = ctx.async(10); - OraclePool pool = createPool(options, 1); + Pool pool = createPool(options, 1); Function>> success = conn -> conn.query("SELECT 1 FROM DUAL").execute(); Function>> failure = conn -> conn.query("SELECT 1 FROM does_not_exist").execute(); for (int i = 0; i < 10; i++) { @@ -187,7 +195,7 @@ public void testWithConnection(TestContext ctx) { @Test public void testAuthFailure(TestContext ctx) { Async async = ctx.async(); - OraclePool pool = createPool(new OracleConnectOptions(options).setPassword("wrong"), 1); + Pool pool = createPool(new OracleConnectOptions(options).setPassword("wrong"), 1); pool .query("SELECT id, randomnumber FROM WORLD") .execute() @@ -201,7 +209,7 @@ public void testRunWithExisting(TestContext ctx) { Async async = ctx.async(); vertx.runOnContext(v -> { try { - OraclePool.pool(options, new PoolOptions()); + Pool.pool(options, new PoolOptions()); ctx.fail(); } catch (IllegalStateException ignore) { async.complete(); @@ -212,7 +220,7 @@ public void testRunWithExisting(TestContext ctx) { @Test public void testRunStandalone(TestContext ctx) { Async async = ctx.async(); - OraclePool pool = createPool(new OracleConnectOptions(options), new PoolOptions()); + Pool pool = createPool(new OracleConnectOptions(options), new PoolOptions()); pool .query("SELECT id, randomnumber FROM WORLD") .execute() @@ -225,7 +233,7 @@ public void testRunStandalone(TestContext ctx) { @Test public void testMaxWaitQueueSize(TestContext ctx) { Async async = ctx.async(); - OraclePool pool = createPool(options, new PoolOptions().setMaxSize(1).setMaxWaitQueueSize(0)); + Pool pool = createPool(options, new PoolOptions().setMaxSize(1).setMaxWaitQueueSize(0)); pool .getConnection() .onComplete(ctx.asyncAssertSuccess(v -> { @@ -242,7 +250,7 @@ public void testMaxWaitQueueSize(TestContext ctx) { // will actually use the same connection for the prepare and the query commands @Test public void testConcurrentMultipleConnection(TestContext ctx) { - OraclePool pool = createPool(new OracleConnectOptions(this.options).setCachePreparedStatements(true), 2); + Pool pool = createPool(new OracleConnectOptions(this.options).setCachePreparedStatements(true), 2); int numRequests = 2; Async async = ctx.async(numRequests); for (int i = 0; i < numRequests; i++) { @@ -268,7 +276,7 @@ public void testConnectionHook(TestContext ctx) { f.close(); }); }; - OraclePool pool = createPool(options, new PoolOptions().setMaxSize(1)).connectHandler(hook); + Pool pool = createPool(options, new PoolOptions().setMaxSize(1), hook); pool .getConnection() .onComplete(ctx.asyncAssertSuccess(conn -> { @@ -284,7 +292,7 @@ public void testConnectionHook(TestContext ctx) { @Test public void testDirectQueryFromDuplicatedContext(TestContext ctx) { - OraclePool pool = createPool(options, new PoolOptions().setMaxSize(1)); + Pool pool = createPool(options, new PoolOptions().setMaxSize(1)); Async async = ctx.async(); vertx.runOnContext(v1 -> { ContextInternal current = (ContextInternal) Vertx.currentContext(); @@ -309,7 +317,7 @@ public void testDirectQueryFromDuplicatedContext(TestContext ctx) { @Test public void testQueryFromDuplicatedContext(TestContext ctx) { - OraclePool pool = createPool(options, new PoolOptions().setMaxSize(1)); + Pool pool = createPool(options, new PoolOptions().setMaxSize(1)); Async async = ctx.async(); vertx.runOnContext(v1 -> { ContextInternal current = (ContextInternal) Vertx.currentContext(); diff --git a/vertx-oracle-client/src/test/java/io/vertx/oracleclient/test/OracleQueriesTest.java b/vertx-oracle-client/src/test/java/io/vertx/oracleclient/test/OracleQueriesTest.java index 15458d233..ecf36536c 100644 --- a/vertx-oracle-client/src/test/java/io/vertx/oracleclient/test/OracleQueriesTest.java +++ b/vertx-oracle-client/src/test/java/io/vertx/oracleclient/test/OracleQueriesTest.java @@ -13,9 +13,9 @@ import io.vertx.ext.unit.TestContext; import io.vertx.ext.unit.junit.VertxUnitRunner; -import io.vertx.oracleclient.OraclePool; +import io.vertx.oracleclient.OracleBuilder; import io.vertx.oracleclient.test.junit.OracleRule; -import io.vertx.sqlclient.PoolOptions; +import io.vertx.sqlclient.Pool; import io.vertx.sqlclient.Tuple; import io.vertx.sqlclient.desc.ColumnDescriptor; import org.junit.After; @@ -39,11 +39,13 @@ public class OracleQueriesTest extends OracleTestBase { @ClassRule public static OracleRule oracle = OracleRule.SHARED_INSTANCE; - OraclePool pool; + Pool pool; @Before public void setUp() throws Exception { - pool = OraclePool.pool(vertx, oracle.options(), new PoolOptions()); + pool = OracleBuilder.pool(builder -> builder + .connectingTo(oracle.options()) + .using(vertx)); } @Test diff --git a/vertx-oracle-client/src/test/java/io/vertx/oracleclient/test/OracleTemporalDataTypesTest.java b/vertx-oracle-client/src/test/java/io/vertx/oracleclient/test/OracleTemporalDataTypesTest.java index 7fa71a1c7..7a403ab63 100644 --- a/vertx-oracle-client/src/test/java/io/vertx/oracleclient/test/OracleTemporalDataTypesTest.java +++ b/vertx-oracle-client/src/test/java/io/vertx/oracleclient/test/OracleTemporalDataTypesTest.java @@ -13,9 +13,9 @@ import io.vertx.ext.unit.TestContext; import io.vertx.ext.unit.junit.VertxUnitRunner; -import io.vertx.oracleclient.OraclePool; +import io.vertx.oracleclient.OracleBuilder; import io.vertx.oracleclient.test.junit.OracleRule; -import io.vertx.sqlclient.PoolOptions; +import io.vertx.sqlclient.Pool; import io.vertx.sqlclient.Row; import io.vertx.sqlclient.Tuple; import io.vertx.sqlclient.desc.ColumnDescriptor; @@ -36,11 +36,13 @@ public class OracleTemporalDataTypesTest extends OracleTestBase { @ClassRule public static OracleRule oracle = OracleRule.SHARED_INSTANCE; - OraclePool pool; + Pool pool; @Before public void setUp() throws Exception { - pool = OraclePool.pool(vertx, oracle.options(), new PoolOptions()); + pool = OracleBuilder.pool(builder -> builder + .connectingTo(oracle.options()) + .using(vertx)); } @After diff --git a/vertx-oracle-client/src/test/java/io/vertx/oracleclient/test/tck/ClientConfig.java b/vertx-oracle-client/src/test/java/io/vertx/oracleclient/test/tck/ClientConfig.java index 802be67be..4a4615bb1 100644 --- a/vertx-oracle-client/src/test/java/io/vertx/oracleclient/test/tck/ClientConfig.java +++ b/vertx-oracle-client/src/test/java/io/vertx/oracleclient/test/tck/ClientConfig.java @@ -14,13 +14,10 @@ import io.vertx.core.Future; import io.vertx.core.Handler; import io.vertx.core.Vertx; +import io.vertx.oracleclient.OracleBuilder; import io.vertx.oracleclient.OracleConnectOptions; import io.vertx.oracleclient.OracleConnection; -import io.vertx.oracleclient.OraclePool; -import io.vertx.sqlclient.PoolOptions; -import io.vertx.sqlclient.SqlClient; -import io.vertx.sqlclient.SqlConnectOptions; -import io.vertx.sqlclient.SqlConnection; +import io.vertx.sqlclient.*; import io.vertx.sqlclient.tck.Connector; public enum ClientConfig { @@ -51,8 +48,11 @@ public void close() { POOLED() { @Override Connector connect(Vertx vertx, SqlConnectOptions options) { - OraclePool pool = OraclePool - .pool(vertx, new OracleConnectOptions(options), new PoolOptions().setMaxSize(5)); + Pool pool = OracleBuilder + .pool(builder -> builder + .with(new PoolOptions().setMaxSize(5)) + .connectingTo(new OracleConnectOptions(options)) + .using(vertx)); return new Connector<>() { @Override public void connect(Handler> handler) { diff --git a/vertx-oracle-client/src/test/java/io/vertx/oracleclient/test/tck/OracleMetricsTest.java b/vertx-oracle-client/src/test/java/io/vertx/oracleclient/test/tck/OracleMetricsTest.java index f64e9eb54..702181755 100644 --- a/vertx-oracle-client/src/test/java/io/vertx/oracleclient/test/tck/OracleMetricsTest.java +++ b/vertx-oracle-client/src/test/java/io/vertx/oracleclient/test/tck/OracleMetricsTest.java @@ -13,10 +13,9 @@ import io.vertx.core.Vertx; import io.vertx.ext.unit.TestContext; -import io.vertx.oracleclient.OraclePool; +import io.vertx.oracleclient.OracleBuilder; import io.vertx.oracleclient.test.junit.OracleRule; import io.vertx.sqlclient.Pool; -import io.vertx.sqlclient.PoolOptions; import io.vertx.sqlclient.tck.MetricsTestBase; import org.junit.ClassRule; import org.junit.Ignore; @@ -29,7 +28,9 @@ public class OracleMetricsTest extends MetricsTestBase { @Override protected Pool createPool(Vertx vertx) { - return OraclePool.pool(vertx, rule.options(), new PoolOptions()); + return OracleBuilder.pool(builder -> builder + .connectingTo(rule.options()) + .using(vertx)); } @Override diff --git a/vertx-oracle-client/src/test/java/io/vertx/oracleclient/test/tck/OracleTracingTest.java b/vertx-oracle-client/src/test/java/io/vertx/oracleclient/test/tck/OracleTracingTest.java index d8d085a37..44f585983 100644 --- a/vertx-oracle-client/src/test/java/io/vertx/oracleclient/test/tck/OracleTracingTest.java +++ b/vertx-oracle-client/src/test/java/io/vertx/oracleclient/test/tck/OracleTracingTest.java @@ -14,10 +14,9 @@ import io.vertx.core.Vertx; import io.vertx.ext.unit.TestContext; import io.vertx.ext.unit.junit.VertxUnitRunner; -import io.vertx.oracleclient.OraclePool; +import io.vertx.oracleclient.OracleBuilder; import io.vertx.oracleclient.test.junit.OracleRule; import io.vertx.sqlclient.Pool; -import io.vertx.sqlclient.PoolOptions; import io.vertx.sqlclient.tck.TracingTestBase; import org.junit.ClassRule; import org.junit.Ignore; @@ -32,7 +31,9 @@ public class OracleTracingTest extends TracingTestBase { @Override protected Pool createPool(Vertx vertx) { - return OraclePool.pool(vertx, rule.options(), new PoolOptions()); + return OracleBuilder.pool(builder -> builder + .connectingTo(rule.options()) + .using(vertx)); } @Override diff --git a/vertx-oracle-client/src/test/java/io/vertx/oracleclient/test/tck/OracleTransactionTest.java b/vertx-oracle-client/src/test/java/io/vertx/oracleclient/test/tck/OracleTransactionTest.java index eb3cdfdd1..df74c4eae 100644 --- a/vertx-oracle-client/src/test/java/io/vertx/oracleclient/test/tck/OracleTransactionTest.java +++ b/vertx-oracle-client/src/test/java/io/vertx/oracleclient/test/tck/OracleTransactionTest.java @@ -13,7 +13,7 @@ import io.vertx.ext.unit.Async; import io.vertx.ext.unit.TestContext; import io.vertx.ext.unit.junit.VertxUnitRunner; -import io.vertx.oracleclient.OraclePool; +import io.vertx.oracleclient.OracleBuilder; import io.vertx.oracleclient.test.junit.OracleRule; import io.vertx.sqlclient.Pool; import io.vertx.sqlclient.PoolOptions; @@ -31,12 +31,18 @@ public class OracleTransactionTest extends TransactionTestBase { @Override protected Pool createPool() { - return OraclePool.pool(vertx, rule.options(), new PoolOptions().setMaxSize(1)); + return OracleBuilder.pool(builder -> builder + .with(new PoolOptions().setMaxSize(1)) + .connectingTo(rule.options()) + .using(vertx)); } @Override protected Pool nonTxPool() { - return OraclePool.pool(vertx, rule.options(), new PoolOptions().setMaxSize(1)); + return OracleBuilder.pool(builder -> builder + .with(new PoolOptions().setMaxSize(1)) + .connectingTo(rule.options()) + .using(vertx)); } @Override diff --git a/vertx-pg-client/src/main/asciidoc/index.adoc b/vertx-pg-client/src/main/asciidoc/index.adoc index f95e7dbf7..dbdcc4d7e 100644 --- a/vertx-pg-client/src/main/asciidoc/index.adoc +++ b/vertx-pg-client/src/main/asciidoc/index.adoc @@ -109,7 +109,7 @@ You can set this value to `1` to disable pipelining. == Pool versus pooled client -The {@link io.vertx.pgclient.PgPool} allows you to create a pool or a pooled client +The {@link io.vertx.pgclient.PgBuilder} allows you to create a pool or a pooled client [source,$lang] ---- diff --git a/vertx-pg-client/src/main/java/examples/PgClientExamples.java b/vertx-pg-client/src/main/java/examples/PgClientExamples.java index 9745dc550..c086f6daa 100644 --- a/vertx-pg-client/src/main/java/examples/PgClientExamples.java +++ b/vertx-pg-client/src/main/java/examples/PgClientExamples.java @@ -23,10 +23,7 @@ import io.vertx.core.net.ClientSSLOptions; import io.vertx.core.net.PemTrustOptions; import io.vertx.docgen.Source; -import io.vertx.pgclient.PgConnectOptions; -import io.vertx.pgclient.PgConnection; -import io.vertx.pgclient.PgPool; -import io.vertx.pgclient.SslMode; +import io.vertx.pgclient.*; import io.vertx.pgclient.pubsub.PgSubscriber; import io.vertx.sqlclient.*; import io.vertx.sqlclient.data.Numeric; @@ -60,7 +57,11 @@ public void gettingStarted() { .setMaxSize(5); // Create the client pool - SqlClient client = PgPool.client(connectOptions, poolOptions); + SqlClient client = PgBuilder + .client() + .with(poolOptions) + .connectingTo(connectOptions) + .build(); // A simple query client @@ -82,7 +83,9 @@ public void gettingStarted() { public void configureFromEnv(Vertx vertx) { // Create the pool from the environment variables - PgPool pool = PgPool.pool(); + Pool pool = PgBuilder.pool() + .using(vertx) + .build(); // Create the connection from the environment variables PgConnection.connect(vertx) @@ -105,7 +108,11 @@ public void configureFromDataObject(Vertx vertx) { PoolOptions poolOptions = new PoolOptions().setMaxSize(5); // Create the pool from the data object - PgPool pool = PgPool.pool(vertx, connectOptions, poolOptions); + Pool pool = PgBuilder.pool() + .with(poolOptions) + .connectingTo(connectOptions) + .using(vertx) + .build(); pool.getConnection() .onComplete(ar -> { @@ -129,7 +136,10 @@ public void configureFromUri(Vertx vertx) { String connectionUri = "postgresql://dbuser:secretpassword@database.server.com:5432/mydb"; // Create the pool from the connection URI - PgPool pool = PgPool.pool(connectionUri); + Pool pool = PgBuilder.pool() + .connectingTo(connectionUri) + .using(vertx) + .build(); // Create the connection from the connection URI PgConnection @@ -154,7 +164,11 @@ public void connecting01() { .setMaxSize(5); // Create the pooled client - SqlClient client = PgPool.client(connectOptions, poolOptions); + SqlClient client = PgBuilder + .client() + .with(poolOptions) + .connectingTo(connectOptions) + .build(); } public void connecting02(Vertx vertx) { @@ -172,10 +186,15 @@ public void connecting02(Vertx vertx) { .setMaxSize(5); // Create the pooled client - SqlClient client = PgPool.client(vertx, connectOptions, poolOptions); + SqlClient client = PgBuilder + .client() + .with(poolOptions) + .connectingTo(connectOptions) + .using(vertx) + .build(); } - public void connecting03(PgPool client) { + public void connecting03(Pool client) { // Close the pooled client and all the associated resources client.close(); @@ -196,7 +215,12 @@ public void connecting04(Vertx vertx) { .setMaxSize(5); // Create the pooled client - PgPool pool = PgPool.pool(vertx, connectOptions, poolOptions); + Pool pool = PgBuilder + .pool() + .with(poolOptions) + .connectingTo(connectOptions) + .using(vertx) + .build(); // Get a connection from the pool pool.getConnection().compose(conn -> { @@ -215,7 +239,6 @@ public void connecting04(Vertx vertx) { }); }).onComplete(ar -> { if (ar.succeeded()) { - System.out.println("Done"); } else { System.out.println("Something went wrong " + ar.cause().getMessage()); @@ -260,19 +283,34 @@ public void connecting05(Vertx vertx) { } public void clientPipelining(Vertx vertx, PgConnectOptions connectOptions, PoolOptions poolOptions) { - PgPool pool = PgPool.pool(vertx, connectOptions.setPipeliningLimit(16), poolOptions); + Pool pool = PgBuilder + .pool() + .connectingTo(connectOptions.setPipeliningLimit(16)) + .with(poolOptions) + .using(vertx) + .build(); } public void poolVersusPooledClient(Vertx vertx, String sql, PgConnectOptions connectOptions, PoolOptions poolOptions) { // Pooled client - SqlClient client = PgPool.client(vertx, connectOptions, poolOptions); + SqlClient client = PgBuilder + .client() + .with(poolOptions) + .connectingTo(connectOptions) + .using(vertx) + .build(); // Pipelined Future> res1 = client.query(sql).execute(); // Connection pool - PgPool pool = PgPool.pool(vertx, connectOptions, poolOptions); + Pool pool = PgBuilder + .pool() + .connectingTo(connectOptions) + .with(poolOptions) + .using(vertx) + .build(); // Not pipelined Future> res2 = pool.query(sql).execute(); @@ -292,11 +330,20 @@ public void unixDomainSockets(Vertx vertx) { .setMaxSize(5); // Create the pooled client - PgPool client = PgPool.pool(connectOptions, poolOptions); + Pool client = PgBuilder + .pool() + .connectingTo(connectOptions) + .with(poolOptions) + .build(); // Create the pooled client with a vertx instance // Make sure the vertx instance has enabled native transports - PgPool client2 = PgPool.pool(vertx, connectOptions, poolOptions); + Pool client2 = PgBuilder + .pool() + .connectingTo(connectOptions) + .with(poolOptions) + .using(vertx) + .build(); } public void reconnectAttempts(PgConnectOptions options) { diff --git a/vertx-pg-client/src/main/java/examples/SqlClientExamples.java b/vertx-pg-client/src/main/java/examples/SqlClientExamples.java index 1d3b1ae12..605181200 100644 --- a/vertx-pg-client/src/main/java/examples/SqlClientExamples.java +++ b/vertx-pg-client/src/main/java/examples/SqlClientExamples.java @@ -22,11 +22,9 @@ import io.vertx.core.Vertx; import io.vertx.core.tracing.TracingPolicy; import io.vertx.docgen.Source; +import io.vertx.pgclient.PgBuilder; import io.vertx.pgclient.PgConnectOptions; -import io.vertx.pgclient.PgPool; -import io.vertx.pgclient.spi.PgDriver; import io.vertx.sqlclient.*; -import io.vertx.sqlclient.spi.ConnectionFactory; import java.util.ArrayList; import java.util.Arrays; @@ -364,12 +362,16 @@ public void tracing01(PgConnectOptions options) { options.setTracingPolicy(TracingPolicy.ALWAYS); } - public void poolConfig01(PgConnectOptions server1, PgConnectOptions server2, PgConnectOptions server3, PoolOptions options) { - PgPool pool = PgPool.pool(Arrays.asList(server1, server2, server3), options); + public void poolConfig01(Vertx vertx, PgConnectOptions server1, PgConnectOptions server2, PgConnectOptions server3, PoolOptions options) { + Pool pool = PgBuilder.pool() + .with(options) + .connectingTo(Arrays.asList(server1, server2, server3)) + .using(vertx) + .build(); } - public void poolConfig02(PgPool pool, String sql) { - pool.connectHandler(conn -> { + public void poolConfig02(ClientBuilder builder, String sql) { + builder.withConnectHandler(conn -> { conn.query(sql).execute().onSuccess(res -> { // Release the connection to the pool, ready to be used by the application conn.close(); @@ -378,7 +380,7 @@ public void poolConfig02(PgPool pool, String sql) { } public void poolSharing1(Vertx vertx, PgConnectOptions database, int maxSize) { - PgPool pool = PgPool.pool(database, new PoolOptions().setMaxSize(maxSize)); + Pool pool = Pool.pool(database, new PoolOptions().setMaxSize(maxSize)); vertx.deployVerticle(() -> new AbstractVerticle() { @Override public void start() throws Exception { @@ -389,36 +391,48 @@ public void start() throws Exception { public void poolSharing2(Vertx vertx, PgConnectOptions database, int maxSize) { vertx.deployVerticle(() -> new AbstractVerticle() { - PgPool pool; + Pool pool; @Override public void start() { // Get or create a shared pool // this actually creates a lease to the pool // when the verticle is undeployed, the lease will be released automaticaly - pool = PgPool.pool(database, new PoolOptions() - .setMaxSize(maxSize) - .setShared(true) - .setName("my-pool")); + pool = PgBuilder.pool() + .with(new PoolOptions() + .setMaxSize(maxSize) + .setShared(true) + .setName("my-pool")) + .connectingTo(database) + .using(vertx) + .build(); } }, new DeploymentOptions().setInstances(4)); } public static void poolSharing3(Vertx vertx, PgConnectOptions database, int maxSize) { - PgPool pool = PgPool.pool(database, new PoolOptions() - .setMaxSize(maxSize) - .setShared(true) - .setName("my-pool") - .setEventLoopSize(4)); + Pool pool = PgBuilder.pool() + .with(new PoolOptions() + .setMaxSize(maxSize) + .setShared(true) + .setName("my-pool") + .setEventLoopSize(4)) + .connectingTo(database) + .using(vertx) + .build(); } public void dynamicPoolConfig(Vertx vertx, PoolOptions poolOptions) { - PgPool pool = PgPool.pool(vertx, () -> { - Future connectOptions = retrieveOptions(); - return connectOptions; - }, poolOptions); + Pool pool = PgBuilder.pool() + .with(poolOptions) + .connectingTo(() -> { + Future connectOptions = retrieveOptions(); + return connectOptions; + }) + .using(vertx) + .build(); } - private Future retrieveOptions() { + private Future retrieveOptions() { return null; } } 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 new file mode 100644 index 000000000..93ffeaafd --- /dev/null +++ b/vertx-pg-client/src/main/java/io/vertx/pgclient/PgBuilder.java @@ -0,0 +1,93 @@ +/* + * 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.VertxGen; +import io.vertx.core.Future; +import io.vertx.core.Handler; +import io.vertx.core.Vertx; +import io.vertx.core.net.NetClientOptions; +import io.vertx.pgclient.impl.PgPoolOptions; +import io.vertx.pgclient.spi.PgDriver; +import io.vertx.sqlclient.*; +import io.vertx.sqlclient.impl.ClientBuilderBase; + +import java.util.function.Supplier; + +/** + * Entry point for building PostgreSQL clients. + */ +@VertxGen +public interface PgBuilder { + + /** + * Build a pool with the specified {@code block} argument. + * The {@code block} argument is usually a lambda that configures the provided builder + *

+ * Example usage: {@code Pool pool = PgBuilder.pool(builder -> builder.connectingTo(connectOptions));} + * + * @return the pool as configured by the code {@code block} + */ + static Pool pool(Handler> block) { + return ClientBuilder.pool(PgDriver.INSTANCE, block); + } + + /** + * Provide a builder for PostgreSQL pool of connections + *

+ * Example usage: {@code Pool pool = PgBuilder.pool().connectingTo(connectOptions).build()} + */ + static ClientBuilder pool() { + return ClientBuilder.pool(PgDriver.INSTANCE); + } + + /** + * Build a client backed by a connection pool with the specified {@code block} argument. + * The {@code block} argument is usually a lambda that configures the provided builder + *

+ * Example usage: {@code SqlClient client = PgBuilder.client(builder -> builder.connectingTo(connectOptions));} + * + * @return the client as configured by the code {@code block} + */ + static SqlClient client(Handler> handler) { + ClientBuilder builder = client(); + handler.handle(builder); + return builder.build(); + } + + /** + * Provide a builder for PostgreSQL client backed by a connection pool. + *

+ * Example usage: {@code SqlClient client = PgBuilder.client().connectingTo(connectOptions).build()} + */ + static ClientBuilder client() { + return new ClientBuilderBase(PgDriver.INSTANCE) { + @Override + public ClientBuilder with(PoolOptions options) { + if (options != null) { + options = new PgPoolOptions(options).setPipelined(true); + } + return super.with(options); + } + @Override + protected SqlClient create(Vertx vertx, Supplier> databases, PoolOptions poolOptions, NetClientOptions transportOptions) { + return driver.createPool(vertx, databases, poolOptions, transportOptions); + } + }; + } +} 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 index 2ab73a35a..77853ae91 100644 --- a/vertx-pg-client/src/main/java/io/vertx/pgclient/PgPool.java +++ b/vertx-pg-client/src/main/java/io/vertx/pgclient/PgPool.java @@ -22,73 +22,71 @@ import io.vertx.core.Future; import io.vertx.core.Handler; import io.vertx.pgclient.impl.PgPoolOptions; -import io.vertx.pgclient.spi.PgDriver; -import io.vertx.sqlclient.PoolOptions; -import io.vertx.sqlclient.Pool; +import io.vertx.sqlclient.*; import io.vertx.codegen.annotations.VertxGen; import io.vertx.core.Vertx; -import io.vertx.sqlclient.SqlClient; -import io.vertx.sqlclient.SqlConnection; 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 PgPool pool() { - return pool(PgConnectOptions.fromEnv(), new PoolOptions()); + static Pool pool() { + return pool(new PoolOptions()); } /** * Like {@link #pool(PgConnectOptions, PoolOptions)} with {@code connectOptions} build from the environment variables. */ - static PgPool pool(PoolOptions options) { - return pool(PgConnectOptions.fromEnv(), options); + 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 PgPool pool(String connectionUri) { + static Pool pool(String connectionUri) { return pool(connectionUri, new PoolOptions()); } /** * Like {@link #pool(PgConnectOptions, PoolOptions)} with {@code connectOptions} build from {@code connectionUri}. */ - static PgPool pool(String connectionUri, PoolOptions options) { + static Pool pool(String connectionUri, PoolOptions options) { return pool(PgConnectOptions.fromUri(connectionUri), options); } /** * Like {@link #pool(Vertx, String,PoolOptions)} with default options. */ - static PgPool pool(Vertx vertx, String connectionUri) { + 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 PgPool pool(Vertx vertx, PoolOptions options) { + 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 PgPool pool(Vertx vertx, String connectionUri, PoolOptions poolOptions) { + static Pool pool(Vertx vertx, String connectionUri, PoolOptions poolOptions) { return pool(vertx, PgConnectOptions.fromUri(connectionUri), poolOptions); } @@ -99,14 +97,14 @@ static PgPool pool(Vertx vertx, String connectionUri, PoolOptions poolOptions) { * @param options the options for creating the pool * @return the connection pool */ - static PgPool pool(PgConnectOptions database, PoolOptions options) { + static Pool pool(PgConnectOptions database, PoolOptions options) { return pool(null, database, options); } /** * Like {@link #pool(PgConnectOptions, PoolOptions)} with a specific {@link Vertx} instance. */ - static PgPool pool(Vertx vertx, PgConnectOptions database, PoolOptions options) { + static Pool pool(Vertx vertx, PgConnectOptions database, PoolOptions options) { return pool(vertx, SingletonSupplier.wrap(database), options); } @@ -118,15 +116,20 @@ static PgPool pool(Vertx vertx, PgConnectOptions database, PoolOptions options) * @param poolOptions the options for creating the pool * @return the connection pool */ - static PgPool pool(List databases, PoolOptions poolOptions) { + static Pool pool(List databases, PoolOptions poolOptions) { return pool(null, databases, poolOptions); } /** * Like {@link #pool(List, PoolOptions)} with a specific {@link Vertx} instance. */ - static PgPool pool(Vertx vertx, List databases, PoolOptions poolOptions) { - return (PgPool) PgDriver.INSTANCE.createPool(vertx, databases, poolOptions); + 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(); } /** @@ -137,15 +140,15 @@ static PgPool pool(Vertx vertx, List databases, PoolOptions po * @param poolOptions the options for creating the pool * @return the connection pool */ - static PgPool pool(Supplier> databases, PoolOptions poolOptions) { + static Pool pool(Supplier> databases, PoolOptions poolOptions) { return pool(null, databases, poolOptions); } /** * Like {@link #pool(Supplier, PoolOptions)} with a specific {@link Vertx} instance. */ - static PgPool pool(Vertx vertx, Supplier> databases, PoolOptions poolOptions) { - return (PgPool) PgDriver.INSTANCE.createPool(vertx, databases, poolOptions); + static Pool pool(Vertx vertx, Supplier> databases, PoolOptions poolOptions) { + return PgBuilder.pool().connectingTo(() -> databases.get().map(c -> c)).with(poolOptions).using(vertx).build(); } /** @@ -218,7 +221,10 @@ static SqlClient client(Vertx vertx, PgConnectOptions database, PoolOptions opti * Like {@link #client(List, PoolOptions)} with a specific {@link Vertx} instance. */ static SqlClient client(Vertx vertx, List databases, PoolOptions options) { - return PgDriver.INSTANCE.createPool(vertx, databases, new PgPoolOptions(options).setPipelined(true)); + return PgBuilder.pool(b -> b + .connectingTo(databases.stream().map(SqlConnectOptions.class::cast).collect(Collectors.toList())) + .with(new PgPoolOptions(options).setPipelined(true)) + .using(vertx)); } /** @@ -237,7 +243,7 @@ static SqlClient client(List databases, PoolOptions options) { * Like {@link #client(Supplier, PoolOptions)} with a specific {@link Vertx} instance. */ static SqlClient client(Vertx vertx, Supplier> databases, PoolOptions options) { - return PgDriver.INSTANCE.createPool(vertx, databases, new PgPoolOptions(options).setPipelined(true)); + return PgBuilder.pool().connectingTo(() -> databases.get().map(c -> c)).with(new PgPoolOptions(options).setPipelined(true)).using(vertx).build(); } /** diff --git a/vertx-pg-client/src/main/java/io/vertx/pgclient/impl/PgPoolOptions.java b/vertx-pg-client/src/main/java/io/vertx/pgclient/impl/PgPoolOptions.java index c2a99e235..f493891c1 100644 --- a/vertx-pg-client/src/main/java/io/vertx/pgclient/impl/PgPoolOptions.java +++ b/vertx-pg-client/src/main/java/io/vertx/pgclient/impl/PgPoolOptions.java @@ -24,6 +24,9 @@ public PgPoolOptions(PoolOptions other) { super(other); } + public PgPoolOptions() { + } + private boolean pipelined; public boolean isPipelined() { 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 92fd269ab..04aef4b1b 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 @@ -6,6 +6,7 @@ import io.vertx.core.impl.ContextInternal; import io.vertx.core.impl.VertxInternal; import io.vertx.core.json.JsonObject; +import io.vertx.core.net.NetClientOptions; import io.vertx.pgclient.PgConnectOptions; import io.vertx.pgclient.impl.*; import io.vertx.sqlclient.Pool; @@ -26,21 +27,21 @@ public class PgDriver implements Driver { public static final PgDriver INSTANCE = new PgDriver(); @Override - public Pool newPool(Vertx vertx, Supplier> databases, PoolOptions options, CloseFuture closeFuture) { + public Pool newPool(Vertx vertx, Supplier> databases, PoolOptions poolOptions, NetClientOptions transportOptions, 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)); + if (poolOptions.isShared()) { + pool = vx.createSharedResource(SHARED_CLIENT_KEY, poolOptions.getName(), closeFuture, cf -> newPoolImpl(vx, databases, poolOptions, transportOptions, cf)); } else { - pool = newPoolImpl(vx, databases, options, closeFuture); + pool = newPoolImpl(vx, databases, poolOptions, transportOptions, closeFuture); } return new PgPoolImpl(vx, closeFuture, pool); } - private PoolImpl newPoolImpl(VertxInternal vertx, Supplier> databases, PoolOptions options, CloseFuture closeFuture) { - boolean pipelinedPool = options instanceof PgPoolOptions && ((PgPoolOptions) options).isPipelined(); - PoolImpl pool = new PoolImpl(vertx, this, pipelinedPool, options, null, null, closeFuture); - ConnectionFactory factory = createConnectionFactory(vertx); + private PoolImpl newPoolImpl(VertxInternal vertx, 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())); pool.init(); closeFuture.add(factory); @@ -64,7 +65,7 @@ public PgConnectOptions downcast(SqlConnectOptions connectOptions) { } @Override - public ConnectionFactory createConnectionFactory(Vertx vertx) { + public ConnectionFactory createConnectionFactory(Vertx vertx, NetClientOptions transportOptions) { return new PgConnectionFactory((VertxInternal) vertx); } 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 b41d0681b..304ec59b4 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 @@ -6,9 +6,11 @@ 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; @@ -46,7 +48,7 @@ public void testCloseConnection(TestContext ctx) { @Test public void testClosePooledConnection(TestContext ctx) { testCloseConnection(ctx, () -> { - PgPool pool = PgPool.pool(vertx, options, new PoolOptions().setMaxSize(1)); + Pool pool = PgBuilder.pool().connectingTo(options).with(new PoolOptions().setMaxSize(1)).using(vertx).build(); pool.getConnection().onComplete(ctx.asyncAssertSuccess(conn -> { conn.close().onComplete(ctx.asyncAssertSuccess(v -> { pool.close().onComplete(ctx.asyncAssertSuccess()); @@ -58,8 +60,8 @@ public void testClosePooledConnection(TestContext ctx) { @Test public void testCloseNetSocket(TestContext ctx) { testCloseConnection(ctx, () -> { - PgPool pool = PgPool.pool(vertx, options, new PoolOptions().setMaxSize(1)); - ConnectionFactory factory = PgDriver.INSTANCE.createConnectionFactory(vertx); + 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) { diff --git a/vertx-pg-client/src/test/java/io/vertx/pgclient/PgMetricsTest.java b/vertx-pg-client/src/test/java/io/vertx/pgclient/PgMetricsTest.java index d6b19c12f..644025b13 100644 --- a/vertx-pg-client/src/test/java/io/vertx/pgclient/PgMetricsTest.java +++ b/vertx-pg-client/src/test/java/io/vertx/pgclient/PgMetricsTest.java @@ -12,15 +12,10 @@ package io.vertx.pgclient; import io.vertx.core.Vertx; -import io.vertx.ext.unit.junit.VertxUnitRunner; import io.vertx.pgclient.junit.ContainerPgRule; import io.vertx.sqlclient.Pool; -import io.vertx.sqlclient.PoolOptions; import io.vertx.sqlclient.tck.MetricsTestBase; -import io.vertx.sqlclient.tck.TracingTestBase; import org.junit.ClassRule; -import org.junit.Test; -import org.junit.runner.RunWith; public class PgMetricsTest extends MetricsTestBase { @@ -29,7 +24,7 @@ public class PgMetricsTest extends MetricsTestBase { @Override protected Pool createPool(Vertx vertx) { - return PgPool.pool(vertx, rule.options(), new PoolOptions()); + return PgBuilder.pool().connectingTo(rule.options()).using(vertx).build(); } @Override 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 2c09f892b..50962dab5 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 @@ -22,6 +22,7 @@ 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; @@ -56,14 +57,14 @@ public class PgPoolTest extends PgPoolTestBase { @Rule public RepeatRule rule = new RepeatRule(); - private Set pools = new HashSet<>(); + private Set pools = new HashSet<>(); @Override public void tearDown(TestContext ctx) { int size = pools.size(); if (size > 0) { Async async = ctx.async(size); - Set pools = this.pools; + Set pools = this.pools; this.pools = new HashSet<>(); pools.forEach(pool -> { pool @@ -78,8 +79,13 @@ public void tearDown(TestContext ctx) { } @Override - protected PgPool createPool(PgConnectOptions connectOptions, PoolOptions poolOptions) { - PgPool pool = PgPool.pool(vertx, connectOptions, poolOptions); + protected Pool createPool(PgConnectOptions connectOptions, PoolOptions poolOptions, Handler connectHandler) { + Pool pool = PgBuilder.pool(b -> b + .connectingTo(connectOptions) + .with(poolOptions) + .using(vertx) + .withConnectHandler(connectHandler) + ); pools.add(pool); return pool; } @@ -87,7 +93,7 @@ protected PgPool createPool(PgConnectOptions connectOptions, PoolOptions poolOpt @Test public void testClosePool(TestContext ctx) { Async async = ctx.async(); - PgPool pool = createPool(options, new PoolOptions().setMaxSize(1).setMaxWaitQueueSize(0)); + Pool pool = createPool(options, new PoolOptions().setMaxSize(1).setMaxWaitQueueSize(0)); pool .getConnection() .onComplete(ctx.asyncAssertSuccess(conn -> { @@ -112,7 +118,7 @@ public void testReconnectQueued(TestContext ctx) { conn.connect(); }); proxy.listen(8080, "localhost", ctx.asyncAssertSuccess(v1 -> { - PgPool pool = createPool(new PgConnectOptions(options).setPort(8080).setHost("localhost"), 1); + Pool pool = createPool(new PgConnectOptions(options).setPort(8080).setHost("localhost"), 1); pool .getConnection() .onComplete(ctx.asyncAssertSuccess(conn -> { @@ -134,7 +140,7 @@ public void testReconnectQueued(TestContext ctx) { @Test public void testAuthFailure(TestContext ctx) { Async async = ctx.async(); - PgPool pool = createPool(new PgConnectOptions(options).setPassword("wrong"), 1); + Pool pool = createPool(new PgConnectOptions(options).setPassword("wrong"), 1); pool .query("SELECT id, randomnumber from WORLD") .execute() @@ -152,7 +158,7 @@ public void testConnectionFailure(TestContext ctx) { proxyConn.set(conn); conn.connect(); }); - PgPool pool = createPool(new PgConnectOptions(options).setPort(8080).setHost("localhost"), + Pool pool = createPool(new PgConnectOptions(options).setPort(8080).setHost("localhost"), new PoolOptions() .setMaxSize(1) .setMaxWaitQueueSize(0) @@ -175,7 +181,7 @@ public void testRunWithExisting(TestContext ctx) { Async async = ctx.async(); vertx.runOnContext(v -> { try { - PgPool.pool(new PoolOptions()); + PgBuilder.pool(b -> b.with(new PoolOptions())); ctx.fail(); } catch (IllegalStateException ignore) { async.complete(); @@ -186,7 +192,7 @@ public void testRunWithExisting(TestContext ctx) { @Test public void testRunStandalone(TestContext ctx) { Async async = ctx.async(); - PgPool pool = createPool(new PgConnectOptions(options), new PoolOptions()); + Pool pool = createPool(new PgConnectOptions(options), new PoolOptions()); pool .query("SELECT id, randomnumber from WORLD") .execute() @@ -199,7 +205,7 @@ public void testRunStandalone(TestContext ctx) { @Test public void testMaxWaitQueueSize(TestContext ctx) { Async async = ctx.async(); - PgPool pool = createPool(options, new PoolOptions().setMaxSize(1).setMaxWaitQueueSize(0)); + Pool pool = createPool(options, new PoolOptions().setMaxSize(1).setMaxWaitQueueSize(0)); pool .getConnection() .onComplete(ctx.asyncAssertSuccess(v -> { @@ -218,7 +224,7 @@ public void testMaxWaitQueueSize(TestContext ctx) { // will actually use the same connection for the prepare and the query commands @Test public void testConcurrentMultipleConnection(TestContext ctx) { - PgPool pool = createPool(new PgConnectOptions(this.options).setCachePreparedStatements(true), 2); + Pool pool = createPool(new PgConnectOptions(this.options).setCachePreparedStatements(true), 2); int numRequests = 2; Async async = ctx.async(numRequests); for (int i = 0; i < numRequests; i++) { @@ -239,7 +245,7 @@ public void testConcurrentMultipleConnection(TestContext ctx) { public void testUseAvailableResources(TestContext ctx) { int poolSize = 10; Async async = ctx.async(poolSize + 1); - PgPool pool = PgPool.pool(options, new PoolOptions().setMaxSize(poolSize)); + Pool pool = PgBuilder.pool(b -> b.connectingTo(options).with(new PoolOptions().setMaxSize(poolSize))); AtomicReference ctrlConnRef = new AtomicReference<>(); PgConnection.connect(vertx, options).onComplete(ctx.asyncAssertSuccess(ctrlConn -> { ctrlConnRef.set(ctrlConn); @@ -277,7 +283,7 @@ public void testUseAvailableResources(TestContext ctx) { public void testEventLoopSize(TestContext ctx) { int num = VertxOptions.DEFAULT_EVENT_LOOP_POOL_SIZE; int size = num * 2; - PgPool pool = PgPool.pool(options, new PoolOptions().setMaxSize(size).setEventLoopSize(2)); + Pool pool = PgBuilder.pool(b -> b.with(new PoolOptions().setMaxSize(size).setEventLoopSize(2)).connectingTo(options)); Set eventLoops = Collections.synchronizedSet(new HashSet<>()); Async async = ctx.async(size); for (int i = 0;i < size;i++) { @@ -323,7 +329,7 @@ public void testPipelining(TestContext ctx) { int num = 3; Async async = ctx.async(num); - SqlClient pool = PgPool.client(options, new PoolOptions().setMaxSize(1)); + SqlClient pool = PgBuilder.client(b -> b.connectingTo(options).with(new PoolOptions().setMaxSize(1))); AtomicLong start = new AtomicLong(); // Connect to the database pool @@ -348,7 +354,7 @@ public void testPipelining(TestContext ctx) { @Test public void testCannotAcquireConnectionOnPipelinedPool(TestContext ctx) { - PgPool pool = (PgPool) PgPool.client(options, new PoolOptions().setMaxSize(1)); + Pool pool = (Pool) PgBuilder.client(b -> b.connectingTo(options).with(new PoolOptions().setMaxSize(1))); pool .getConnection() .onComplete(ctx.asyncAssertFailure()); @@ -414,7 +420,7 @@ public void testPoolIdleTimeout(TestContext ctx) { .setIdleTimeoutUnit(TimeUnit.MILLISECONDS); options.setPort(8080); options.setHost("localhost"); - PgPool pool = createPool(options, poolOptions); + Pool pool = createPool(options, poolOptions); // Create a connection that remains in the pool pool @@ -457,7 +463,7 @@ public void testPoolMaxLifetime(TestContext ctx) { .setMaxLifetimeUnit(TimeUnit.MILLISECONDS); options.setPort(8080); options.setHost("localhost"); - PgPool pool = createPool(options, poolOptions); + Pool pool = createPool(options, poolOptions); // Create a connection that remains in the pool pool @@ -487,7 +493,7 @@ public void testPoolConnectTimeout(TestContext ctx) { .setConnectionTimeoutUnit(TimeUnit.SECONDS); options.setPort(8080); options.setHost("localhost"); - PgPool pool = createPool(options, poolOptions); + Pool pool = createPool(options, poolOptions); // Create a connection that remains in the pool long now = System.currentTimeMillis(); @@ -523,7 +529,7 @@ public void testNoConnectionLeaks(TestContext ctx) { .setIdleTimeout(idleTimeout) .setIdleTimeoutUnit(TimeUnit.MILLISECONDS) .setPoolCleanerPeriod(5); - PgPool pool = createPool(options, poolOptions); + Pool pool = createPool(options, poolOptions); Async async = ctx.async(); AtomicInteger pid = new AtomicInteger(); @@ -559,7 +565,7 @@ public void testConnectionHook1(TestContext ctx) { f.close().onComplete(ctx.asyncAssertSuccess(v -> async.countDown())); }); }; - PgPool pool = createPool(options, new PoolOptions().setMaxSize(1)).connectHandler(hook); + Pool pool = createPool(options, new PoolOptions().setMaxSize(1), hook); pool .getConnection() .onComplete(ctx.asyncAssertSuccess(conn -> { @@ -580,7 +586,7 @@ public void testConnectionHook2(TestContext ctx) { f.close().onComplete(ctx.asyncAssertSuccess(v -> async.countDown())); }); }; - PgPool pool = createPool(options, new PoolOptions().setMaxSize(1)).connectHandler(hook); + Pool pool = createPool(options, new PoolOptions().setMaxSize(1), hook); pool .query("SELECT id, randomnumber from WORLD") .execute() @@ -605,7 +611,7 @@ public void testConnectionClosedInHook(TestContext ctx) { }); proxyConn.get().close(); }; - PgPool pool = createPool(new PgConnectOptions(options).setPort(8080).setHost("localhost"), new PoolOptions().setMaxSize(1)).connectHandler(hook); + Pool pool = createPool(new PgConnectOptions(options).setPort(8080).setHost("localhost"), new PoolOptions().setMaxSize(1), hook); pool .getConnection() .onComplete(ctx.asyncAssertFailure(conn -> { @@ -634,8 +640,8 @@ private void testConnectionClosedInProvider(TestContext ctx, boolean immediately }); proxy.listen(8080, "localhost", ctx.asyncAssertSuccess(v1 -> { PgConnectOptions options = new PgConnectOptions(this.options).setPort(8080).setHost("localhost"); - ConnectionFactory factory = PgDriver.INSTANCE.createConnectionFactory(vertx); - PgPool pool = createPool(options, new PoolOptions().setMaxSize(1)); + 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) { diff --git a/vertx-pg-client/src/test/java/io/vertx/pgclient/PgPoolTestBase.java b/vertx-pg-client/src/test/java/io/vertx/pgclient/PgPoolTestBase.java index 0391e6f7e..1b97559ca 100644 --- a/vertx-pg-client/src/test/java/io/vertx/pgclient/PgPoolTestBase.java +++ b/vertx-pg-client/src/test/java/io/vertx/pgclient/PgPoolTestBase.java @@ -18,6 +18,7 @@ package io.vertx.pgclient; import io.vertx.core.Future; +import io.vertx.core.Handler; import io.vertx.core.Vertx; import io.vertx.ext.unit.Async; import io.vertx.ext.unit.TestContext; @@ -47,17 +48,21 @@ public void tearDown(TestContext ctx) { vertx.close().onComplete(ctx.asyncAssertSuccess()); } - protected PgPool createPool(PgConnectOptions connectOptions, int size) { + protected Pool createPool(PgConnectOptions connectOptions, int size) { return createPool(connectOptions, new PoolOptions().setMaxSize(size)); } - protected abstract PgPool createPool(PgConnectOptions connectOptions, PoolOptions poolOptions); + protected Pool createPool(PgConnectOptions connectOptions, PoolOptions poolOptions) { + return createPool(connectOptions, poolOptions, null); + } + + protected abstract Pool createPool(PgConnectOptions connectOptions, PoolOptions poolOptions, Handler connectHandler); @Test public void testPool(TestContext ctx) { int num = 1000; Async async = ctx.async(num); - PgPool pool = createPool(options, 4); + Pool pool = createPool(options, 4); for (int i = 0;i < num;i++) { pool .getConnection() @@ -83,7 +88,7 @@ public void testPool(TestContext ctx) { public void testQuery(TestContext ctx) { int num = 1000; Async async = ctx.async(num); - PgPool pool = createPool(options, 4); + Pool pool = createPool(options, 4); for (int i = 0;i < num;i++) { pool .query("SELECT id, randomnumber from WORLD") @@ -113,7 +118,7 @@ public void testCachedQueryWithParams(TestContext ctx) { private void testQueryWithParams(TestContext ctx, PgConnectOptions options) { int num = 2; Async async = ctx.async(num); - PgPool pool = createPool(options, 1); + Pool pool = createPool(options, 1); for (int i = 0;i < num;i++) { pool .preparedQuery("SELECT id, randomnumber from WORLD where id=$1") @@ -135,7 +140,7 @@ private void testQueryWithParams(TestContext ctx, PgConnectOptions options) { public void testUpdate(TestContext ctx) { int num = 1000; Async async = ctx.async(num); - PgPool pool = createPool(options, 4); + Pool pool = createPool(options, 4); for (int i = 0;i < num;i++) { pool .query("UPDATE Fortune SET message = 'Whatever' WHERE id = 9") @@ -156,7 +161,7 @@ public void testUpdate(TestContext ctx) { public void testUpdateWithParams(TestContext ctx) { int num = 1000; Async async = ctx.async(num); - PgPool pool = createPool(options, 4); + Pool pool = createPool(options, 4); for (int i = 0;i < num;i++) { pool .preparedQuery("UPDATE Fortune SET message = 'Whatever' WHERE id = $1") @@ -183,7 +188,7 @@ public void testReconnect(TestContext ctx) { conn.connect(); }); proxy.listen(8080, "localhost", ctx.asyncAssertSuccess(v1 -> { - PgPool pool = createPool(new PgConnectOptions(options).setPort(8080).setHost("localhost"), 1); + Pool pool = createPool(new PgConnectOptions(options).setPort(8080).setHost("localhost"), 1); pool .getConnection() .onComplete(ctx.asyncAssertSuccess(conn1 -> { @@ -212,7 +217,7 @@ public void testReconnect(TestContext ctx) { @Test public void testCancelRequest(TestContext ctx) { Async async = ctx.async(); - PgPool pool = createPool(options, 4); + Pool pool = createPool(options, 4); pool .getConnection() .onComplete(ctx.asyncAssertSuccess(conn -> { @@ -233,7 +238,7 @@ public void testCancelRequest(TestContext ctx) { @Test public void testWithConnection(TestContext ctx) { Async async = ctx.async(10); - PgPool pool = createPool(options, 1); + Pool pool = createPool(options, 1); Function>> success = conn -> conn.query("SELECT 1").execute(); Function>> failure = conn -> conn.query("SELECT does_not_exist").execute(); for (int i = 0;i < 10;i++) { diff --git a/vertx-pg-client/src/test/java/io/vertx/pgclient/PgPooledConnectionTest.java b/vertx-pg-client/src/test/java/io/vertx/pgclient/PgPooledConnectionTest.java index f58536f19..14ee61e14 100644 --- a/vertx-pg-client/src/test/java/io/vertx/pgclient/PgPooledConnectionTest.java +++ b/vertx-pg-client/src/test/java/io/vertx/pgclient/PgPooledConnectionTest.java @@ -19,6 +19,7 @@ import io.vertx.ext.unit.Async; import io.vertx.ext.unit.TestContext; +import io.vertx.sqlclient.Pool; import io.vertx.sqlclient.PoolOptions; import org.junit.Test; @@ -27,12 +28,12 @@ */ public class PgPooledConnectionTest extends PgConnectionTestBase { - private PgPool pool; + private Pool pool; public PgPooledConnectionTest() { connector = handler -> { if (pool == null) { - pool = PgPool.pool(vertx, new PgConnectOptions(options), new PoolOptions().setMaxSize(1)); + pool = PgBuilder.pool().connectingTo(new PgConnectOptions(options)).with(new PoolOptions().setMaxSize(1)).using(vertx).build(); } pool.getConnection().onComplete(handler); }; @@ -41,7 +42,7 @@ public PgPooledConnectionTest() { @Override public void tearDown(TestContext ctx) { if (pool != null) { - PgPool p = pool; + Pool p = pool; pool = null; p.close(); } 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 27823b4ef..c25b09d29 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 @@ -3,11 +3,13 @@ import io.vertx.core.Context; import io.vertx.core.Future; import io.vertx.core.Vertx; +import io.vertx.core.net.NetClientOptions; import io.vertx.ext.unit.Async; import io.vertx.ext.unit.TestContext; import io.vertx.ext.unit.junit.VertxUnitRunner; import io.vertx.pgclient.junit.ContainerPgRule; import io.vertx.pgclient.spi.PgDriver; +import io.vertx.sqlclient.Pool; import io.vertx.sqlclient.PoolOptions; import io.vertx.sqlclient.SqlConnection; import io.vertx.sqlclient.spi.ConnectionFactory; @@ -48,13 +50,13 @@ public void teardown(TestContext ctx) { @Test public void testListLoadBalancing(TestContext ctx) { - testLoadBalancing(ctx, PgPool.pool(vertx, Arrays.asList(db1.options(), db2.options()),new PoolOptions().setMaxSize(5))); + testLoadBalancing(ctx, PgBuilder.pool().connectingTo(Arrays.asList(db1.options(), db2.options())).with(new PoolOptions().setMaxSize(5)).using(vertx).build()); } @Test public void testAsyncLoadBalancing(TestContext ctx) { - PgPool pool = PgPool.pool(vertx, new PoolOptions().setMaxSize(5)); - ConnectionFactory provider = PgDriver.INSTANCE.createConnectionFactory(vertx); + 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 @@ -65,7 +67,7 @@ public Future apply(Context context) { testLoadBalancing(ctx, pool); } - private void testLoadBalancing(TestContext ctx, PgPool pool) { + private void testLoadBalancing(TestContext ctx, Pool pool) { int count = 5; Async async = ctx.async(count); List> futures = new ArrayList<>(); diff --git a/vertx-pg-client/src/test/java/io/vertx/pgclient/SharedPoolTest.java b/vertx-pg-client/src/test/java/io/vertx/pgclient/SharedPoolTest.java index bd8b2266e..85b5158b5 100644 --- a/vertx-pg-client/src/test/java/io/vertx/pgclient/SharedPoolTest.java +++ b/vertx-pg-client/src/test/java/io/vertx/pgclient/SharedPoolTest.java @@ -19,6 +19,7 @@ import io.vertx.ext.unit.Async; import io.vertx.ext.unit.TestContext; import io.vertx.ext.unit.junit.VertxUnitRunner; +import io.vertx.sqlclient.Pool; import io.vertx.sqlclient.PoolOptions; import io.vertx.sqlclient.SqlConnection; import org.junit.After; @@ -51,10 +52,10 @@ public void testUseSamePool(TestContext ctx) { int maxSize = 8; int instances = maxSize * 4; vertx.deployVerticle(() -> new AbstractVerticle() { - PgPool pool; + Pool pool; @Override public void start() { - pool = PgPool.pool(vertx, options, new PoolOptions().setMaxSize(maxSize).setShared(true)); + pool = PgBuilder.pool().connectingTo(options).with(new PoolOptions().setMaxSize(maxSize).setShared(true)).using(vertx).build(); pool .query("SELECT pg_sleep(0.5);SELECT count(*) FROM pg_stat_activity WHERE application_name LIKE '%vertx%'") .execute() @@ -73,10 +74,13 @@ public void testCloseAutomatically(TestContext ctx) { AtomicReference deployment = new AtomicReference<>(); Async async = ctx.async(); vertx.deployVerticle(() -> new AbstractVerticle() { - PgPool pool; + Pool pool; @Override public void start() { - pool = PgPool.pool(vertx, options, new PoolOptions().setMaxSize(maxSize).setShared(true)); + pool = PgBuilder.pool(builder -> builder + .with(new PoolOptions().setMaxSize(maxSize).setShared(true)) + .connectingTo(options) + .using(vertx)); pool .query("SELECT 1") .execute() @@ -115,14 +119,14 @@ public void testPartialClose(TestContext ctx) { int instances = maxSize * 4; Async async = ctx.async(); vertx.deployVerticle(new AbstractVerticle() { - PgPool pool; + Pool pool; @Override public void start() { - pool = PgPool.pool(vertx, options, new PoolOptions().setMaxSize(maxSize).setShared(true)); + pool = PgBuilder.pool().connectingTo(options).with(new PoolOptions().setMaxSize(maxSize).setShared(true)).using(vertx).build(); vertx.deployVerticle(() -> new AbstractVerticle() { @Override public void start(Promise startPromise) { - PgPool pool = PgPool.pool(vertx, options, new PoolOptions().setMaxSize(maxSize).setShared(true)); + Pool pool = PgBuilder.pool().connectingTo(options).with(new PoolOptions().setMaxSize(maxSize).setShared(true)).using(vertx).build(); pool.query("SELECT 1").execute() .mapEmpty() .onComplete(startPromise); diff --git a/vertx-pg-client/src/test/java/io/vertx/pgclient/UnixDomainSocketTest.java b/vertx-pg-client/src/test/java/io/vertx/pgclient/UnixDomainSocketTest.java index 1e7b978b8..b4df05f71 100644 --- a/vertx-pg-client/src/test/java/io/vertx/pgclient/UnixDomainSocketTest.java +++ b/vertx-pg-client/src/test/java/io/vertx/pgclient/UnixDomainSocketTest.java @@ -21,8 +21,9 @@ import io.vertx.core.VertxOptions; import io.vertx.ext.unit.TestContext; import io.vertx.ext.unit.junit.VertxUnitRunner; -import io.vertx.sqlclient.PoolOptions; +import io.vertx.sqlclient.Pool; import io.vertx.sqlclient.SqlClient; +import io.vertx.sqlclient.SqlConnectOptions; import io.vertx.sqlclient.impl.ConnectionFactoryBase; import org.junit.*; import org.junit.runner.RunWith; @@ -46,7 +47,7 @@ public class UnixDomainSocketTest { @ClassRule public static PgRule rule = new PgRule().domainSockets(nativeTransportEnabled); - private PgPool client; + private Pool client; private PgConnectOptions options; private Vertx vertx; @@ -76,7 +77,7 @@ public void after(TestContext ctx) { public void uriTest(TestContext context) { assumeTrue(options.isUsingDomainSocket()); String uri = "postgresql://postgres:postgres@/postgres?host=" + options.getHost() + "&port=" + options.getPort(); - client = PgPool.pool(vertx, uri); + client = PgBuilder.pool().connectingTo(uri).using(vertx).build(); client .getConnection() .onComplete(context.asyncAssertSuccess(SqlClient::close)); @@ -85,7 +86,7 @@ public void uriTest(TestContext context) { @Test public void simpleConnect(TestContext context) { assumeTrue(options.isUsingDomainSocket()); - client = PgPool.pool(vertx, new PgConnectOptions(options), new PoolOptions()); + client = PgBuilder.pool().connectingTo(new PgConnectOptions(options)).using(vertx).build(); client .getConnection() .onComplete(context.asyncAssertSuccess(pgConnection -> pgConnection.close().onComplete(context.asyncAssertSuccess()))); @@ -94,7 +95,7 @@ public void simpleConnect(TestContext context) { @Test public void connectWithVertxInstance(TestContext context) { assumeTrue(options.isUsingDomainSocket()); - client = PgPool.pool(vertx, new PgConnectOptions(options), new PoolOptions()); + client = PgBuilder.pool().connectingTo(new PgConnectOptions(options)).using(vertx).build(); client .getConnection() .onComplete(context.asyncAssertSuccess(pgConnection -> { @@ -106,7 +107,7 @@ public void connectWithVertxInstance(TestContext context) { @Test public void testIgnoreSslMode(TestContext context) { assumeTrue(options.isUsingDomainSocket()); - client = PgPool.pool(vertx, new PgConnectOptions(options).setSslMode(SslMode.REQUIRE), new PoolOptions()); + client = PgBuilder.pool().connectingTo(new PgConnectOptions(options).setSslMode(SslMode.REQUIRE)).using(vertx).build(); client .getConnection() .onComplete(context.asyncAssertSuccess(pgConnection -> { @@ -117,7 +118,7 @@ public void testIgnoreSslMode(TestContext context) { @Test public void testNativeTransportMustBeEnabled(TestContext ctx) { - PgPool pool = PgPool.pool(PgConnectOptions.fromUri("postgresql:///dbname?host=/var/lib/postgresql"), new PoolOptions()); + Pool pool = PgBuilder.pool().connectingTo(SqlConnectOptions.fromUri("postgresql:///dbname?host=/var/lib/postgresql")).build(); pool.getConnection().onComplete(ctx.asyncAssertFailure(err -> { assertEquals(ConnectionFactoryBase.NATIVE_TRANSPORT_REQUIRED, err.getMessage()); })); diff --git a/vertx-pg-client/src/test/java/io/vertx/pgclient/context/ContextTest.java b/vertx-pg-client/src/test/java/io/vertx/pgclient/context/ContextTest.java index 83210b519..d1f7bae5f 100644 --- a/vertx-pg-client/src/test/java/io/vertx/pgclient/context/ContextTest.java +++ b/vertx-pg-client/src/test/java/io/vertx/pgclient/context/ContextTest.java @@ -4,10 +4,10 @@ import io.vertx.core.Vertx; import io.vertx.ext.unit.Async; import io.vertx.ext.unit.TestContext; +import io.vertx.pgclient.PgBuilder; import io.vertx.pgclient.PgConnection; -import io.vertx.pgclient.PgPool; import io.vertx.pgclient.PgTestBase; -import io.vertx.sqlclient.PoolOptions; +import io.vertx.sqlclient.Pool; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -53,7 +53,7 @@ public void testPooledConnection(TestContext testCtx) { Async async = testCtx.async(); Context connCtx = vertx.getOrCreateContext(); connCtx.runOnContext(v1 -> { - PgPool pool = PgPool.pool(vertx, options, new PoolOptions()); + Pool pool = PgBuilder.pool().connectingTo(options).using(vertx).build(); appCtx.runOnContext(v -> { pool.getConnection().onComplete(testCtx.asyncAssertSuccess(conn -> { testCtx.assertEquals(appCtx, Vertx.currentContext()); @@ -75,7 +75,7 @@ public void testPoolQuery(TestContext testCtx) { Async async = testCtx.async(); Context connCtx = vertx.getOrCreateContext(); connCtx.runOnContext(v1 -> { - PgPool pool = PgPool.pool(vertx, options, new PoolOptions()); + Pool pool = PgBuilder.pool().connectingTo(options).using(vertx).build(); appCtx.runOnContext(v -> { pool .query("SELECT * FROM (VALUES ('Hello world')) t1 (col1) WHERE 1 = 1") diff --git a/vertx-pg-client/src/test/java/io/vertx/pgclient/tck/ClientConfig.java b/vertx-pg-client/src/test/java/io/vertx/pgclient/tck/ClientConfig.java index da2d04def..f1bd45cfe 100644 --- a/vertx-pg-client/src/test/java/io/vertx/pgclient/tck/ClientConfig.java +++ b/vertx-pg-client/src/test/java/io/vertx/pgclient/tck/ClientConfig.java @@ -16,9 +16,10 @@ */ package io.vertx.pgclient.tck; +import io.vertx.pgclient.PgBuilder; import io.vertx.pgclient.PgConnectOptions; import io.vertx.pgclient.PgConnection; -import io.vertx.pgclient.PgPool; +import io.vertx.sqlclient.Pool; import io.vertx.sqlclient.PoolOptions; import io.vertx.sqlclient.tck.Connector; import io.vertx.sqlclient.SqlClient; @@ -54,7 +55,11 @@ public void close() { POOLED() { @Override Connector connect(Vertx vertx, SqlConnectOptions options) { - PgPool pool = PgPool.pool(vertx, new PgConnectOptions(options), new PoolOptions().setMaxSize(1)); + Pool pool = PgBuilder + .pool() + .connectingTo(new PgConnectOptions(options)) + .with(new PoolOptions().setMaxSize(1)) + .using(vertx).build(); return new Connector() { @Override public void connect(Handler> handler) { diff --git a/vertx-pg-client/src/test/java/io/vertx/pgclient/tck/PgPipeliningQueryTest.java b/vertx-pg-client/src/test/java/io/vertx/pgclient/tck/PgPipeliningQueryTest.java index 2735cce70..dada0b7a6 100644 --- a/vertx-pg-client/src/test/java/io/vertx/pgclient/tck/PgPipeliningQueryTest.java +++ b/vertx-pg-client/src/test/java/io/vertx/pgclient/tck/PgPipeliningQueryTest.java @@ -1,8 +1,8 @@ package io.vertx.pgclient.tck; import io.vertx.ext.unit.junit.VertxUnitRunner; +import io.vertx.pgclient.PgBuilder; import io.vertx.pgclient.PgConnectOptions; -import io.vertx.pgclient.PgPool; import io.vertx.pgclient.junit.ContainerPgRule; import io.vertx.sqlclient.PoolOptions; import io.vertx.sqlclient.tck.PipeliningQueryTestBase; @@ -21,7 +21,7 @@ protected void init() { pgConnectOptions.setPipeliningLimit(64); connectionConnector = ClientConfig.CONNECT.connect(vertx, options); pooledConnectionConnector = ClientConfig.POOLED.connect(vertx, options); - pooledClientSupplier = () -> PgPool.client(vertx, (PgConnectOptions) options, new PoolOptions().setMaxSize(8)); + pooledClientSupplier = () -> PgBuilder.client(b -> b.connectingTo(options).with(new PoolOptions().setMaxSize(8)).using(vertx)); } @Override diff --git a/vertx-pg-client/src/test/java/io/vertx/pgclient/tck/PgTracingTest.java b/vertx-pg-client/src/test/java/io/vertx/pgclient/tck/PgTracingTest.java index 81f9c58b0..50cda1a1c 100644 --- a/vertx-pg-client/src/test/java/io/vertx/pgclient/tck/PgTracingTest.java +++ b/vertx-pg-client/src/test/java/io/vertx/pgclient/tck/PgTracingTest.java @@ -13,10 +13,9 @@ import io.vertx.core.Vertx; import io.vertx.ext.unit.junit.VertxUnitRunner; -import io.vertx.pgclient.PgPool; +import io.vertx.pgclient.PgBuilder; import io.vertx.pgclient.junit.ContainerPgRule; import io.vertx.sqlclient.Pool; -import io.vertx.sqlclient.PoolOptions; import io.vertx.sqlclient.tck.TracingTestBase; import org.junit.ClassRule; import org.junit.runner.RunWith; @@ -28,7 +27,7 @@ public class PgTracingTest extends TracingTestBase { @Override protected Pool createPool(Vertx vertx) { - return PgPool.pool(vertx, rule.options(), new PoolOptions()); + return PgBuilder.pool().connectingTo(rule.options()).using(vertx).build(); } @Override diff --git a/vertx-pg-client/src/test/java/io/vertx/pgclient/tck/PgTransactionTest.java b/vertx-pg-client/src/test/java/io/vertx/pgclient/tck/PgTransactionTest.java index 9dc33bafd..ccaccd6e7 100644 --- a/vertx-pg-client/src/test/java/io/vertx/pgclient/tck/PgTransactionTest.java +++ b/vertx-pg-client/src/test/java/io/vertx/pgclient/tck/PgTransactionTest.java @@ -18,9 +18,8 @@ import io.vertx.ext.unit.Async; import io.vertx.ext.unit.TestContext; import io.vertx.ext.unit.junit.VertxUnitRunner; -import io.vertx.pgclient.PgConnectOptions; +import io.vertx.pgclient.PgBuilder; import io.vertx.pgclient.PgException; -import io.vertx.pgclient.PgPool; import io.vertx.pgclient.junit.ContainerPgRule; import io.vertx.sqlclient.Pool; import io.vertx.sqlclient.PoolOptions; @@ -37,12 +36,12 @@ public class PgTransactionTest extends TransactionTestBase { @Override protected Pool createPool() { - return PgPool.pool(vertx, new PgConnectOptions(rule.options()), new PoolOptions().setMaxSize(1)); + return PgBuilder.pool().connectingTo(rule.options()).with(new PoolOptions().setMaxSize(1)).using(vertx).build(); } @Override protected Pool nonTxPool() { - return PgPool.pool(vertx, new PgConnectOptions(rule.options()), new PoolOptions().setMaxSize(1)); + return PgBuilder.pool().connectingTo(rule.options()).with(new PoolOptions().setMaxSize(1)).using(vertx).build(); } @Override diff --git a/vertx-sql-client-templates/src/test/java/io/vertx/sqlclient/templates/MySQLTest.java b/vertx-sql-client-templates/src/test/java/io/vertx/sqlclient/templates/MySQLTest.java index efb07d8cf..2221221bb 100644 --- a/vertx-sql-client-templates/src/test/java/io/vertx/sqlclient/templates/MySQLTest.java +++ b/vertx-sql-client-templates/src/test/java/io/vertx/sqlclient/templates/MySQLTest.java @@ -3,9 +3,9 @@ import io.vertx.core.Vertx; import io.vertx.ext.unit.TestContext; import io.vertx.ext.unit.junit.VertxUnitRunner; +import io.vertx.mysqlclient.MySQLBuilder; import io.vertx.mysqlclient.MySQLConnectOptions; -import io.vertx.mysqlclient.MySQLPool; -import io.vertx.sqlclient.PoolOptions; +import io.vertx.sqlclient.Pool; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; @@ -53,12 +53,12 @@ public static MySQLConnectOptions connectOptions() { } protected Vertx vertx; - protected MySQLPool pool; + protected Pool pool; @Before public void setup() throws Exception { vertx = Vertx.vertx(); - pool = MySQLPool.pool(vertx, connectOptions(), new PoolOptions()); + pool = MySQLBuilder.pool(builder -> builder.connectingTo(connectOptions()).using(vertx)); } @Test 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 890ff0b1f..2edcf7d48 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 @@ -4,6 +4,7 @@ import io.vertx.core.Handler; 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; @@ -46,11 +47,11 @@ 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, CloseFuture closeFuture) { + public Pool newPool(Vertx vertx, Supplier> databases, PoolOptions options, NetClientOptions transportOptions, CloseFuture closeFuture) { throw new UnsupportedOperationException(); } @Override - public ConnectionFactory createConnectionFactory(Vertx vertx) { + public ConnectionFactory createConnectionFactory(Vertx vertx, NetClientOptions transportOptions) { throw new UnsupportedOperationException(); } @Override diff --git a/vertx-sql-client/src/main/asciidoc/pool_config.adoc b/vertx-sql-client/src/main/asciidoc/pool_config.adoc index 5b4543064..79b876d08 100644 --- a/vertx-sql-client/src/main/asciidoc/pool_config.adoc +++ b/vertx-sql-client/src/main/asciidoc/pool_config.adoc @@ -13,8 +13,8 @@ NOTE: this provides load balancing when the connection is created and not when t === Pool connection initialization -You can use the {@link io.vertx.sqlclient.Pool#connectHandler} to interact with a connection after it -has been created and before it is inserted in the pool. +You can use the {@link io.vertx.sqlclient.ClientBuilder#withConnectHandler} to interact with a connection after it +has been created and before it is inserted in a pool. [source,$lang] ---- diff --git a/vertx-sql-client/src/main/java/examples/SqlClientExamples.java b/vertx-sql-client/src/main/java/examples/SqlClientExamples.java index 9dc9d254e..d7695e794 100644 --- a/vertx-sql-client/src/main/java/examples/SqlClientExamples.java +++ b/vertx-sql-client/src/main/java/examples/SqlClientExamples.java @@ -412,8 +412,8 @@ public void poolConfig01(SqlConnectOptions server1, SqlConnectOptions server2, S // Not generic } - public void poolConfig02(Pool pool, String sql) { - pool.connectHandler(conn -> { + public void poolConfig02(ClientBuilder builder, String sql) { + builder.withConnectHandler(conn -> { conn.query(sql).execute().onSuccess(res -> { // Release the connection to the pool, ready to be used by the application conn.close(); 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 new file mode 100644 index 000000000..210ca0f32 --- /dev/null +++ b/vertx-sql-client/src/main/java/io/vertx/sqlclient/ClientBuilder.java @@ -0,0 +1,144 @@ +/* + * Copyright (c) 2011-2023 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.sqlclient; + +import io.vertx.codegen.annotations.Fluent; +import io.vertx.codegen.annotations.GenIgnore; +import io.vertx.codegen.annotations.VertxGen; +import io.vertx.core.Future; +import io.vertx.core.Handler; +import io.vertx.core.Vertx; +import io.vertx.core.net.NetClientOptions; +import io.vertx.sqlclient.impl.ClientBuilderBase; +import io.vertx.sqlclient.spi.Driver; + +import java.util.List; +import java.util.function.Supplier; + +/** + * Builder for {@link SqlClient} instances. + */ +@VertxGen +public interface ClientBuilder { + + /** + * Provide a builder for a pool of connections for the specified {@link Driver} + *

+ * Example usage: {@code Pool pool = ClientBuilder.pool(driver).connectingTo(connectOptions).build()} + */ + @GenIgnore + 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); + } + }; + } + + /** + * Build a pool with the specified {@code block} argument and {@link Driver} + * The {@code block} argument is usually a lambda that configures the provided builder + *

+ * Example usage: {@code Pool pool = ClientBuilder.pool(driver, builder -> builder.connectingTo(connectOptions));} + * + * @return the pool as configured by the code {@code block} + */ + @GenIgnore + static Pool pool(Driver driver, Handler> block) { + ClientBuilder builder = pool(driver); + block.handle(builder); + return builder.build(); + } + + /** + * Configure the client with the given pool {@code options} + * @param options the pool options + * @return a reference to this, so the API can be used fluently + */ + @Fluent + ClientBuilder with(PoolOptions options); + + /** + * Configure the client pool with the given transport {@code options}. + * Note: the SSL options part is ignored, since {@link SqlConnectOptions} configures the transport SSL. + * + * @param options the transport options + * @return a reference to this, so the API can be used fluently + */ + @Fluent + ClientBuilder with(NetClientOptions options); + + /** + * Configure the {@code database} the client should connect to. The target {@code database} is specified as + * a {@link SqlConnectOptions} coordinates. + * @param database the database coordinates + * @return a reference to this, so the API can be used fluently + */ + @Fluent + ClientBuilder connectingTo(SqlConnectOptions database); + + /** + * Configure the {@code database} the client should connect to. The target {@code database} is specified as + * a {@link SqlConnectOptions} coordinates. + * @param database the database URI + * @return a reference to this, so the API can be used fluently + */ + @Fluent + ClientBuilder connectingTo(String database); + + /** + * Configure the {@code database} the client should connect to. When the client needs to connect to the database, + * it gets fresh database configuration from the database {@code supplier}. + * @param supplier the supplier of database coordinates + * @return a reference to this, so the API can be used fluently + */ + @Fluent + ClientBuilder connectingTo(Supplier> supplier); + + /** + * Configure the {@code database} the client should connect to. When the client needs to connect to the database, + * it gets a database configuration from the list of {@code databases} using a round-robin policy. + * @param databases the list of database coordinates + * @return a reference to this, so the API can be used fluently + */ + @Fluent + ClientBuilder connectingTo(List databases); + + /** + * Sets the vertx instance to use. + * @param vertx the vertx instance + * @return a reference to this, so the API can be used fluently + */ + @Fluent + ClientBuilder using(Vertx vertx); + + /** + * Set a 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 + */ + @Fluent + ClientBuilder withConnectHandler(Handler handler); + + /** + * Build and return the client. + * @return the client + */ + C build(); + +} 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 b3c1063ed..8bd07b48a 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 @@ -31,10 +31,13 @@ import io.vertx.core.Handler; import io.vertx.core.Vertx; import io.vertx.core.impl.ContextInternal; +import io.vertx.core.net.NetClientOptions; import io.vertx.sqlclient.impl.PoolImpl; +import io.vertx.sqlclient.impl.Utils; import io.vertx.sqlclient.spi.Driver; import java.util.function.Function; +import java.util.function.Supplier; import static io.vertx.sqlclient.impl.PoolImpl.startPropagatableConnection; @@ -74,8 +77,8 @@ static Pool pool(SqlConnectOptions database, PoolOptions options) { * @throws ServiceConfigurationError if no compatible drivers are found, or if multiple compatible drivers are found */ static Pool pool(Vertx vertx, SqlConnectOptions database, PoolOptions options) { - List candidates = new ArrayList<>(1); - for (Driver d : ServiceLoader.load(Driver.class)) { + List> candidates = new ArrayList<>(1); + for (Driver d : ServiceLoader.load(Driver.class)) { if (d.acceptsOptions(database)) { candidates.add(d); } @@ -85,8 +88,8 @@ static Pool pool(Vertx vertx, SqlConnectOptions database, PoolOptions options) { } else if (candidates.size() > 1) { throw new ServiceConfigurationError("Multiple implementations of " + Driver.class + " found: " + candidates); } else { - Driver driver = candidates.get(0); - return candidates.get(0).createPool(vertx, Collections.singletonList(driver.downcast(database)), options); + Driver driver = candidates.get(0); + return driver.createPool(vertx, Utils.singletonSupplier(database), options, new NetClientOptions()); } } @@ -200,7 +203,9 @@ static Pool pool(Vertx vertx, SqlConnectOptions database, PoolOptions options) { * * @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); @@ -212,7 +217,9 @@ static Pool pool(Vertx vertx, SqlConnectOptions database, PoolOptions options) { * * @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); diff --git a/vertx-sql-client/src/main/java/io/vertx/sqlclient/SqlClient.java b/vertx-sql-client/src/main/java/io/vertx/sqlclient/SqlClient.java index 3ff2f2f19..a64885e19 100644 --- a/vertx-sql-client/src/main/java/io/vertx/sqlclient/SqlClient.java +++ b/vertx-sql-client/src/main/java/io/vertx/sqlclient/SqlClient.java @@ -18,10 +18,7 @@ package io.vertx.sqlclient; import io.vertx.codegen.annotations.VertxGen; -import io.vertx.core.AsyncResult; import io.vertx.core.Future; -import io.vertx.core.Handler; -import io.vertx.sqlclient.spi.DatabaseMetadata; /** * Defines common SQL client operations with a database server. 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 new file mode 100644 index 000000000..f9d67a041 --- /dev/null +++ b/vertx-sql-client/src/main/java/io/vertx/sqlclient/impl/ClientBuilderBase.java @@ -0,0 +1,106 @@ +/* + * 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.sqlclient.impl; + +import io.vertx.core.Future; +import io.vertx.core.Handler; +import io.vertx.core.Vertx; +import io.vertx.core.net.NetClientOptions; +import io.vertx.sqlclient.*; +import io.vertx.sqlclient.spi.Driver; + +import java.util.List; +import java.util.function.Supplier; + +public abstract class ClientBuilderBase implements ClientBuilder { + + protected final Driver driver; + private PoolOptions poolOptions; + private NetClientOptions transportOptions; + private Supplier> database; + private Handler connectionHandler; + private Vertx vertx; + + public ClientBuilderBase(Driver driver) { + this.driver = (Driver) driver; + } + + @Override + public ClientBuilder with(PoolOptions options) { + this.poolOptions = options; + return this; + } + + @Override + public ClientBuilder with(NetClientOptions options) { + this.transportOptions = options; + return this; + } + + @Override + public ClientBuilder connectingTo(SqlConnectOptions database) { + return connectingTo(SingletonSupplier.wrap(database)); + } + + @Override + public ClientBuilder connectingTo(String database) { + return connectingTo(driver.parseConnectionUri(database)); + } + + @Override + public ClientBuilder connectingTo(Supplier> supplier) { + this.database = supplier; + return this; + } + + @Override + public ClientBuilder connectingTo(List databases) { + return connectingTo(Utils.roundRobinSupplier(databases)); + } + + @Override + public ClientBuilder withConnectHandler(Handler handler) { + this.connectionHandler = handler; + return this; + } + + @Override + public ClientBuilder using(Vertx vertx) { + this.vertx = vertx; + return this; + } + + @Override + public final C build() { + PoolOptions poolOptions = this.poolOptions; + if (poolOptions == null) { + poolOptions = new PoolOptions(); + } + NetClientOptions transportOptions = this.transportOptions; + if (transportOptions == null) { + transportOptions = new NetClientOptions(); + } + C c = create(vertx, database, poolOptions, transportOptions); + if (c instanceof Pool) { + ((Pool)c).connectHandler(connectionHandler); + } + return c; + } + + protected abstract C create(Vertx vertx, Supplier> databases, PoolOptions poolOptions, NetClientOptions transportOptions); + +} diff --git a/vertx-sql-client/src/main/java/io/vertx/sqlclient/impl/Utils.java b/vertx-sql-client/src/main/java/io/vertx/sqlclient/impl/Utils.java index 7e11fb268..434ae330b 100644 --- a/vertx-sql-client/src/main/java/io/vertx/sqlclient/impl/Utils.java +++ b/vertx-sql-client/src/main/java/io/vertx/sqlclient/impl/Utils.java @@ -55,4 +55,8 @@ public Future get() { } }; } + + public static Supplier> singletonSupplier(T factory) { + return () -> Future.succeededFuture(factory); + } } 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 f89386e43..8ffee4d7e 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 @@ -22,6 +22,7 @@ import io.vertx.core.impl.CloseFuture; import io.vertx.core.impl.ContextInternal; import io.vertx.core.impl.VertxInternal; +import io.vertx.core.net.NetClientOptions; import io.vertx.sqlclient.Pool; import io.vertx.sqlclient.PoolOptions; import io.vertx.sqlclient.SqlConnectOptions; @@ -42,12 +43,13 @@ 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 options the options for creating the pool + * @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 * @return the connection pool */ - default Pool createPool(Vertx vertx, Supplier> databases, PoolOptions options) { + default Pool createPool(Vertx vertx, Supplier> databases, PoolOptions poolOptions, NetClientOptions transportOptions) { VertxInternal vx; if (vertx == null) { if (Vertx.currentContext() != null) { @@ -60,7 +62,7 @@ default Pool createPool(Vertx vertx, Supplier> databases, PoolOptions CloseFuture closeFuture = new CloseFuture(); Pool pool; try { - pool = newPool(vx, databases, options, closeFuture); + pool = newPool(vx, databases, poolOptions, transportOptions, closeFuture); } catch (Exception e) { if (vertx == null) { vx.close(); @@ -82,54 +84,26 @@ 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)}. * - *

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 options the options for creating the pool - * @return the connection pool - */ - default Pool createPool(Vertx vertx, List databases, PoolOptions options) { - return createPool(vertx, Utils.roundRobinSupplier(databases), options); - } - - /** - * 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, List, PoolOptions)}. - * - * @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 closeFuture the close future - * @return the connection pool - */ - default Pool newPool(Vertx vertx, List databases, PoolOptions options, CloseFuture closeFuture) { - return newPool(vertx, Utils.roundRobinSupplier(databases), options, closeFuture); - } - - /** - * 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, List, PoolOptions)}. - * - * @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 closeFuture the close future + * @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 closeFuture the close future * @return the connection pool */ - Pool newPool(Vertx vertx, Supplier> databases, PoolOptions options, CloseFuture closeFuture); + Pool newPool(Vertx vertx, Supplier> databases, PoolOptions options, NetClientOptions transportOptions, CloseFuture closeFuture); /** * Create a connection factory to the given {@code database}. * - * @param vertx the Vertx instance + * @param vertx the Vertx instance + * @param transportOptions the options to configure the TCP client * @return the connection factory */ - ConnectionFactory createConnectionFactory(Vertx vertx); + ConnectionFactory createConnectionFactory(Vertx vertx, NetClientOptions transportOptions); /** * @return {@code true} if the driver accepts the {@code connectOptions}, {@code false} otherwise 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 f578f29c0..7f713cd50 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 @@ -12,6 +12,8 @@ import java.util.function.Function; import java.util.function.Supplier; +import io.vertx.core.Future; +import io.vertx.core.net.NetClientOptions; import io.vertx.ext.unit.Async; import org.junit.Test; @@ -52,7 +54,7 @@ public void testRejectsOtherOptions() { @Test public void testCreatePoolFromDriver(TestContext ctx) { - testCreatePoolWithVertx(ctx, vertx -> getDriver().createPool(vertx, Collections.singletonList(defaultOptions()), new PoolOptions().setMaxSize(1))); + testCreatePoolWithVertx(ctx, vertx -> getDriver().createPool(vertx, () -> Future.succeededFuture(defaultOptions()), new PoolOptions().setMaxSize(1), new NetClientOptions())); } @Test @@ -138,7 +140,7 @@ public void testRejectCreatePool03(TestContext ctx) { public static class BogusOptions extends SqlConnectOptions { } - private Driver getDriver() { + private Driver getDriver() { return ServiceLoader.load(Driver.class).iterator().next(); } }