Skip to content

Commit

Permalink
Stop using Constants utility in DefaultTransactionDefinition
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrannen committed Jul 19, 2023
1 parent c110644 commit d0076f5
Show file tree
Hide file tree
Showing 4 changed files with 174 additions and 53 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2023 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 @@ -473,11 +473,10 @@ private TransactionStatus handleExistingTransaction(
if (definition.getIsolationLevel() != TransactionDefinition.ISOLATION_DEFAULT) {
Integer currentIsolationLevel = TransactionSynchronizationManager.getCurrentTransactionIsolationLevel();
if (currentIsolationLevel == null || currentIsolationLevel != definition.getIsolationLevel()) {
Constants isoConstants = DefaultTransactionDefinition.constants;
throw new IllegalTransactionStateException("Participating transaction with definition [" +
definition + "] specifies isolation level which is incompatible with existing transaction: " +
(currentIsolationLevel != null ?
isoConstants.toCode(currentIsolationLevel, DefaultTransactionDefinition.PREFIX_ISOLATION) :
DefaultTransactionDefinition.getIsolationLevelName(currentIsolationLevel) :
"(unknown)"));
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2023 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 @@ -17,10 +17,11 @@
package org.springframework.transaction.support;

import java.io.Serializable;
import java.util.Map;

import org.springframework.core.Constants;
import org.springframework.lang.Nullable;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.util.Assert;

/**
* Default implementation of the {@link TransactionDefinition} interface,
Expand All @@ -31,6 +32,7 @@
* {@link org.springframework.transaction.interceptor.DefaultTransactionAttribute}.
*
* @author Juergen Hoeller
* @author Sam Brannen
* @since 08.05.2003
*/
@SuppressWarnings("serial")
Expand All @@ -49,8 +51,31 @@ public class DefaultTransactionDefinition implements TransactionDefinition, Seri
public static final String READ_ONLY_MARKER = "readOnly";


/** Constants instance for TransactionDefinition. */
static final Constants constants = new Constants(TransactionDefinition.class);
/**
* Map of constant names to constant values for the propagation constants
* defined in {@link TransactionDefinition}.
*/
static final Map<String, Integer> propagationConstants = Map.of(
"PROPAGATION_REQUIRED", TransactionDefinition.PROPAGATION_REQUIRED,
"PROPAGATION_SUPPORTS", TransactionDefinition.PROPAGATION_SUPPORTS,
"PROPAGATION_MANDATORY", TransactionDefinition.PROPAGATION_MANDATORY,
"PROPAGATION_REQUIRES_NEW", TransactionDefinition.PROPAGATION_REQUIRES_NEW,
"PROPAGATION_NOT_SUPPORTED", TransactionDefinition.PROPAGATION_NOT_SUPPORTED,
"PROPAGATION_NEVER", TransactionDefinition.PROPAGATION_NEVER,
"PROPAGATION_NESTED", TransactionDefinition.PROPAGATION_NESTED
);

/**
* Map of constant names to constant values for the isolation constants
* defined in {@link TransactionDefinition}.
*/
static final Map<String, Integer> isolationConstants = Map.of(
"ISOLATION_DEFAULT", TransactionDefinition.ISOLATION_DEFAULT,
"ISOLATION_READ_UNCOMMITTED", TransactionDefinition.ISOLATION_READ_UNCOMMITTED,
"ISOLATION_READ_COMMITTED", TransactionDefinition.ISOLATION_READ_COMMITTED,
"ISOLATION_REPEATABLE_READ", TransactionDefinition.ISOLATION_REPEATABLE_READ,
"ISOLATION_SERIALIZABLE", TransactionDefinition.ISOLATION_SERIALIZABLE
);

private int propagationBehavior = PROPAGATION_REQUIRED;

Expand Down Expand Up @@ -108,18 +133,18 @@ public DefaultTransactionDefinition(int propagationBehavior) {

/**
* Set the propagation behavior by the name of the corresponding constant in
* TransactionDefinition, e.g. "PROPAGATION_REQUIRED".
* {@link TransactionDefinition} &mdash; for example, {@code "PROPAGATION_REQUIRED"}.
* @param constantName name of the constant
* @throws IllegalArgumentException if the supplied value is not resolvable
* to one of the {@code PROPAGATION_} constants or is {@code null}
* @see #setPropagationBehavior
* @see #PROPAGATION_REQUIRED
*/
public final void setPropagationBehaviorName(String constantName) throws IllegalArgumentException {
if (!constantName.startsWith(PREFIX_PROPAGATION)) {
throw new IllegalArgumentException("Only propagation constants allowed");
}
setPropagationBehavior(constants.asNumber(constantName).intValue());
Assert.hasText(constantName, "'constantName' must not be null or blank");
Integer propagationBehavior = propagationConstants.get(constantName);
Assert.notNull(propagationBehavior, "Only propagation behavior constants allowed");
this.propagationBehavior = propagationBehavior;
}

/**
Expand All @@ -138,9 +163,8 @@ public final void setPropagationBehaviorName(String constantName) throws Illegal
* @see #PROPAGATION_REQUIRED
*/
public final void setPropagationBehavior(int propagationBehavior) {
if (!constants.getValues(PREFIX_PROPAGATION).contains(propagationBehavior)) {
throw new IllegalArgumentException("Only values of propagation constants allowed");
}
Assert.isTrue(propagationConstants.containsValue(propagationBehavior),
"Only values of propagation constants allowed");
this.propagationBehavior = propagationBehavior;
}

Expand All @@ -151,18 +175,18 @@ public final int getPropagationBehavior() {

/**
* Set the isolation level by the name of the corresponding constant in
* TransactionDefinition, e.g. "ISOLATION_DEFAULT".
* {@link TransactionDefinition} &mdash; for example, {@code "ISOLATION_DEFAULT"}.
* @param constantName name of the constant
* @throws IllegalArgumentException if the supplied value is not resolvable
* to one of the {@code ISOLATION_} constants or is {@code null}
* @see #setIsolationLevel
* @see #ISOLATION_DEFAULT
*/
public final void setIsolationLevelName(String constantName) throws IllegalArgumentException {
if (!constantName.startsWith(PREFIX_ISOLATION)) {
throw new IllegalArgumentException("Only isolation constants allowed");
}
setIsolationLevel(constants.asNumber(constantName).intValue());
Assert.hasText(constantName, "'constantName' must not be null or blank");
Integer isolationLevel = isolationConstants.get(constantName);
Assert.notNull(isolationLevel, "Only isolation constants allowed");
this.isolationLevel = isolationLevel;
}

/**
Expand All @@ -181,9 +205,8 @@ public final void setIsolationLevelName(String constantName) throws IllegalArgum
* @see #ISOLATION_DEFAULT
*/
public final void setIsolationLevel(int isolationLevel) {
if (!constants.getValues(PREFIX_ISOLATION).contains(isolationLevel)) {
throw new IllegalArgumentException("Only values of isolation constants allowed");
}
Assert.isTrue(isolationConstants.containsValue(isolationLevel),
"Only values of isolation constants allowed");
this.isolationLevel = isolationLevel;
}

Expand Down Expand Up @@ -294,9 +317,9 @@ public String toString() {
*/
protected final StringBuilder getDefinitionDescription() {
StringBuilder result = new StringBuilder();
result.append(constants.toCode(this.propagationBehavior, PREFIX_PROPAGATION));
result.append(getPropagationBehaviorName(this.propagationBehavior));
result.append(',');
result.append(constants.toCode(this.isolationLevel, PREFIX_ISOLATION));
result.append(getIsolationLevelName(this.isolationLevel));
if (this.timeout != TIMEOUT_DEFAULT) {
result.append(',');
result.append(PREFIX_TIMEOUT).append(this.timeout);
Expand All @@ -308,4 +331,22 @@ protected final StringBuilder getDefinitionDescription() {
return result;
}

private static String getPropagationBehaviorName(int propagationBehavior) {
for (Map.Entry<String, Integer> entry : propagationConstants.entrySet()) {
if (entry.getValue().equals(propagationBehavior)) {
return entry.getKey();
}
}
throw new IllegalArgumentException("Unsupported propagation behavior: " + propagationBehavior);
}

static String getIsolationLevelName(int isolationLevel) {
for (Map.Entry<String, Integer> entry : isolationConstants.entrySet()) {
if (entry.getValue().equals(isolationLevel)) {
return entry.getKey();
}
}
throw new IllegalArgumentException("Unsupported isolation level: " + isolationLevel);
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2023 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 @@ -14,10 +14,10 @@
* limitations under the License.
*/

package org.springframework.transaction;
package org.springframework.transaction.support;

import org.springframework.transaction.support.AbstractPlatformTransactionManager;
import org.springframework.transaction.support.DefaultTransactionStatus;
import org.springframework.transaction.CannotCreateTransactionException;
import org.springframework.transaction.TransactionDefinition;

/**
* @author Juergen Hoeller
Expand Down
Loading

0 comments on commit d0076f5

Please sign in to comment.