Skip to content

Commit

Permalink
Lazy initialization of transaction UUID (with deprecated getter methods)
Browse files Browse the repository at this point in the history
Includes removal of trace logging for individual synchronization steps.

Closes gh-26955
  • Loading branch information
jhoeller committed Jul 9, 2021
1 parent 8680fdb commit a07c786
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 95 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -20,9 +20,6 @@
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.lang.Nullable;

/**
Expand All @@ -48,9 +45,6 @@ public class RuleBasedTransactionAttribute extends DefaultTransactionAttribute i
public static final String PREFIX_COMMIT_RULE = "+";


/** Static for optimal serializability. */
private static final Log logger = LogFactory.getLog(RuleBasedTransactionAttribute.class);

@Nullable
private List<RollbackRuleAttribute> rollbackRules;

Expand Down Expand Up @@ -129,10 +123,6 @@ public List<RollbackRuleAttribute> getRollbackRules() {
*/
@Override
public boolean rollbackOn(Throwable ex) {
if (logger.isTraceEnabled()) {
logger.trace("Applying rules to determine whether transaction should rollback on " + ex);
}

RollbackRuleAttribute winner = null;
int deepest = Integer.MAX_VALUE;

Expand All @@ -146,13 +136,8 @@ public boolean rollbackOn(Throwable ex) {
}
}

if (logger.isTraceEnabled()) {
logger.trace("Winning rollback rule is: " + winner);
}

// User superclass behavior (rollback on unchecked) if no rule matches.
if (winner == null) {
logger.trace("No relevant rollback rule found: applying default rules");
return super.rollbackOn(ex);
}

Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -579,9 +579,6 @@ private Mono<Void> triggerBeforeCommit(TransactionSynchronizationManager synchro
GenericReactiveTransaction status) {

if (status.isNewSynchronization()) {
if (status.isDebug()) {
logger.trace("Triggering beforeCommit synchronization");
}
return TransactionSynchronizationUtils.triggerBeforeCommit(
synchronizationManager.getSynchronizations(), status.isReadOnly());
}
Expand All @@ -597,9 +594,6 @@ private Mono<Void> triggerBeforeCompletion(TransactionSynchronizationManager syn
GenericReactiveTransaction status) {

if (status.isNewSynchronization()) {
if (status.isDebug()) {
logger.trace("Triggering beforeCompletion synchronization");
}
return TransactionSynchronizationUtils.triggerBeforeCompletion(synchronizationManager.getSynchronizations());
}
return Mono.empty();
Expand All @@ -614,9 +608,6 @@ private Mono<Void> triggerAfterCommit(TransactionSynchronizationManager synchron
GenericReactiveTransaction status) {

if (status.isNewSynchronization()) {
if (status.isDebug()) {
logger.trace("Triggering afterCommit synchronization");
}
return TransactionSynchronizationUtils.invokeAfterCommit(synchronizationManager.getSynchronizations());
}
return Mono.empty();
Expand All @@ -635,9 +626,6 @@ private Mono<Void> triggerAfterCompletion(TransactionSynchronizationManager sync
List<TransactionSynchronization> synchronizations = synchronizationManager.getSynchronizations();
synchronizationManager.clearSynchronization();
if (!status.hasTransaction() || status.isNewTransaction()) {
if (status.isDebug()) {
logger.trace("Triggering afterCompletion synchronization");
}
// No transaction or new transaction for the current scope ->
// invoke the afterCompletion callbacks immediately
return invokeAfterCompletion(synchronizationManager, synchronizations, completionStatus);
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -23,6 +23,7 @@

import org.springframework.lang.Nullable;
import org.springframework.util.StringUtils;
import org.springframework.util.function.SingletonSupplier;

/**
* Mutable transaction context that encapsulates transactional synchronizations and
Expand All @@ -40,7 +41,7 @@ public class TransactionContext {

private final @Nullable TransactionContext parent;

private final UUID contextId = UUID.randomUUID();
private final SingletonSupplier<UUID> contextId = SingletonSupplier.of(UUID::randomUUID);

private final Map<Object, Object> resources = new LinkedHashMap<>();

Expand Down Expand Up @@ -70,15 +71,18 @@ public TransactionContext getParent() {
return this.parent;
}

@Deprecated
public String getName() {
if (StringUtils.hasText(this.currentTransactionName)) {
return this.contextId + ": " + this.currentTransactionName;
String name = getCurrentTransactionName();
if (StringUtils.hasText(name)) {
return getContextId() + ": " + name;
}
return this.contextId.toString();
return getContextId().toString();
}

@Deprecated
public UUID getContextId() {
return this.contextId;
return this.contextId.obtain();
}

public Map<Object, Object> getResources() {
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -23,8 +23,6 @@
import java.util.Map;
import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import reactor.core.publisher.Mono;

import org.springframework.core.annotation.AnnotationAwareOrderComparator;
Expand Down Expand Up @@ -71,8 +69,6 @@
*/
public class TransactionSynchronizationManager {

private static final Log logger = LogFactory.getLog(TransactionSynchronizationManager.class);

private final TransactionContext transactionContext;


Expand Down Expand Up @@ -112,12 +108,7 @@ public boolean hasResource(Object key) {
@Nullable
public Object getResource(Object key) {
Object actualKey = TransactionSynchronizationUtils.unwrapResourceIfNecessary(key);
Object value = doGetResource(actualKey);
if (value != null && logger.isTraceEnabled()) {
logger.trace("Retrieved value [" + value + "] for key [" + actualKey + "] bound to context [" +
this.transactionContext.getName() + "]");
}
return value;
return doGetResource(actualKey);
}

/**
Expand All @@ -140,12 +131,8 @@ public void bindResource(Object key, Object value) throws IllegalStateException
Map<Object, Object> map = this.transactionContext.getResources();
Object oldValue = map.put(actualKey, value);
if (oldValue != null) {
throw new IllegalStateException("Already value [" + oldValue + "] for key [" +
actualKey + "] bound to context [" + this.transactionContext.getName() + "]");
}
if (logger.isTraceEnabled()) {
logger.trace("Bound value [" + value + "] for key [" + actualKey + "] to context [" +
this.transactionContext.getName() + "]");
throw new IllegalStateException(
"Already value [" + oldValue + "] for key [" + actualKey + "] bound to context");
}
}

Expand All @@ -159,8 +146,7 @@ public Object unbindResource(Object key) throws IllegalStateException {
Object actualKey = TransactionSynchronizationUtils.unwrapResourceIfNecessary(key);
Object value = doUnbindResource(actualKey);
if (value == null) {
throw new IllegalStateException(
"No value for key [" + actualKey + "] bound to context [" + this.transactionContext.getName() + "]");
throw new IllegalStateException("No value for key [" + actualKey + "] bound to context");
}
return value;
}
Expand All @@ -182,12 +168,7 @@ public Object unbindResourceIfPossible(Object key) {
@Nullable
private Object doUnbindResource(Object actualKey) {
Map<Object, Object> map = this.transactionContext.getResources();
Object value = map.remove(actualKey);
if (value != null && logger.isTraceEnabled()) {
logger.trace("Removed value [" + value + "] for key [" + actualKey + "] from context [" +
this.transactionContext.getName() + "]");
}
return value;
return map.remove(actualKey);
}


Expand All @@ -213,7 +194,6 @@ public void initSynchronization() throws IllegalStateException {
if (isSynchronizationActive()) {
throw new IllegalStateException("Cannot activate transaction synchronization - already active");
}
logger.trace("Initializing transaction synchronization");
this.transactionContext.setSynchronizations(new LinkedHashSet<>());
}

Expand Down Expand Up @@ -273,7 +253,6 @@ public void clearSynchronization() throws IllegalStateException {
if (!isSynchronizationActive()) {
throw new IllegalStateException("Cannot deactivate transaction synchronization - not active");
}
logger.trace("Clearing transaction synchronization");
this.transactionContext.setSynchronizations(null);
}

Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -913,9 +913,6 @@ else if (status.hasTransaction() && isGlobalRollbackOnParticipationFailure()) {
*/
protected final void triggerBeforeCommit(DefaultTransactionStatus status) {
if (status.isNewSynchronization()) {
if (status.isDebug()) {
logger.trace("Triggering beforeCommit synchronization");
}
TransactionSynchronizationUtils.triggerBeforeCommit(status.isReadOnly());
}
}
Expand All @@ -926,9 +923,6 @@ protected final void triggerBeforeCommit(DefaultTransactionStatus status) {
*/
protected final void triggerBeforeCompletion(DefaultTransactionStatus status) {
if (status.isNewSynchronization()) {
if (status.isDebug()) {
logger.trace("Triggering beforeCompletion synchronization");
}
TransactionSynchronizationUtils.triggerBeforeCompletion();
}
}
Expand All @@ -939,9 +933,6 @@ protected final void triggerBeforeCompletion(DefaultTransactionStatus status) {
*/
private void triggerAfterCommit(DefaultTransactionStatus status) {
if (status.isNewSynchronization()) {
if (status.isDebug()) {
logger.trace("Triggering afterCommit synchronization");
}
TransactionSynchronizationUtils.triggerAfterCommit();
}
}
Expand All @@ -956,9 +947,6 @@ private void triggerAfterCompletion(DefaultTransactionStatus status, int complet
List<TransactionSynchronization> synchronizations = TransactionSynchronizationManager.getSynchronizations();
TransactionSynchronizationManager.clearSynchronization();
if (!status.hasTransaction() || status.isNewTransaction()) {
if (status.isDebug()) {
logger.trace("Triggering afterCompletion synchronization");
}
// No transaction or new transaction for the current scope ->
// invoke the afterCompletion callbacks immediately
invokeAfterCompletion(synchronizations, completionStatus);
Expand Down
Expand Up @@ -137,12 +137,7 @@ public static boolean hasResource(Object key) {
@Nullable
public static Object getResource(Object key) {
Object actualKey = TransactionSynchronizationUtils.unwrapResourceIfNecessary(key);
Object value = doGetResource(actualKey);
if (value != null && logger.isTraceEnabled()) {
logger.trace("Retrieved value [" + value + "] for key [" + actualKey + "] bound to thread [" +
Thread.currentThread().getName() + "]");
}
return value;
return doGetResource(actualKey);
}

/**
Expand Down Expand Up @@ -189,12 +184,8 @@ public static void bindResource(Object key, Object value) throws IllegalStateExc
oldValue = null;
}
if (oldValue != null) {
throw new IllegalStateException("Already value [" + oldValue + "] for key [" +
actualKey + "] bound to thread [" + Thread.currentThread().getName() + "]");
}
if (logger.isTraceEnabled()) {
logger.trace("Bound value [" + value + "] for key [" + actualKey + "] to thread [" +
Thread.currentThread().getName() + "]");
throw new IllegalStateException(
"Already value [" + oldValue + "] for key [" + actualKey + "] bound to thread");
}
}

Expand All @@ -209,8 +200,7 @@ public static Object unbindResource(Object key) throws IllegalStateException {
Object actualKey = TransactionSynchronizationUtils.unwrapResourceIfNecessary(key);
Object value = doUnbindResource(actualKey);
if (value == null) {
throw new IllegalStateException(
"No value for key [" + actualKey + "] bound to thread [" + Thread.currentThread().getName() + "]");
throw new IllegalStateException("No value for key [" + actualKey + "] bound to thread");
}
return value;
}
Expand Down Expand Up @@ -244,10 +234,6 @@ private static Object doUnbindResource(Object actualKey) {
if (value instanceof ResourceHolder && ((ResourceHolder) value).isVoid()) {
value = null;
}
if (value != null && logger.isTraceEnabled()) {
logger.trace("Removed value [" + value + "] for key [" + actualKey + "] from thread [" +
Thread.currentThread().getName() + "]");
}
return value;
}

Expand All @@ -274,7 +260,6 @@ public static void initSynchronization() throws IllegalStateException {
if (isSynchronizationActive()) {
throw new IllegalStateException("Cannot activate transaction synchronization - already active");
}
logger.trace("Initializing transaction synchronization");
synchronizations.set(new LinkedHashSet<>());
}

Expand Down Expand Up @@ -334,7 +319,6 @@ public static void clearSynchronization() throws IllegalStateException {
if (!isSynchronizationActive()) {
throw new IllegalStateException("Cannot deactivate transaction synchronization - not active");
}
logger.trace("Clearing transaction synchronization");
synchronizations.remove();
}

Expand Down

0 comments on commit a07c786

Please sign in to comment.