This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-dbcp.git
The following commit(s) were added to refs/heads/master by this push: new f57c9fef Avoid object creation when invoking isDisconnectionSqlException. (#422) f57c9fef is described below commit f57c9fef0d55e6faaac89c86786cf1319c984b80 Author: Johno Crawford <jo...@hellface.com> AuthorDate: Mon Aug 19 19:28:28 2024 +0200 Avoid object creation when invoking isDisconnectionSqlException. (#422) * Avoid object creation when invoking isDisconnectionSqlException. * Update since tag. --- .../org/apache/commons/dbcp2/PoolableConnection.java | 2 +- src/main/java/org/apache/commons/dbcp2/Utils.java | 11 +++++++++++ src/test/java/org/apache/commons/dbcp2/TestUtils.java | 16 ++++++++++++++-- 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/apache/commons/dbcp2/PoolableConnection.java b/src/main/java/org/apache/commons/dbcp2/PoolableConnection.java index 612560a3..01ce941d 100644 --- a/src/main/java/org/apache/commons/dbcp2/PoolableConnection.java +++ b/src/main/java/org/apache/commons/dbcp2/PoolableConnection.java @@ -305,7 +305,7 @@ public class PoolableConnection extends DelegatingConnection<Connection> impleme return false; } fatalException = disconnectionSqlCodes == null - ? sqlState.startsWith(Utils.DISCONNECTION_SQL_CODE_PREFIX) || Utils.getDisconnectionSqlCodes().contains(sqlState) + ? sqlState.startsWith(Utils.DISCONNECTION_SQL_CODE_PREFIX) || Utils.isDisconnectionSqlCode(sqlState) : disconnectionSqlCodes.contains(sqlState); } return fatalException; diff --git a/src/main/java/org/apache/commons/dbcp2/Utils.java b/src/main/java/org/apache/commons/dbcp2/Utils.java index 6f236c52..e9324adb 100644 --- a/src/main/java/org/apache/commons/dbcp2/Utils.java +++ b/src/main/java/org/apache/commons/dbcp2/Utils.java @@ -216,6 +216,17 @@ public final class Utils { return new HashSet<>(DISCONNECTION_SQL_CODES); } + /** + * Checks if the given SQL state corresponds to a fatal connection error. + * + * @param sqlState the SQL state to check. + * @return true if the SQL state is a fatal connection error, false otherwise. + * @since 2.13.0 + */ + static boolean isDisconnectionSqlCode(String sqlState) { + return DISCONNECTION_SQL_CODES.contains(sqlState); + } + /** * Gets the correct i18n message for the given key. * diff --git a/src/test/java/org/apache/commons/dbcp2/TestUtils.java b/src/test/java/org/apache/commons/dbcp2/TestUtils.java index 79d06eb0..395d25b2 100644 --- a/src/test/java/org/apache/commons/dbcp2/TestUtils.java +++ b/src/test/java/org/apache/commons/dbcp2/TestUtils.java @@ -19,15 +19,17 @@ package org.apache.commons.dbcp2; import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import org.junit.jupiter.api.Test; import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.HashSet; -import org.junit.jupiter.api.Test; - public class TestUtils { public static PStmtKey getPStmtKey(final PoolablePreparedStatement<PStmtKey> poolablePreparedStatement) { @@ -87,4 +89,14 @@ public class TestUtils { public void testClassLoads() { Utils.closeQuietly((AutoCloseable) null); } + + @Test + public void testIsDisconnectionSqlCode() { + assertTrue(Utils.isDisconnectionSqlCode("57P01"), "57P01 should be recognised as a disconnection SQL code."); + assertTrue(Utils.isDisconnectionSqlCode("01002"), "01002 should be recognised as a disconnection SQL code."); + assertTrue(Utils.isDisconnectionSqlCode("JZ0C0"), "JZ0C0 should be recognised as a disconnection SQL code."); + + assertFalse(Utils.isDisconnectionSqlCode("INVALID"), "INVALID should not be recognised as a disconnection SQL code."); + assertFalse(Utils.isDisconnectionSqlCode("00000"), "00000 should not be recognised as a disconnection SQL code."); + } }