diff --git a/src/main/java/com/zaxxer/hikari/util/IsolationLevel.java b/src/main/java/com/zaxxer/hikari/util/IsolationLevel.java index 58a68736..72a7621d 100644 --- a/src/main/java/com/zaxxer/hikari/util/IsolationLevel.java +++ b/src/main/java/com/zaxxer/hikari/util/IsolationLevel.java @@ -1,7 +1,23 @@ -package com.zaxxer.hikari.util; +/* + * Copyright (C) 2019 Brett Wooldridge + * + * 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 + * + * http://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. + */ -public enum IsolationLevel { +package com.zaxxer.hikari.util; +public enum IsolationLevel +{ TRANSACTION_NONE(0), TRANSACTION_READ_UNCOMMITTED(1), TRANSACTION_READ_COMMITTED(2), diff --git a/src/main/java/com/zaxxer/hikari/util/UtilityElf.java b/src/main/java/com/zaxxer/hikari/util/UtilityElf.java index 6ec4a18f..62c12264 100644 --- a/src/main/java/com/zaxxer/hikari/util/UtilityElf.java +++ b/src/main/java/com/zaxxer/hikari/util/UtilityElf.java @@ -16,17 +16,12 @@ package com.zaxxer.hikari.util; -import static java.lang.Thread.currentThread; -import static java.util.concurrent.TimeUnit.SECONDS; - import java.lang.reflect.Constructor; import java.util.Locale; -import java.util.Optional; -import java.util.concurrent.BlockingQueue; -import java.util.concurrent.LinkedBlockingQueue; -import java.util.concurrent.RejectedExecutionHandler; -import java.util.concurrent.ThreadFactory; -import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.*; + +import static java.lang.Thread.currentThread; +import static java.util.concurrent.TimeUnit.SECONDS; /** * @@ -34,11 +29,6 @@ import java.util.concurrent.ThreadPoolExecutor; */ public final class UtilityElf { - /** - * A constant for SQL Server's Snapshot isolation level - */ - private static final int SQL_SERVER_SNAPSHOT_ISOLATION_LEVEL = 4096; - /** * * @return null if string is null or empty @@ -164,14 +154,28 @@ public final class UtilityElf * @param transactionIsolationName the name of the transaction isolation level * @return the int value of the isolation level or -1 */ - public static int getTransactionIsolation(final String transactionIsolationName) { + public static int getTransactionIsolation(final String transactionIsolationName) + { if (transactionIsolationName != null) { try { // use the english locale to avoid the infamous turkish locale bug final String upperCaseIsolationLevelName = transactionIsolationName.toUpperCase(Locale.ENGLISH); return IsolationLevel.valueOf(upperCaseIsolationLevelName).getLevelId(); - } catch (Exception e) { - throw new IllegalArgumentException("Invalid transaction isolation value: " + transactionIsolationName); + } catch (IllegalArgumentException e) { + // legacy support for passing an integer version of the isolation level + try { + final int level = Integer.parseInt(transactionIsolationName); + for (IsolationLevel iso : IsolationLevel.values()) { + if (iso.getLevelId() == level) { + return iso.getLevelId(); + } + } + + throw new IllegalArgumentException("Invalid transaction isolation value: " + transactionIsolationName); + } + catch (NumberFormatException nfe) { + throw new IllegalArgumentException("Invalid transaction isolation value: " + transactionIsolationName, nfe); + } } } diff --git a/src/test/java/com/zaxxer/hikari/util/UtilityElfTest.java b/src/test/java/com/zaxxer/hikari/util/UtilityElfTest.java index 11f6bcaa..b8120296 100644 --- a/src/test/java/com/zaxxer/hikari/util/UtilityElfTest.java +++ b/src/test/java/com/zaxxer/hikari/util/UtilityElfTest.java @@ -1,14 +1,30 @@ +/* + * Copyright (C) 2013, 2019 Brett Wooldridge + * + * 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 + * + * http://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 com.zaxxer.hikari.util; import org.junit.Test; import static org.junit.Assert.assertEquals; - -public class UtilityElfTest { - +public class UtilityElfTest +{ @Test - public void shouldReturnValidTransactionIsolationLevel() { + public void shouldReturnValidTransactionIsolationLevel() + { //Act int expectedLevel = UtilityElf.getTransactionIsolation("TRANSACTION_SQL_SERVER_SNAPSHOT_ISOLATION_LEVEL"); @@ -16,10 +32,24 @@ public class UtilityElfTest { assertEquals(expectedLevel, 4096); } - @Test(expected = IllegalArgumentException.class) - public void shouldThrowWhenInvalidTransactionNameGiven() { + public void shouldThrowWhenInvalidTransactionNameGiven() + { //Act UtilityElf.getTransactionIsolation("INVALID_TRANSACTION"); } + + @Test + public void shouldReturnTransationIsolationLevelFromInteger() + { + int expectedLevel = UtilityElf.getTransactionIsolation("4096"); + assertEquals(expectedLevel, 4096); + } + + @Test(expected = IllegalArgumentException.class) + public void shouldThrowWhenInvalidTransactionIntegerGiven() + { + //Act + UtilityElf.getTransactionIsolation("9999"); + } }