diff --git a/hibernate-reactive-core/src/main/java/org/hibernate/reactive/pool/impl/SqlClientConnection.java b/hibernate-reactive-core/src/main/java/org/hibernate/reactive/pool/impl/SqlClientConnection.java index a02f971e8..c36ca24f9 100644 --- a/hibernate-reactive-core/src/main/java/org/hibernate/reactive/pool/impl/SqlClientConnection.java +++ b/hibernate-reactive-core/src/main/java/org/hibernate/reactive/pool/impl/SqlClientConnection.java @@ -288,8 +288,12 @@ private SqlClient client() { @Override public CompletionStage beginTransaction() { + if ( transaction != null ) { + throw new IllegalStateException( "Can't begin a new transaction as an active transaction is already associated to this connection" ); + } return connection.begin() .onSuccess( tx -> LOG.tracef( "Transaction started: %s", tx ) ) + .onFailure( v -> LOG.errorf( "Failed to start a transaction: %s", transaction ) ) .toCompletionStage() .thenAccept( tx -> transaction = tx ); } @@ -298,22 +302,28 @@ public CompletionStage beginTransaction() { public CompletionStage commitTransaction() { return transaction.commit() .onSuccess( v -> LOG.tracef( "Transaction committed: %s", transaction ) ) + .onFailure( v -> LOG.errorf( "Failed to commit transaction: %s", transaction ) ) .toCompletionStage() - .whenComplete( (v, x) -> transaction = null ); + .whenComplete( this::afterTransactionEnd ); } @Override public CompletionStage rollbackTransaction() { return transaction.rollback() + .onFailure( v -> LOG.errorf( "Failed to rollback transaction: %s", transaction ) ) .onSuccess( v -> LOG.tracef( "Transaction rolled back: %s", transaction ) ) .toCompletionStage() - .whenComplete( (v, x) -> transaction = null ); + .whenComplete( this::afterTransactionEnd ); } @Override public CompletionStage close() { + if ( transaction != null ) { + throw new IllegalStateException( "Connection being closed with a live transaction associated to it" ); + } return connection.close() .onSuccess( event -> LOG.tracef( "Connection closed: %s", connection ) ) + .onFailure( v -> LOG.errorf( "Failed to close a connection: %s", connection ) ) .toCompletionStage(); } @@ -333,6 +343,11 @@ private static T getLastInsertedId(RowSet rows, Class idClass, Strin return null; } + private void afterTransactionEnd(Void v, Throwable x) { + LOG.tracef( "Clearing current transaction instance from connection: %s", transaction ); + transaction = null; + } + private static class RowSetResult implements Result { private final RowSet rowset; private final RowIterator it;