Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion vertx-pg-client/src/main/asciidoc/index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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]
----
Expand Down
74 changes: 56 additions & 18 deletions vertx-pg-client/src/main/java/examples/PgClientExamples.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@
import io.vertx.core.json.JsonObject;
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;
Expand Down Expand Up @@ -59,7 +56,11 @@ public void gettingStarted() {
.setMaxSize(5);

// Create the client pool
SqlClient client = PgPool.client(connectOptions, poolOptions);
SqlClient client = PgBuilder
.client()
.config(poolOptions)
.connectingTo(connectOptions)
.build();

// A simple query
client
Expand All @@ -81,7 +82,7 @@ public void gettingStarted() {
public void configureFromEnv(Vertx vertx) {

// Create the pool from the environment variables
PgPool pool = PgPool.pool();
Pool pool = PgBuilder.pool().build();

// Create the connection from the environment variables
PgConnection.connect(vertx)
Expand All @@ -104,7 +105,7 @@ 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().connectingTo(connectOptions).config(poolOptions).using(vertx).build();

pool.getConnection()
.onComplete(ar -> {
Expand All @@ -128,7 +129,7 @@ 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(PgConnectOptions.fromUri(connectionUri)).build();

// Create the connection from the connection URI
PgConnection
Expand All @@ -153,7 +154,11 @@ public void connecting01() {
.setMaxSize(5);

// Create the pooled client
SqlClient client = PgPool.client(connectOptions, poolOptions);
SqlClient client = PgBuilder
.client()
.config(poolOptions)
.connectingTo(connectOptions)
.build();
}

public void connecting02(Vertx vertx) {
Expand All @@ -171,10 +176,15 @@ public void connecting02(Vertx vertx) {
.setMaxSize(5);

// Create the pooled client
SqlClient client = PgPool.client(vertx, connectOptions, poolOptions);
SqlClient client = PgBuilder
.client()
.config(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();
Expand All @@ -195,7 +205,12 @@ public void connecting04(Vertx vertx) {
.setMaxSize(5);

// Create the pooled client
PgPool pool = PgPool.pool(vertx, connectOptions, poolOptions);
Pool pool = PgBuilder
.pool()
.config(poolOptions)
.connectingTo(connectOptions)
.using(vertx)
.build();

// Get a connection from the pool
pool.getConnection().compose(conn -> {
Expand All @@ -214,7 +229,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());
Expand Down Expand Up @@ -259,19 +273,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))
.config(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()
.config(poolOptions)
.connectingTo(connectOptions)
.using(vertx)
.build();

// Pipelined
Future<RowSet<Row>> res1 = client.query(sql).execute();

// Connection pool
PgPool pool = PgPool.pool(vertx, connectOptions, poolOptions);
Pool pool = PgBuilder
.pool()
.connectingTo(connectOptions)
.config(poolOptions)
.using(vertx)
.build();

// Not pipelined
Future<RowSet<Row>> res2 = pool.query(sql).execute();
Expand All @@ -291,11 +320,20 @@ public void unixDomainSockets(Vertx vertx) {
.setMaxSize(5);

// Create the pooled client
PgPool client = PgPool.pool(connectOptions, poolOptions);
Pool client = PgBuilder
.pool()
.connectingTo(connectOptions)
.config(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)
.config(poolOptions)
.using(vertx)
.build();
}

public void reconnectAttempts(PgConnectOptions options) {
Expand Down
48 changes: 27 additions & 21 deletions vertx-pg-client/src/main/java/examples/SqlClientExamples.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -365,10 +363,10 @@ public void tracing01(PgConnectOptions options) {
}

public void poolConfig01(PgConnectOptions server1, PgConnectOptions server2, PgConnectOptions server3, PoolOptions options) {
PgPool pool = PgPool.pool(Arrays.asList(server1, server2, server3), options);
Pool pool = PgBuilder.pool().connectingTo(Arrays.asList(server1, server2, server3)).config(options).build();
}

public void poolConfig02(PgPool pool, String sql) {
public void poolConfig02(Pool pool, String sql) {
pool.connectHandler(conn -> {
conn.query(sql).execute().onSuccess(res -> {
// Release the connection to the pool, ready to be used by the application
Expand All @@ -378,7 +376,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 = PgBuilder.pool().config(new PoolOptions().setMaxSize(maxSize)).connectingTo(database).build();
vertx.deployVerticle(() -> new AbstractVerticle() {
@Override
public void start() throws Exception {
Expand All @@ -389,36 +387,44 @@ 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().
config(new PoolOptions()
.setMaxSize(maxSize)
.setShared(true)
.setName("my-pool")).connectingTo(database)
.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()
.config(new PoolOptions()
.setMaxSize(maxSize)
.setShared(true)
.setName("my-pool")
.setEventLoopSize(4)).connectingTo(database)
.build();
}

public void dynamicPoolConfig(Vertx vertx, PoolOptions poolOptions) {
PgPool pool = PgPool.pool(vertx, () -> {
Future<PgConnectOptions> connectOptions = retrieveOptions();
return connectOptions;
}, poolOptions);
Pool pool = PgBuilder.pool()
.config(poolOptions)
.connectingTo(() -> {
Future<SqlConnectOptions> connectOptions = retrieveOptions();
return connectOptions;
})
.using(vertx)
.build();
}

private Future<PgConnectOptions> retrieveOptions() {
private Future<SqlConnectOptions> retrieveOptions() {
return null;
}
}
98 changes: 98 additions & 0 deletions vertx-pg-client/src/main/java/io/vertx/pgclient/PgBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* 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.Handler;
import io.vertx.pgclient.impl.PgPoolOptions;
import io.vertx.pgclient.spi.PgDriver;
import io.vertx.sqlclient.*;
import io.vertx.sqlclient.impl.ClientBuilderBase;

/**
* 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
* <p>
* 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<ClientBuilder<Pool>> block) {
ClientBuilder<Pool> builder = pool();
block.handle(builder);
return builder.build();
}

/**
* Provide a builder for PostgreSQL pool of connections
* <p>
* Example usage: {@code Pool pool = PgBuilder.pool().connectingTo(connectOptions).build()}
*/
static ClientBuilder<Pool> pool() {
return new ClientBuilderBase<Pool>(PgDriver.INSTANCE) {
@Override
public Pool build() {
PoolOptions options = poolOptions;
if (options == null) {
options = new PoolOptions();
}
return driver.createPool(vertx, database, options);
}
};
}

/**
* 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
* <p>
* 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<ClientBuilder<SqlClient>> handler) {
ClientBuilder<SqlClient> builder = client();
handler.handle(builder);
return builder.build();
}

/**
* Provide a builder for PostgreSQL client backed by a connection pool.
* <p>
* Example usage: {@code SqlClient client = PgBuilder.client().connectingTo(connectOptions).build()}
*/
static ClientBuilder<SqlClient> client() {
return new ClientBuilderBase<SqlClient>(PgDriver.INSTANCE) {
@Override
public Pool build() {
PoolOptions options = poolOptions;
if (options != null) {
options = new PgPoolOptions(poolOptions).setPipelined(true);
} else {
options = new PoolOptions();
}
return driver.createPool(vertx, database, options);
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
*
* @author <a href="mailto:julien@julienviet.com">Julien Viet</a>
*/
@Deprecated
@VertxGen
public interface PgPool extends Pool {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ public PgPoolOptions(PoolOptions other) {
super(other);
}

public PgPoolOptions() {
}

private boolean pipelined;

public boolean isPipelined() {
Expand Down
Loading