diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/LazyConnectionDataSourceProxy.java b/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/LazyConnectionDataSourceProxy.java index 3244f01e1940..ed352bf38857 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/LazyConnectionDataSourceProxy.java +++ b/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/LazyConnectionDataSourceProxy.java @@ -163,6 +163,8 @@ public void setDefaultTransactionIsolationName(String constantName) { * @see java.sql.Connection#setTransactionIsolation */ public void setDefaultTransactionIsolation(int defaultTransactionIsolation) { + Assert.isTrue(constants.containsValue(defaultTransactionIsolation), + "Only values of transaction isolation constants allowed"); this.defaultTransactionIsolation = defaultTransactionIsolation; } diff --git a/spring-jdbc/src/test/java/org/springframework/jdbc/datasource/LazyConnectionDataSourceProxyTests.java b/spring-jdbc/src/test/java/org/springframework/jdbc/datasource/LazyConnectionDataSourceProxyTests.java index 7159752118bf..289b1b8e6e73 100644 --- a/spring-jdbc/src/test/java/org/springframework/jdbc/datasource/LazyConnectionDataSourceProxyTests.java +++ b/spring-jdbc/src/test/java/org/springframework/jdbc/datasource/LazyConnectionDataSourceProxyTests.java @@ -29,6 +29,9 @@ import static java.sql.Connection.TRANSACTION_NONE; import static java.sql.Connection.TRANSACTION_READ_COMMITTED; +import static java.sql.Connection.TRANSACTION_READ_UNCOMMITTED; +import static java.sql.Connection.TRANSACTION_REPEATABLE_READ; +import static java.sql.Connection.TRANSACTION_SERIALIZABLE; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; @@ -75,16 +78,20 @@ void setDefaultTransactionIsolationNameToAllSupportedValues() { @Test void setDefaultTransactionIsolation() { - // unsupported values are not checked: - proxy.setDefaultTransactionIsolation(-999); - assertThat(proxy.defaultTransactionIsolation()).isEqualTo(-999); - - // TRANSACTION_NONE should not be supported, but we currently do not check that. - proxy.setDefaultTransactionIsolation(TRANSACTION_NONE); - assertThat(proxy.defaultTransactionIsolation()).isEqualTo(TRANSACTION_NONE); + assertThatIllegalArgumentException().isThrownBy(() -> proxy.setDefaultTransactionIsolation(-999)); + assertThatIllegalArgumentException().isThrownBy(() -> proxy.setDefaultTransactionIsolation(TRANSACTION_NONE)); proxy.setDefaultTransactionIsolation(TRANSACTION_READ_COMMITTED); assertThat(proxy.defaultTransactionIsolation()).isEqualTo(TRANSACTION_READ_COMMITTED); + + proxy.setDefaultTransactionIsolation(TRANSACTION_READ_UNCOMMITTED); + assertThat(proxy.defaultTransactionIsolation()).isEqualTo(TRANSACTION_READ_UNCOMMITTED); + + proxy.setDefaultTransactionIsolation(TRANSACTION_REPEATABLE_READ); + assertThat(proxy.defaultTransactionIsolation()).isEqualTo(TRANSACTION_REPEATABLE_READ); + + proxy.setDefaultTransactionIsolation(TRANSACTION_SERIALIZABLE); + assertThat(proxy.defaultTransactionIsolation()).isEqualTo(TRANSACTION_SERIALIZABLE); }