@@ -288,8 +288,12 @@ private SqlClient client() {
288
288
289
289
@ Override
290
290
public CompletionStage <Void > beginTransaction () {
291
+ if ( transaction != null ) {
292
+ throw new IllegalStateException ( "Can't begin a new transaction as an active transaction is already associated to this connection" );
293
+ }
291
294
return connection .begin ()
292
295
.onSuccess ( tx -> LOG .tracef ( "Transaction started: %s" , tx ) )
296
+ .onFailure ( v -> LOG .errorf ( "Failed to start a transaction: %s" , transaction ) )
293
297
.toCompletionStage ()
294
298
.thenAccept ( tx -> transaction = tx );
295
299
}
@@ -298,22 +302,28 @@ public CompletionStage<Void> beginTransaction() {
298
302
public CompletionStage <Void > commitTransaction () {
299
303
return transaction .commit ()
300
304
.onSuccess ( v -> LOG .tracef ( "Transaction committed: %s" , transaction ) )
305
+ .onFailure ( v -> LOG .errorf ( "Failed to commit transaction: %s" , transaction ) )
301
306
.toCompletionStage ()
302
- .whenComplete ( ( v , x ) -> transaction = null );
307
+ .whenComplete ( this :: afterTransactionEnd );
303
308
}
304
309
305
310
@ Override
306
311
public CompletionStage <Void > rollbackTransaction () {
307
312
return transaction .rollback ()
313
+ .onFailure ( v -> LOG .errorf ( "Failed to rollback transaction: %s" , transaction ) )
308
314
.onSuccess ( v -> LOG .tracef ( "Transaction rolled back: %s" , transaction ) )
309
315
.toCompletionStage ()
310
- .whenComplete ( ( v , x ) -> transaction = null );
316
+ .whenComplete ( this :: afterTransactionEnd );
311
317
}
312
318
313
319
@ Override
314
320
public CompletionStage <Void > close () {
321
+ if ( transaction != null ) {
322
+ throw new IllegalStateException ( "Connection being closed with a live transaction associated to it" );
323
+ }
315
324
return connection .close ()
316
325
.onSuccess ( event -> LOG .tracef ( "Connection closed: %s" , connection ) )
326
+ .onFailure ( v -> LOG .errorf ( "Failed to close a connection: %s" , connection ) )
317
327
.toCompletionStage ();
318
328
}
319
329
@@ -333,6 +343,11 @@ private static <T> T getLastInsertedId(RowSet<Row> rows, Class<T> idClass, Strin
333
343
return null ;
334
344
}
335
345
346
+ private void afterTransactionEnd (Void v , Throwable x ) {
347
+ LOG .tracef ( "Clearing current transaction instance from connection: %s" , transaction );
348
+ transaction = null ;
349
+ }
350
+
336
351
private static class RowSetResult implements Result {
337
352
private final RowSet <Row > rowset ;
338
353
private final RowIterator <Row > it ;
0 commit comments