Skip to content

Commit a07c786

Browse files
committed
Lazy initialization of transaction UUID (with deprecated getter methods)
Includes removal of trace logging for individual synchronization steps. Closes gh-26955
1 parent 8680fdb commit a07c786

6 files changed

+23
-95
lines changed

spring-tx/src/main/java/org/springframework/transaction/interceptor/RuleBasedTransactionAttribute.java

+1-16
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,9 +20,6 @@
2020
import java.util.ArrayList;
2121
import java.util.List;
2222

23-
import org.apache.commons.logging.Log;
24-
import org.apache.commons.logging.LogFactory;
25-
2623
import org.springframework.lang.Nullable;
2724

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

5047

51-
/** Static for optimal serializability. */
52-
private static final Log logger = LogFactory.getLog(RuleBasedTransactionAttribute.class);
53-
5448
@Nullable
5549
private List<RollbackRuleAttribute> rollbackRules;
5650

@@ -129,10 +123,6 @@ public List<RollbackRuleAttribute> getRollbackRules() {
129123
*/
130124
@Override
131125
public boolean rollbackOn(Throwable ex) {
132-
if (logger.isTraceEnabled()) {
133-
logger.trace("Applying rules to determine whether transaction should rollback on " + ex);
134-
}
135-
136126
RollbackRuleAttribute winner = null;
137127
int deepest = Integer.MAX_VALUE;
138128

@@ -146,13 +136,8 @@ public boolean rollbackOn(Throwable ex) {
146136
}
147137
}
148138

149-
if (logger.isTraceEnabled()) {
150-
logger.trace("Winning rollback rule is: " + winner);
151-
}
152-
153139
// User superclass behavior (rollback on unchecked) if no rule matches.
154140
if (winner == null) {
155-
logger.trace("No relevant rollback rule found: applying default rules");
156141
return super.rollbackOn(ex);
157142
}
158143

spring-tx/src/main/java/org/springframework/transaction/reactive/AbstractReactiveTransactionManager.java

+1-13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -579,9 +579,6 @@ private Mono<Void> triggerBeforeCommit(TransactionSynchronizationManager synchro
579579
GenericReactiveTransaction status) {
580580

581581
if (status.isNewSynchronization()) {
582-
if (status.isDebug()) {
583-
logger.trace("Triggering beforeCommit synchronization");
584-
}
585582
return TransactionSynchronizationUtils.triggerBeforeCommit(
586583
synchronizationManager.getSynchronizations(), status.isReadOnly());
587584
}
@@ -597,9 +594,6 @@ private Mono<Void> triggerBeforeCompletion(TransactionSynchronizationManager syn
597594
GenericReactiveTransaction status) {
598595

599596
if (status.isNewSynchronization()) {
600-
if (status.isDebug()) {
601-
logger.trace("Triggering beforeCompletion synchronization");
602-
}
603597
return TransactionSynchronizationUtils.triggerBeforeCompletion(synchronizationManager.getSynchronizations());
604598
}
605599
return Mono.empty();
@@ -614,9 +608,6 @@ private Mono<Void> triggerAfterCommit(TransactionSynchronizationManager synchron
614608
GenericReactiveTransaction status) {
615609

616610
if (status.isNewSynchronization()) {
617-
if (status.isDebug()) {
618-
logger.trace("Triggering afterCommit synchronization");
619-
}
620611
return TransactionSynchronizationUtils.invokeAfterCommit(synchronizationManager.getSynchronizations());
621612
}
622613
return Mono.empty();
@@ -635,9 +626,6 @@ private Mono<Void> triggerAfterCompletion(TransactionSynchronizationManager sync
635626
List<TransactionSynchronization> synchronizations = synchronizationManager.getSynchronizations();
636627
synchronizationManager.clearSynchronization();
637628
if (!status.hasTransaction() || status.isNewTransaction()) {
638-
if (status.isDebug()) {
639-
logger.trace("Triggering afterCompletion synchronization");
640-
}
641629
// No transaction or new transaction for the current scope ->
642630
// invoke the afterCompletion callbacks immediately
643631
return invokeAfterCompletion(synchronizationManager, synchronizations, completionStatus);

spring-tx/src/main/java/org/springframework/transaction/reactive/TransactionContext.java

+10-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,6 +23,7 @@
2323

2424
import org.springframework.lang.Nullable;
2525
import org.springframework.util.StringUtils;
26+
import org.springframework.util.function.SingletonSupplier;
2627

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

4142
private final @Nullable TransactionContext parent;
4243

43-
private final UUID contextId = UUID.randomUUID();
44+
private final SingletonSupplier<UUID> contextId = SingletonSupplier.of(UUID::randomUUID);
4445

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

@@ -70,15 +71,18 @@ public TransactionContext getParent() {
7071
return this.parent;
7172
}
7273

74+
@Deprecated
7375
public String getName() {
74-
if (StringUtils.hasText(this.currentTransactionName)) {
75-
return this.contextId + ": " + this.currentTransactionName;
76+
String name = getCurrentTransactionName();
77+
if (StringUtils.hasText(name)) {
78+
return getContextId() + ": " + name;
7679
}
77-
return this.contextId.toString();
80+
return getContextId().toString();
7881
}
7982

83+
@Deprecated
8084
public UUID getContextId() {
81-
return this.contextId;
85+
return this.contextId.obtain();
8286
}
8387

8488
public Map<Object, Object> getResources() {

spring-tx/src/main/java/org/springframework/transaction/reactive/TransactionSynchronizationManager.java

+6-27
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,8 +23,6 @@
2323
import java.util.Map;
2424
import java.util.Set;
2525

26-
import org.apache.commons.logging.Log;
27-
import org.apache.commons.logging.LogFactory;
2826
import reactor.core.publisher.Mono;
2927

3028
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
@@ -71,8 +69,6 @@
7169
*/
7270
public class TransactionSynchronizationManager {
7371

74-
private static final Log logger = LogFactory.getLog(TransactionSynchronizationManager.class);
75-
7672
private final TransactionContext transactionContext;
7773

7874

@@ -112,12 +108,7 @@ public boolean hasResource(Object key) {
112108
@Nullable
113109
public Object getResource(Object key) {
114110
Object actualKey = TransactionSynchronizationUtils.unwrapResourceIfNecessary(key);
115-
Object value = doGetResource(actualKey);
116-
if (value != null && logger.isTraceEnabled()) {
117-
logger.trace("Retrieved value [" + value + "] for key [" + actualKey + "] bound to context [" +
118-
this.transactionContext.getName() + "]");
119-
}
120-
return value;
111+
return doGetResource(actualKey);
121112
}
122113

123114
/**
@@ -140,12 +131,8 @@ public void bindResource(Object key, Object value) throws IllegalStateException
140131
Map<Object, Object> map = this.transactionContext.getResources();
141132
Object oldValue = map.put(actualKey, value);
142133
if (oldValue != null) {
143-
throw new IllegalStateException("Already value [" + oldValue + "] for key [" +
144-
actualKey + "] bound to context [" + this.transactionContext.getName() + "]");
145-
}
146-
if (logger.isTraceEnabled()) {
147-
logger.trace("Bound value [" + value + "] for key [" + actualKey + "] to context [" +
148-
this.transactionContext.getName() + "]");
134+
throw new IllegalStateException(
135+
"Already value [" + oldValue + "] for key [" + actualKey + "] bound to context");
149136
}
150137
}
151138

@@ -159,8 +146,7 @@ public Object unbindResource(Object key) throws IllegalStateException {
159146
Object actualKey = TransactionSynchronizationUtils.unwrapResourceIfNecessary(key);
160147
Object value = doUnbindResource(actualKey);
161148
if (value == null) {
162-
throw new IllegalStateException(
163-
"No value for key [" + actualKey + "] bound to context [" + this.transactionContext.getName() + "]");
149+
throw new IllegalStateException("No value for key [" + actualKey + "] bound to context");
164150
}
165151
return value;
166152
}
@@ -182,12 +168,7 @@ public Object unbindResourceIfPossible(Object key) {
182168
@Nullable
183169
private Object doUnbindResource(Object actualKey) {
184170
Map<Object, Object> map = this.transactionContext.getResources();
185-
Object value = map.remove(actualKey);
186-
if (value != null && logger.isTraceEnabled()) {
187-
logger.trace("Removed value [" + value + "] for key [" + actualKey + "] from context [" +
188-
this.transactionContext.getName() + "]");
189-
}
190-
return value;
171+
return map.remove(actualKey);
191172
}
192173

193174

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

@@ -273,7 +253,6 @@ public void clearSynchronization() throws IllegalStateException {
273253
if (!isSynchronizationActive()) {
274254
throw new IllegalStateException("Cannot deactivate transaction synchronization - not active");
275255
}
276-
logger.trace("Clearing transaction synchronization");
277256
this.transactionContext.setSynchronizations(null);
278257
}
279258

spring-tx/src/main/java/org/springframework/transaction/support/AbstractPlatformTransactionManager.java

+1-13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -913,9 +913,6 @@ else if (status.hasTransaction() && isGlobalRollbackOnParticipationFailure()) {
913913
*/
914914
protected final void triggerBeforeCommit(DefaultTransactionStatus status) {
915915
if (status.isNewSynchronization()) {
916-
if (status.isDebug()) {
917-
logger.trace("Triggering beforeCommit synchronization");
918-
}
919916
TransactionSynchronizationUtils.triggerBeforeCommit(status.isReadOnly());
920917
}
921918
}
@@ -926,9 +923,6 @@ protected final void triggerBeforeCommit(DefaultTransactionStatus status) {
926923
*/
927924
protected final void triggerBeforeCompletion(DefaultTransactionStatus status) {
928925
if (status.isNewSynchronization()) {
929-
if (status.isDebug()) {
930-
logger.trace("Triggering beforeCompletion synchronization");
931-
}
932926
TransactionSynchronizationUtils.triggerBeforeCompletion();
933927
}
934928
}
@@ -939,9 +933,6 @@ protected final void triggerBeforeCompletion(DefaultTransactionStatus status) {
939933
*/
940934
private void triggerAfterCommit(DefaultTransactionStatus status) {
941935
if (status.isNewSynchronization()) {
942-
if (status.isDebug()) {
943-
logger.trace("Triggering afterCommit synchronization");
944-
}
945936
TransactionSynchronizationUtils.triggerAfterCommit();
946937
}
947938
}
@@ -956,9 +947,6 @@ private void triggerAfterCompletion(DefaultTransactionStatus status, int complet
956947
List<TransactionSynchronization> synchronizations = TransactionSynchronizationManager.getSynchronizations();
957948
TransactionSynchronizationManager.clearSynchronization();
958949
if (!status.hasTransaction() || status.isNewTransaction()) {
959-
if (status.isDebug()) {
960-
logger.trace("Triggering afterCompletion synchronization");
961-
}
962950
// No transaction or new transaction for the current scope ->
963951
// invoke the afterCompletion callbacks immediately
964952
invokeAfterCompletion(synchronizations, completionStatus);

spring-tx/src/main/java/org/springframework/transaction/support/TransactionSynchronizationManager.java

+4-20
Original file line numberDiff line numberDiff line change
@@ -137,12 +137,7 @@ public static boolean hasResource(Object key) {
137137
@Nullable
138138
public static Object getResource(Object key) {
139139
Object actualKey = TransactionSynchronizationUtils.unwrapResourceIfNecessary(key);
140-
Object value = doGetResource(actualKey);
141-
if (value != null && logger.isTraceEnabled()) {
142-
logger.trace("Retrieved value [" + value + "] for key [" + actualKey + "] bound to thread [" +
143-
Thread.currentThread().getName() + "]");
144-
}
145-
return value;
140+
return doGetResource(actualKey);
146141
}
147142

148143
/**
@@ -189,12 +184,8 @@ public static void bindResource(Object key, Object value) throws IllegalStateExc
189184
oldValue = null;
190185
}
191186
if (oldValue != null) {
192-
throw new IllegalStateException("Already value [" + oldValue + "] for key [" +
193-
actualKey + "] bound to thread [" + Thread.currentThread().getName() + "]");
194-
}
195-
if (logger.isTraceEnabled()) {
196-
logger.trace("Bound value [" + value + "] for key [" + actualKey + "] to thread [" +
197-
Thread.currentThread().getName() + "]");
187+
throw new IllegalStateException(
188+
"Already value [" + oldValue + "] for key [" + actualKey + "] bound to thread");
198189
}
199190
}
200191

@@ -209,8 +200,7 @@ public static Object unbindResource(Object key) throws IllegalStateException {
209200
Object actualKey = TransactionSynchronizationUtils.unwrapResourceIfNecessary(key);
210201
Object value = doUnbindResource(actualKey);
211202
if (value == null) {
212-
throw new IllegalStateException(
213-
"No value for key [" + actualKey + "] bound to thread [" + Thread.currentThread().getName() + "]");
203+
throw new IllegalStateException("No value for key [" + actualKey + "] bound to thread");
214204
}
215205
return value;
216206
}
@@ -244,10 +234,6 @@ private static Object doUnbindResource(Object actualKey) {
244234
if (value instanceof ResourceHolder && ((ResourceHolder) value).isVoid()) {
245235
value = null;
246236
}
247-
if (value != null && logger.isTraceEnabled()) {
248-
logger.trace("Removed value [" + value + "] for key [" + actualKey + "] from thread [" +
249-
Thread.currentThread().getName() + "]");
250-
}
251237
return value;
252238
}
253239

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

@@ -334,7 +319,6 @@ public static void clearSynchronization() throws IllegalStateException {
334319
if (!isSynchronizationActive()) {
335320
throw new IllegalStateException("Cannot deactivate transaction synchronization - not active");
336321
}
337-
logger.trace("Clearing transaction synchronization");
338322
synchronizations.remove();
339323
}
340324

0 commit comments

Comments
 (0)