@@ -288,8 +288,12 @@ private SqlClient client() {
288288
289289 @ Override
290290 public CompletionStage <Void > beginTransaction () {
291+ if ( transaction != null ) {
292+ throw new IllegalStateException ("Earlier transaction was found!" );
293+ }
291294 return connection .begin ()
292295 .onSuccess ( tx -> LOG .tracef ( "Transaction started: %s" , tx ) )
296+ .onFailure ( v -> LOG .errorf ( "Failed to start a transaction: %s" , transaction ) )
293297 .toCompletionStage ()
294298 .thenAccept ( tx -> transaction = tx );
295299 }
@@ -298,22 +302,28 @@ public CompletionStage<Void> beginTransaction() {
298302 public CompletionStage <Void > commitTransaction () {
299303 return transaction .commit ()
300304 .onSuccess ( v -> LOG .tracef ( "Transaction committed: %s" , transaction ) )
305+ .onFailure ( v -> LOG .errorf ( "Failed to commit transaction: %s" , transaction ) )
301306 .toCompletionStage ()
302- .whenComplete ( ( v , x ) -> transaction = null );
307+ .whenComplete ( this :: afterTransactionEnd );
303308 }
304309
305310 @ Override
306311 public CompletionStage <Void > rollbackTransaction () {
307312 return transaction .rollback ()
313+ .onFailure ( v -> LOG .errorf ( "Failed to rollback transaction: %s" , transaction ) )
308314 .onSuccess ( v -> LOG .tracef ( "Transaction rolled back: %s" , transaction ) )
309315 .toCompletionStage ()
310- .whenComplete ( ( v , x ) -> transaction = null );
316+ .whenComplete ( this :: afterTransactionEnd );
311317 }
312318
313319 @ Override
314320 public CompletionStage <Void > close () {
321+ if ( transaction != null ) {
322+ throw new IllegalStateException ( "Connection being closed with a live transaction associated to it" );
323+ }
315324 return connection .close ()
316325 .onSuccess ( event -> LOG .tracef ( "Connection closed: %s" , connection ) )
326+ .onFailure ( v -> LOG .errorf ( "Failed to close a connection: %s" , connection ) )
317327 .toCompletionStage ();
318328 }
319329
@@ -333,6 +343,11 @@ private static <T> T getLastInsertedId(RowSet<Row> rows, Class<T> idClass, Strin
333343 return null ;
334344 }
335345
346+ private void afterTransactionEnd (Void v , Throwable x ) {
347+ LOG .tracef ( "Clearing current transaction instance from connection: %s" , transaction );
348+ transaction = null ;
349+ }
350+
336351 private static class RowSetResult implements Result {
337352 private final RowSet <Row > rowset ;
338353 private final RowIterator <Row > it ;
0 commit comments