Skip to content

Commit 883a144

Browse files
committed
The new withTransaction method should be a default method so the JDBC implementation does not need to implement it
1 parent 8b28a02 commit 883a144

File tree

2 files changed

+24
-23
lines changed

2 files changed

+24
-23
lines changed

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,14 @@
3131
import io.vertx.core.Future;
3232
import io.vertx.core.Handler;
3333
import io.vertx.core.Vertx;
34+
import io.vertx.core.impl.ContextInternal;
35+
import io.vertx.sqlclient.impl.PoolImpl;
3436
import io.vertx.sqlclient.spi.Driver;
3537

3638
import java.util.function.Function;
3739

40+
import static io.vertx.sqlclient.impl.PoolImpl.startPropagatableConnection;
41+
3842
/**
3943
* A connection pool which reuses a number of SQL connections.
4044
*
@@ -168,7 +172,23 @@ default <T> void withTransaction(Function<SqlConnection, Future<@Nullable T>> fu
168172
* Like {@link #withTransaction(Function, Handler)} but allows for setting the mode, defining how the acquired
169173
* connection is managed during the execution of the function.
170174
*/
171-
<T> Future<@Nullable T> withTransaction(TransactionPropagation txPropagation, Function<SqlConnection, Future<@Nullable T>> function);
175+
default <T> Future<@Nullable T> withTransaction(TransactionPropagation txPropagation, Function<SqlConnection, Future<@Nullable T>> function) {
176+
if (txPropagation == TransactionPropagation.CONTEXT) {
177+
ContextInternal context = (ContextInternal) Vertx.currentContext();
178+
SqlConnection sqlConnection = context.getLocal(PoolImpl.PROPAGATABLE_CONNECTION);
179+
if (sqlConnection == null) {
180+
return startPropagatableConnection(this, function);
181+
}
182+
return context.succeededFuture(sqlConnection)
183+
.flatMap(conn -> function.apply(conn)
184+
.onFailure(err -> {
185+
if (!(err instanceof TransactionRollbackException)) {
186+
conn.transaction().rollback();
187+
}
188+
}));
189+
}
190+
return withTransaction(function);
191+
}
172192

173193
/**
174194
* Get a connection from the pool and execute the given {@code function}.

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

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public class PoolImpl extends SqlClientBase implements Pool, Closeable {
5151
private long timerID;
5252
private volatile Function<Context, Future<SqlConnection>> connectionProvider;
5353

54-
private static final String PROPAGATABLE_CONNECTION = "propagatable_connection";
54+
public static final String PROPAGATABLE_CONNECTION = "propagatable_connection";
5555

5656
public PoolImpl(VertxInternal vertx,
5757
Driver driver,
@@ -155,28 +155,9 @@ public Future<SqlConnection> getConnection() {
155155
});
156156
}
157157

158-
public <T> Future<@Nullable T> withTransaction(TransactionPropagation txPropagation,
159-
Function<SqlConnection, Future<@Nullable T>> function) {
160-
if (txPropagation == TransactionPropagation.CONTEXT) {
161-
ContextInternal context = (ContextInternal) Vertx.currentContext();
162-
SqlConnection sqlConnection = context.getLocal(PROPAGATABLE_CONNECTION);
163-
if (sqlConnection == null) {
164-
return startPropagatableConnection(function);
165-
}
166-
return context.succeededFuture(sqlConnection)
167-
.flatMap(conn -> function.apply(conn)
168-
.onFailure(err -> {
169-
if (!(err instanceof TransactionRollbackException)) {
170-
conn.transaction().rollback();
171-
}
172-
}));
173-
}
174-
return withTransaction(function);
175-
}
176-
177-
private <T> Future<@Nullable T> startPropagatableConnection(Function<SqlConnection, Future<@Nullable T>> function) {
158+
public static <T> Future<@Nullable T> startPropagatableConnection(Pool pool, Function<SqlConnection, Future<@Nullable T>> function) {
178159
ContextInternal context = (ContextInternal) Vertx.currentContext();
179-
return getConnection().onComplete(handler -> context.putLocal(PROPAGATABLE_CONNECTION, handler.result()))
160+
return pool.getConnection().onComplete(handler -> context.putLocal(PROPAGATABLE_CONNECTION, handler.result()))
180161
.flatMap(conn -> conn
181162
.begin()
182163
.flatMap(tx -> function

0 commit comments

Comments
 (0)