If not specified, the target DataSource's default will be used.
* Note that a transaction-specific isolation value will always override
* any isolation setting specified at the DataSource level.
@@ -77,10 +88,10 @@ public class IsolationLevelDataSourceAdapter extends UserCredentialsDataSourceAd
* @see #setIsolationLevel
*/
public final void setIsolationLevelName(String constantName) throws IllegalArgumentException {
- if (!constantName.startsWith(DefaultTransactionDefinition.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 = constants.get(constantName);
+ Assert.notNull(isolationLevel, "Only isolation constants allowed");
+ setIsolationLevel(isolationLevel);
}
/**
@@ -103,9 +114,7 @@ public class IsolationLevelDataSourceAdapter extends UserCredentialsDataSourceAd
* @see org.springframework.transaction.support.TransactionSynchronizationManager#getCurrentTransactionIsolationLevel()
*/
public void setIsolationLevel(int isolationLevel) {
- if (!constants.getValues(DefaultTransactionDefinition.PREFIX_ISOLATION).contains(isolationLevel)) {
- throw new IllegalArgumentException("Only values of isolation constants allowed");
- }
+ Assert.isTrue(constants.containsValue(isolationLevel), "Only values of isolation constants allowed");
this.isolationLevel = (isolationLevel != TransactionDefinition.ISOLATION_DEFAULT ? isolationLevel : null);
}
diff --git a/spring-jdbc/src/test/java/org/springframework/jdbc/datasource/IsolationLevelDataSourceAdapterTests.java b/spring-jdbc/src/test/java/org/springframework/jdbc/datasource/IsolationLevelDataSourceAdapterTests.java
new file mode 100644
index 0000000000..20fac2c532
--- /dev/null
+++ b/spring-jdbc/src/test/java/org/springframework/jdbc/datasource/IsolationLevelDataSourceAdapterTests.java
@@ -0,0 +1,95 @@
+/*
+ * 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.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.springframework.jdbc.datasource;
+
+import java.lang.reflect.Field;
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.Set;
+import java.util.stream.Stream;
+
+import org.junit.jupiter.api.Test;
+
+import org.springframework.transaction.TransactionDefinition;
+import org.springframework.util.ReflectionUtils;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
+import static org.springframework.transaction.TransactionDefinition.ISOLATION_DEFAULT;
+import static org.springframework.transaction.TransactionDefinition.ISOLATION_READ_COMMITTED;
+
+/**
+ * Tests for {@link IsolationLevelDataSourceAdapter}.
+ *
+ * @author Sam Brannen
+ * @since 6.1
+ */
+class IsolationLevelDataSourceAdapterTests {
+
+ private final IsolationLevelDataSourceAdapter adapter = new IsolationLevelDataSourceAdapter();
+
+
+ @Test
+ void setIsolationLevelNameToUnsupportedValues() {
+ assertThatIllegalArgumentException().isThrownBy(() -> adapter.setIsolationLevelName(null));
+ assertThatIllegalArgumentException().isThrownBy(() -> adapter.setIsolationLevelName(" "));
+ assertThatIllegalArgumentException().isThrownBy(() -> adapter.setIsolationLevelName("bogus"));
+ }
+
+ /**
+ * Verify that the internal 'constants' map is properly configured for all
+ * ISOLATION_ constants defined in {@link TransactionDefinition}.
+ */
+ @Test
+ void setIsolationLevelNameToAllSupportedValues() {
+ Set