Skip to content

Commit db554f0

Browse files
Use current context, store connection locally
1 parent 2ea0ee8 commit db554f0

File tree

2 files changed

+19
-11
lines changed

2 files changed

+19
-11
lines changed

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,8 @@ public Future<SqlConnection> getConnection() {
159159
}
160160

161161
public <T> Future<@Nullable T> withPropagatedTransaction(Function<SqlConnection, Future<@Nullable T>> function) {
162-
SqlConnection sqlConnection = context().get(PROPAGATABLE_CONNECTION);
162+
Context context = Vertx.currentContext();
163+
SqlConnection sqlConnection = context.getLocal(PROPAGATABLE_CONNECTION);
163164
if (sqlConnection == null) {
164165
return initializePropagatedConnectionAndTransaction(function);
165166
}
@@ -173,7 +174,8 @@ public Future<SqlConnection> getConnection() {
173174
}
174175

175176
private <T> Future<@Nullable T> initializePropagatedConnectionAndTransaction(Function<SqlConnection, Future<@Nullable T>> function) {
176-
return getConnection().onComplete(handler -> context().put(PROPAGATABLE_CONNECTION, handler.result()))
177+
Context context = Vertx.currentContext();
178+
return getConnection().onComplete(handler -> context.putLocal(PROPAGATABLE_CONNECTION, handler.result()))
177179
.flatMap(conn -> conn
178180
.begin()
179181
.flatMap(tx -> function
@@ -191,7 +193,7 @@ public Future<SqlConnection> getConnection() {
191193
.compose(v -> Future.failedFuture(err), failure -> Future.failedFuture(err));
192194
}
193195
}))
194-
.onComplete(ar -> conn.close(v -> context().remove(PROPAGATABLE_CONNECTION))));
196+
.onComplete(ar -> conn.close(v -> context.removeLocal(PROPAGATABLE_CONNECTION))));
195197
}
196198

197199
@Override

vertx-sql-client/src/test/java/io/vertx/sqlclient/tck/TransactionTestBase.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,7 @@ public void testWithTransactionImplicitRollback(TestContext ctx) {
313313
public void testWithPropagatedConnectionTransactionCommit(TestContext ctx) {
314314
Async async = ctx.async();
315315
Pool pool = createPool();
316+
vertx.runOnContext(handler -> {
316317
pool.withPropagatedTransaction(c ->
317318
pool.withPropagatedTransaction(conn -> conn.query("INSERT INTO mutable (id, val) VALUES (1, 'hello-1')").execute().mapEmpty()).flatMap(v ->
318319
pool.withPropagatedTransaction(conn -> conn.query("INSERT INTO mutable (id, val) VALUES (2, 'hello-2')").execute().mapEmpty())).flatMap(v2 ->
@@ -321,22 +322,27 @@ public void testWithPropagatedConnectionTransactionCommit(TestContext ctx) {
321322
.query("SELECT id, val FROM mutable")
322323
.execute(ctx.asyncAssertSuccess(rows -> {
323324
ctx.assertEquals(3, rows.size());
325+
ctx.assertNull(Vertx.currentContext().getLocal("propagatable_connection"));
324326
async.complete();
325327
}))));
328+
});
326329
}
327330

328331
@Test
329332
public void testWithPropagatedConnectionTransactionRollback(TestContext ctx) {
330333
Async async = ctx.async();
331334
Pool pool = createPool();
332335
Throwable failure = new Throwable();
333-
pool.withPropagatedTransaction(c ->
334-
pool.withPropagatedTransaction(conn -> conn.query("INSERT INTO mutable (id, val) VALUES (1, 'hello-1')").execute().mapEmpty().flatMap(v -> Future.failedFuture(failure)))
335-
).onComplete(ctx.asyncAssertFailure(v -> pool
336-
.query("SELECT id, val FROM mutable")
337-
.execute(ctx.asyncAssertSuccess(rows -> {
338-
ctx.assertEquals(0, rows.size());
339-
async.complete();
340-
}))));
336+
vertx.runOnContext(handler -> {
337+
pool.withPropagatedTransaction(c ->
338+
pool.withPropagatedTransaction(conn -> conn.query("INSERT INTO mutable (id, val) VALUES (1, 'hello-1')").execute().mapEmpty().flatMap(v -> Future.failedFuture(failure)))
339+
).onComplete(ctx.asyncAssertFailure(v -> pool
340+
.query("SELECT id, val FROM mutable")
341+
.execute(ctx.asyncAssertSuccess(rows -> {
342+
ctx.assertEquals(0, rows.size());
343+
ctx.assertNull(Vertx.currentContext().getLocal("propagatable_connection"));
344+
async.complete();
345+
}))));
346+
});
341347
}
342348
}

0 commit comments

Comments
 (0)