Reuse NoTransactionInContextException instances

New NoTransactionInContextException instances are created frequently in
TransactionContextManager#currentContext() when there is no transaction,
we could reuse the same instance instead to reduce the GC pressure.

Closes gh-33601
This commit is contained in:
Sébastien Deleuze 2024-09-27 17:45:02 +02:00
parent bed3025274
commit 8e3846991d
1 changed files with 4 additions and 1 deletions

View File

@ -37,6 +37,9 @@ import org.springframework.transaction.NoTransactionException;
*/ */
public abstract class TransactionContextManager { public abstract class TransactionContextManager {
private static final NoTransactionInContextException NO_TRANSACTION_IN_CONTEXT_EXCEPTION =
new NoTransactionInContextException();
private TransactionContextManager() { private TransactionContextManager() {
} }
@ -60,7 +63,7 @@ public abstract class TransactionContextManager {
return Mono.just(holder.currentContext()); return Mono.just(holder.currentContext());
} }
} }
return Mono.error(new NoTransactionInContextException()); return Mono.error(NO_TRANSACTION_IN_CONTEXT_EXCEPTION);
}); });
} }