Skip to content

Commit 0a24a15

Browse files
committed
Move some code from PoolBase as default methods in Pool to ease reuse
1 parent 8047a92 commit 0a24a15

File tree

2 files changed

+26
-36
lines changed

2 files changed

+26
-36
lines changed

vertx-sql-client/src/main/java/io/vertx/sqlclient/Pool.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,12 +145,31 @@ static Pool pool(Vertx vertx, SqlConnectOptions connectOptions, PoolOptions pool
145145
* @param function the code to execute
146146
* @param handler the result handler
147147
*/
148-
<T> void withTransaction(Function<SqlClient, Future<T>> function, Handler<AsyncResult<T>> handler);
148+
default <T> void withTransaction(Function<SqlClient, Future<T>> function, Handler<AsyncResult<T>> handler) {
149+
Future<T> res = withTransaction(function);
150+
if (handler != null) {
151+
res.onComplete(handler);
152+
}
153+
}
149154

150155
/**
151156
* Like {@link #withTransaction(Function, Handler)} but returns a {@code Future} of the asynchronous result
152157
*/
153-
<T> Future<T> withTransaction(Function<SqlClient, Future<T>> function);
158+
default <T> Future<T> withTransaction(Function<SqlClient, Future<T>> function) {
159+
return getConnection()
160+
.flatMap(conn -> conn
161+
.begin()
162+
.flatMap(tx -> function
163+
.apply(conn)
164+
.compose(
165+
res -> tx
166+
.commit()
167+
.flatMap(v -> Future.succeededFuture(res)),
168+
err -> tx
169+
.rollback()
170+
.flatMap(v -> Future.failedFuture(err))))
171+
.onComplete(ar -> conn.close()));
172+
}
154173

155174
/**
156175
* Close the pool and release the associated resources.

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

Lines changed: 5 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import io.vertx.core.impl.VertxInternal;
2525
import io.vertx.sqlclient.Pool;
2626
import io.vertx.sqlclient.PoolOptions;
27-
import io.vertx.sqlclient.SqlClient;
2827
import io.vertx.sqlclient.SqlConnection;
2928
import io.vertx.sqlclient.impl.command.CommandBase;
3029
import io.vertx.core.AsyncResult;
@@ -33,8 +32,6 @@
3332
import io.vertx.sqlclient.impl.pool.ConnectionPool;
3433
import io.vertx.sqlclient.impl.tracing.QueryTracer;
3534

36-
import java.util.function.Function;
37-
3835
/**
3936
* @author <a href="mailto:julien@julienviet.com">Julien Viet</a>
4037
* @author <a href="mailto:emad.albloushi@gmail.com">Emad Alblueshi</a>
@@ -75,11 +72,6 @@ protected <T> Promise<T> promise(Handler<AsyncResult<T>> handler) {
7572
*/
7673
public abstract void connect(Handler<AsyncResult<Connection>> completionHandler);
7774

78-
private void acquire(Handler<AsyncResult<Connection>> completionHandler) {
79-
pool.acquire(completionHandler);
80-
}
81-
82-
8375
@Override
8476
public void getConnection(Handler<AsyncResult<SqlConnection>> handler) {
8577
Future<SqlConnection> fut = getConnection();
@@ -100,31 +92,6 @@ public Future<SqlConnection> getConnection() {
10092
});
10193
}
10294

103-
@Override
104-
public <T> void withTransaction(Function<SqlClient, Future<T>> function, Handler<AsyncResult<T>> handler) {
105-
Future<T> res = withTransaction(function);
106-
if (handler != null) {
107-
res.onComplete(handler);
108-
}
109-
}
110-
111-
@Override
112-
public <T> Future<T> withTransaction(Function<SqlClient, Future<T>> function) {
113-
return getConnection()
114-
.flatMap(conn -> conn
115-
.begin()
116-
.flatMap(tx -> function
117-
.apply(conn)
118-
.compose(
119-
res -> tx
120-
.commit()
121-
.flatMap(v -> Future.succeededFuture(res)),
122-
err -> tx
123-
.rollback()
124-
.flatMap(v -> Future.failedFuture(err))))
125-
.onComplete(ar -> conn.close()));
126-
}
127-
12895
@Override
12996
public <R> void schedule(CommandBase<R> cmd, Promise<R> promise) {
13097
acquire(new CommandWaiter() {
@@ -141,7 +108,11 @@ protected void onFailure(Throwable cause) {
141108
});
142109
}
143110

144-
private abstract class CommandWaiter implements Connection.Holder, Handler<AsyncResult<Connection>> {
111+
private void acquire(Handler<AsyncResult<Connection>> completionHandler) {
112+
pool.acquire(completionHandler);
113+
}
114+
115+
private static abstract class CommandWaiter implements Connection.Holder, Handler<AsyncResult<Connection>> {
145116

146117
protected abstract void onSuccess(Connection conn);
147118

0 commit comments

Comments
 (0)