Lazy initialization of transaction UUID (with deprecated getter methods)

Includes removal of trace logging for individual synchronization steps.

Closes gh-26955
This commit is contained in:
Juergen Hoeller 2021-07-09 15:07:30 +02:00
parent 8680fdb8bc
commit a07c7865a5
6 changed files with 23 additions and 95 deletions

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -20,9 +20,6 @@ import java.io.Serializable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.lang.Nullable; import org.springframework.lang.Nullable;
/** /**
@ -48,9 +45,6 @@ public class RuleBasedTransactionAttribute extends DefaultTransactionAttribute i
public static final String PREFIX_COMMIT_RULE = "+"; public static final String PREFIX_COMMIT_RULE = "+";
/** Static for optimal serializability. */
private static final Log logger = LogFactory.getLog(RuleBasedTransactionAttribute.class);
@Nullable @Nullable
private List<RollbackRuleAttribute> rollbackRules; private List<RollbackRuleAttribute> rollbackRules;
@ -129,10 +123,6 @@ public class RuleBasedTransactionAttribute extends DefaultTransactionAttribute i
*/ */
@Override @Override
public boolean rollbackOn(Throwable ex) { public boolean rollbackOn(Throwable ex) {
if (logger.isTraceEnabled()) {
logger.trace("Applying rules to determine whether transaction should rollback on " + ex);
}
RollbackRuleAttribute winner = null; RollbackRuleAttribute winner = null;
int deepest = Integer.MAX_VALUE; int deepest = Integer.MAX_VALUE;
@ -146,13 +136,8 @@ public class RuleBasedTransactionAttribute extends DefaultTransactionAttribute i
} }
} }
if (logger.isTraceEnabled()) {
logger.trace("Winning rollback rule is: " + winner);
}
// User superclass behavior (rollback on unchecked) if no rule matches. // User superclass behavior (rollback on unchecked) if no rule matches.
if (winner == null) { if (winner == null) {
logger.trace("No relevant rollback rule found: applying default rules");
return super.rollbackOn(ex); return super.rollbackOn(ex);
} }

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -579,9 +579,6 @@ public abstract class AbstractReactiveTransactionManager implements ReactiveTran
GenericReactiveTransaction status) { GenericReactiveTransaction status) {
if (status.isNewSynchronization()) { if (status.isNewSynchronization()) {
if (status.isDebug()) {
logger.trace("Triggering beforeCommit synchronization");
}
return TransactionSynchronizationUtils.triggerBeforeCommit( return TransactionSynchronizationUtils.triggerBeforeCommit(
synchronizationManager.getSynchronizations(), status.isReadOnly()); synchronizationManager.getSynchronizations(), status.isReadOnly());
} }
@ -597,9 +594,6 @@ public abstract class AbstractReactiveTransactionManager implements ReactiveTran
GenericReactiveTransaction status) { GenericReactiveTransaction status) {
if (status.isNewSynchronization()) { if (status.isNewSynchronization()) {
if (status.isDebug()) {
logger.trace("Triggering beforeCompletion synchronization");
}
return TransactionSynchronizationUtils.triggerBeforeCompletion(synchronizationManager.getSynchronizations()); return TransactionSynchronizationUtils.triggerBeforeCompletion(synchronizationManager.getSynchronizations());
} }
return Mono.empty(); return Mono.empty();
@ -614,9 +608,6 @@ public abstract class AbstractReactiveTransactionManager implements ReactiveTran
GenericReactiveTransaction status) { GenericReactiveTransaction status) {
if (status.isNewSynchronization()) { if (status.isNewSynchronization()) {
if (status.isDebug()) {
logger.trace("Triggering afterCommit synchronization");
}
return TransactionSynchronizationUtils.invokeAfterCommit(synchronizationManager.getSynchronizations()); return TransactionSynchronizationUtils.invokeAfterCommit(synchronizationManager.getSynchronizations());
} }
return Mono.empty(); return Mono.empty();
@ -635,9 +626,6 @@ public abstract class AbstractReactiveTransactionManager implements ReactiveTran
List<TransactionSynchronization> synchronizations = synchronizationManager.getSynchronizations(); List<TransactionSynchronization> synchronizations = synchronizationManager.getSynchronizations();
synchronizationManager.clearSynchronization(); synchronizationManager.clearSynchronization();
if (!status.hasTransaction() || status.isNewTransaction()) { if (!status.hasTransaction() || status.isNewTransaction()) {
if (status.isDebug()) {
logger.trace("Triggering afterCompletion synchronization");
}
// No transaction or new transaction for the current scope -> // No transaction or new transaction for the current scope ->
// invoke the afterCompletion callbacks immediately // invoke the afterCompletion callbacks immediately
return invokeAfterCompletion(synchronizationManager, synchronizations, completionStatus); return invokeAfterCompletion(synchronizationManager, synchronizations, completionStatus);

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -23,6 +23,7 @@ import java.util.UUID;
import org.springframework.lang.Nullable; import org.springframework.lang.Nullable;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import org.springframework.util.function.SingletonSupplier;
/** /**
* Mutable transaction context that encapsulates transactional synchronizations and * Mutable transaction context that encapsulates transactional synchronizations and
@ -40,7 +41,7 @@ public class TransactionContext {
private final @Nullable TransactionContext parent; 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<>(); private final Map<Object, Object> resources = new LinkedHashMap<>();
@ -70,15 +71,18 @@ public class TransactionContext {
return this.parent; return this.parent;
} }
@Deprecated
public String getName() { public String getName() {
if (StringUtils.hasText(this.currentTransactionName)) { String name = getCurrentTransactionName();
return this.contextId + ": " + this.currentTransactionName; if (StringUtils.hasText(name)) {
return getContextId() + ": " + name;
} }
return this.contextId.toString(); return getContextId().toString();
} }
@Deprecated
public UUID getContextId() { public UUID getContextId() {
return this.contextId; return this.contextId.obtain();
} }
public Map<Object, Object> getResources() { public Map<Object, Object> getResources() {

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -23,8 +23,6 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
import org.springframework.core.annotation.AnnotationAwareOrderComparator; import org.springframework.core.annotation.AnnotationAwareOrderComparator;
@ -71,8 +69,6 @@ import org.springframework.util.Assert;
*/ */
public class TransactionSynchronizationManager { public class TransactionSynchronizationManager {
private static final Log logger = LogFactory.getLog(TransactionSynchronizationManager.class);
private final TransactionContext transactionContext; private final TransactionContext transactionContext;
@ -112,12 +108,7 @@ public class TransactionSynchronizationManager {
@Nullable @Nullable
public Object getResource(Object key) { public Object getResource(Object key) {
Object actualKey = TransactionSynchronizationUtils.unwrapResourceIfNecessary(key); Object actualKey = TransactionSynchronizationUtils.unwrapResourceIfNecessary(key);
Object value = doGetResource(actualKey); return doGetResource(actualKey);
if (value != null && logger.isTraceEnabled()) {
logger.trace("Retrieved value [" + value + "] for key [" + actualKey + "] bound to context [" +
this.transactionContext.getName() + "]");
}
return value;
} }
/** /**
@ -140,12 +131,8 @@ public class TransactionSynchronizationManager {
Map<Object, Object> map = this.transactionContext.getResources(); Map<Object, Object> map = this.transactionContext.getResources();
Object oldValue = map.put(actualKey, value); Object oldValue = map.put(actualKey, value);
if (oldValue != null) { if (oldValue != null) {
throw new IllegalStateException("Already value [" + oldValue + "] for key [" + throw new IllegalStateException(
actualKey + "] bound to context [" + this.transactionContext.getName() + "]"); "Already value [" + oldValue + "] for key [" + actualKey + "] bound to context");
}
if (logger.isTraceEnabled()) {
logger.trace("Bound value [" + value + "] for key [" + actualKey + "] to context [" +
this.transactionContext.getName() + "]");
} }
} }
@ -159,8 +146,7 @@ public class TransactionSynchronizationManager {
Object actualKey = TransactionSynchronizationUtils.unwrapResourceIfNecessary(key); Object actualKey = TransactionSynchronizationUtils.unwrapResourceIfNecessary(key);
Object value = doUnbindResource(actualKey); Object value = doUnbindResource(actualKey);
if (value == null) { if (value == null) {
throw new IllegalStateException( throw new IllegalStateException("No value for key [" + actualKey + "] bound to context");
"No value for key [" + actualKey + "] bound to context [" + this.transactionContext.getName() + "]");
} }
return value; return value;
} }
@ -182,12 +168,7 @@ public class TransactionSynchronizationManager {
@Nullable @Nullable
private Object doUnbindResource(Object actualKey) { private Object doUnbindResource(Object actualKey) {
Map<Object, Object> map = this.transactionContext.getResources(); Map<Object, Object> map = this.transactionContext.getResources();
Object value = map.remove(actualKey); return map.remove(actualKey);
if (value != null && logger.isTraceEnabled()) {
logger.trace("Removed value [" + value + "] for key [" + actualKey + "] from context [" +
this.transactionContext.getName() + "]");
}
return value;
} }
@ -213,7 +194,6 @@ public class TransactionSynchronizationManager {
if (isSynchronizationActive()) { if (isSynchronizationActive()) {
throw new IllegalStateException("Cannot activate transaction synchronization - already active"); throw new IllegalStateException("Cannot activate transaction synchronization - already active");
} }
logger.trace("Initializing transaction synchronization");
this.transactionContext.setSynchronizations(new LinkedHashSet<>()); this.transactionContext.setSynchronizations(new LinkedHashSet<>());
} }
@ -273,7 +253,6 @@ public class TransactionSynchronizationManager {
if (!isSynchronizationActive()) { if (!isSynchronizationActive()) {
throw new IllegalStateException("Cannot deactivate transaction synchronization - not active"); throw new IllegalStateException("Cannot deactivate transaction synchronization - not active");
} }
logger.trace("Clearing transaction synchronization");
this.transactionContext.setSynchronizations(null); this.transactionContext.setSynchronizations(null);
} }

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -913,9 +913,6 @@ public abstract class AbstractPlatformTransactionManager implements PlatformTran
*/ */
protected final void triggerBeforeCommit(DefaultTransactionStatus status) { protected final void triggerBeforeCommit(DefaultTransactionStatus status) {
if (status.isNewSynchronization()) { if (status.isNewSynchronization()) {
if (status.isDebug()) {
logger.trace("Triggering beforeCommit synchronization");
}
TransactionSynchronizationUtils.triggerBeforeCommit(status.isReadOnly()); TransactionSynchronizationUtils.triggerBeforeCommit(status.isReadOnly());
} }
} }
@ -926,9 +923,6 @@ public abstract class AbstractPlatformTransactionManager implements PlatformTran
*/ */
protected final void triggerBeforeCompletion(DefaultTransactionStatus status) { protected final void triggerBeforeCompletion(DefaultTransactionStatus status) {
if (status.isNewSynchronization()) { if (status.isNewSynchronization()) {
if (status.isDebug()) {
logger.trace("Triggering beforeCompletion synchronization");
}
TransactionSynchronizationUtils.triggerBeforeCompletion(); TransactionSynchronizationUtils.triggerBeforeCompletion();
} }
} }
@ -939,9 +933,6 @@ public abstract class AbstractPlatformTransactionManager implements PlatformTran
*/ */
private void triggerAfterCommit(DefaultTransactionStatus status) { private void triggerAfterCommit(DefaultTransactionStatus status) {
if (status.isNewSynchronization()) { if (status.isNewSynchronization()) {
if (status.isDebug()) {
logger.trace("Triggering afterCommit synchronization");
}
TransactionSynchronizationUtils.triggerAfterCommit(); TransactionSynchronizationUtils.triggerAfterCommit();
} }
} }
@ -956,9 +947,6 @@ public abstract class AbstractPlatformTransactionManager implements PlatformTran
List<TransactionSynchronization> synchronizations = TransactionSynchronizationManager.getSynchronizations(); List<TransactionSynchronization> synchronizations = TransactionSynchronizationManager.getSynchronizations();
TransactionSynchronizationManager.clearSynchronization(); TransactionSynchronizationManager.clearSynchronization();
if (!status.hasTransaction() || status.isNewTransaction()) { if (!status.hasTransaction() || status.isNewTransaction()) {
if (status.isDebug()) {
logger.trace("Triggering afterCompletion synchronization");
}
// No transaction or new transaction for the current scope -> // No transaction or new transaction for the current scope ->
// invoke the afterCompletion callbacks immediately // invoke the afterCompletion callbacks immediately
invokeAfterCompletion(synchronizations, completionStatus); invokeAfterCompletion(synchronizations, completionStatus);

View File

@ -137,12 +137,7 @@ public abstract class TransactionSynchronizationManager {
@Nullable @Nullable
public static Object getResource(Object key) { public static Object getResource(Object key) {
Object actualKey = TransactionSynchronizationUtils.unwrapResourceIfNecessary(key); Object actualKey = TransactionSynchronizationUtils.unwrapResourceIfNecessary(key);
Object value = doGetResource(actualKey); return doGetResource(actualKey);
if (value != null && logger.isTraceEnabled()) {
logger.trace("Retrieved value [" + value + "] for key [" + actualKey + "] bound to thread [" +
Thread.currentThread().getName() + "]");
}
return value;
} }
/** /**
@ -189,12 +184,8 @@ public abstract class TransactionSynchronizationManager {
oldValue = null; oldValue = null;
} }
if (oldValue != null) { if (oldValue != null) {
throw new IllegalStateException("Already value [" + oldValue + "] for key [" + throw new IllegalStateException(
actualKey + "] bound to thread [" + Thread.currentThread().getName() + "]"); "Already value [" + oldValue + "] for key [" + actualKey + "] bound to thread");
}
if (logger.isTraceEnabled()) {
logger.trace("Bound value [" + value + "] for key [" + actualKey + "] to thread [" +
Thread.currentThread().getName() + "]");
} }
} }
@ -209,8 +200,7 @@ public abstract class TransactionSynchronizationManager {
Object actualKey = TransactionSynchronizationUtils.unwrapResourceIfNecessary(key); Object actualKey = TransactionSynchronizationUtils.unwrapResourceIfNecessary(key);
Object value = doUnbindResource(actualKey); Object value = doUnbindResource(actualKey);
if (value == null) { if (value == null) {
throw new IllegalStateException( throw new IllegalStateException("No value for key [" + actualKey + "] bound to thread");
"No value for key [" + actualKey + "] bound to thread [" + Thread.currentThread().getName() + "]");
} }
return value; return value;
} }
@ -244,10 +234,6 @@ public abstract class TransactionSynchronizationManager {
if (value instanceof ResourceHolder && ((ResourceHolder) value).isVoid()) { if (value instanceof ResourceHolder && ((ResourceHolder) value).isVoid()) {
value = null; value = null;
} }
if (value != null && logger.isTraceEnabled()) {
logger.trace("Removed value [" + value + "] for key [" + actualKey + "] from thread [" +
Thread.currentThread().getName() + "]");
}
return value; return value;
} }
@ -274,7 +260,6 @@ public abstract class TransactionSynchronizationManager {
if (isSynchronizationActive()) { if (isSynchronizationActive()) {
throw new IllegalStateException("Cannot activate transaction synchronization - already active"); throw new IllegalStateException("Cannot activate transaction synchronization - already active");
} }
logger.trace("Initializing transaction synchronization");
synchronizations.set(new LinkedHashSet<>()); synchronizations.set(new LinkedHashSet<>());
} }
@ -334,7 +319,6 @@ public abstract class TransactionSynchronizationManager {
if (!isSynchronizationActive()) { if (!isSynchronizationActive()) {
throw new IllegalStateException("Cannot deactivate transaction synchronization - not active"); throw new IllegalStateException("Cannot deactivate transaction synchronization - not active");
} }
logger.trace("Clearing transaction synchronization");
synchronizations.remove(); synchronizations.remove();
} }