Skip to content

Commit abc2852

Browse files
SanneDavideD
authored andcommitted
[hibernate#1514] Hardening of current transaction state
1 parent e1eb8c9 commit abc2852

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

hibernate-reactive-core/src/main/java/org/hibernate/reactive/pool/impl/SqlClientConnection.java

+11
Original file line numberDiff line numberDiff line change
@@ -286,8 +286,12 @@ private SqlConnection client() {
286286

287287
@Override
288288
public CompletionStage<Void> beginTransaction() {
289+
if ( transaction != null ) {
290+
throw new IllegalStateException( "Can't begin a new transaction as an active transaction is already associated to this connection" );
291+
}
289292
return connection.begin()
290293
.onSuccess( tx -> LOG.tracef( "Transaction started: %s", tx ) )
294+
.onFailure( v -> LOG.errorf( "Failed to start a transaction: %s", transaction ) )
291295
.toCompletionStage()
292296
.thenAccept( this::setTransaction );
293297
}
@@ -296,22 +300,28 @@ public CompletionStage<Void> beginTransaction() {
296300
public CompletionStage<Void> commitTransaction() {
297301
return transaction.commit()
298302
.onSuccess( v -> LOG.tracef( "Transaction committed: %s", transaction ) )
303+
.onFailure( v -> LOG.errorf( "Failed to commit transaction: %s", transaction ) )
299304
.toCompletionStage()
300305
.whenComplete( this::clearTransaction );
301306
}
302307

303308
@Override
304309
public CompletionStage<Void> rollbackTransaction() {
305310
return transaction.rollback()
311+
.onFailure( v -> LOG.errorf( "Failed to rollback transaction: %s", transaction ) )
306312
.onSuccess( v -> LOG.tracef( "Transaction rolled back: %s", transaction ) )
307313
.toCompletionStage()
308314
.whenComplete( this::clearTransaction );
309315
}
310316

311317
@Override
312318
public CompletionStage<Void> close() {
319+
if ( transaction != null ) {
320+
throw new IllegalStateException( "Connection being closed with a live transaction associated to it" );
321+
}
313322
return connection.close()
314323
.onSuccess( event -> LOG.tracef( "Connection closed: %s", connection ) )
324+
.onFailure( v -> LOG.errorf( "Failed to close a connection: %s", connection ) )
315325
.toCompletionStage();
316326
}
317327

@@ -357,6 +367,7 @@ private void setTransaction(Transaction tx) {
357367
}
358368

359369
private void clearTransaction(Void v, Throwable x) {
370+
LOG.tracef( "Clearing current transaction instance from connection: %s", transaction );
360371
transaction = null;
361372
}
362373

0 commit comments

Comments
 (0)