This is an automated email from the ASF dual-hosted git repository. garydgregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-dbcp.git
commit 11ffd070c9e7e5240ec43623de922ae18d800f57 Author: Gary Gregory <[email protected]> AuthorDate: Mon Nov 23 07:12:25 2026 -0500 Fix negative fractional JNDI durations on Java 8 Work around Duration.parse() losing the sign of negative-zero seconds components when restoring DriverAdapterCPDS JNDI References. Normalize the seconds component before parsing and compensate for the adjustment, respecting the duration's leading sign. Preserve existing ISO-8601 output and extreme duration values. Add regression coverage for mixed signs, minute/hour boundaries, malformed input, and legacy-property precedence. Update release notes. --- src/changes/changes.xml | 2 +- .../dbcp2/cpdsadapter/DriverAdapterCPDS.java | 17 +++++- .../TestDriverAdapterCPDSReference.java | 69 +++++++++++++++++++++- 3 files changed, 83 insertions(+), 5 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 2a344633..5575f742 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -82,7 +82,7 @@ The <action> type attribute can be add,update,fix,remove. <action type="fix" dev="ggregory" due-to="Gary Gregory">Undeprecate InstanceKeyDataSource.setLoginTimeout(int) since it implements javax.sql.CommonDataSource.setLoginTimeout(int).</action> <action type="fix" dev="ggregory" due-to="Gary Gregory">Honor positive sub-second default query timeouts and direct connection validation timeouts, and cap oversized durations without losing JDBC rejection of negative values.</action> <action type="fix" dev="ggregory" due-to="Gary Gregory">Restore the original query timeout when returning pooled prepared and callable statements, preventing timeout settings from leaking between borrowers.</action> - <action type="fix" dev="ggregory" due-to="Gary Gregory">Preserve DriverAdapterCPDS eviction durations through JNDI References, preferring Duration properties and accepting long legacy millisecond values.</action> + <action type="fix" dev="ggregory" due-to="Gary Gregory">Preserve DriverAdapterCPDS eviction durations through JNDI References, including negative fractional seconds on Java 8, preferring Duration properties and accepting long legacy millisecond values.</action> <action type="fix" dev="ggregory" due-to="Gary Gregory">Apply SharedPoolDataSource and PerUserPoolDataSource login timeouts to every physical connection attempt, restoring the underlying provider's timeout afterwards. Bound waits to configure the provider, and preserve live timeout settings across deserialization.</action> <!-- ADD --> <action type="add" dev="ggregory" due-to="Gary Gregory">Add DriverAdapterCPDS.exportPasswordAsRefAddr to optionally omit the password address from JNDI References. Password export remains enabled by default for compatibility; the setting survives JNDI lookup and Java serialization.</action> diff --git a/src/main/java/org/apache/commons/dbcp2/cpdsadapter/DriverAdapterCPDS.java b/src/main/java/org/apache/commons/dbcp2/cpdsadapter/DriverAdapterCPDS.java index ae99533b..4d04a618 100644 --- a/src/main/java/org/apache/commons/dbcp2/cpdsadapter/DriverAdapterCPDS.java +++ b/src/main/java/org/apache/commons/dbcp2/cpdsadapter/DriverAdapterCPDS.java @@ -27,6 +27,8 @@ import java.time.Duration; import java.util.Hashtable; import java.util.Properties; import java.util.logging.Logger; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import javax.naming.Context; import javax.naming.Name; @@ -81,6 +83,8 @@ import org.apache.commons.pool2.impl.GenericKeyedObjectPoolConfig; */ public class DriverAdapterCPDS implements ConnectionPoolDataSource, Referenceable, Serializable, ObjectFactory { + private static final Pattern NEGATIVE_ZERO_SECONDS_PATTERN = Pattern.compile("-0+([.,][0-9]{0,9})S$", Pattern.CASE_INSENSITIVE); + private static final String KEY_MIN_EVICTABLE_IDLE_DURATION = "minEvictableIdleDuration"; private static final String KEY_DURATION_BETWEEN_EVICTION_RUNS = "durationBetweenEvictionRuns"; @@ -250,7 +254,7 @@ public class DriverAdapterCPDS implements ConnectionPoolDataSource, Referenceabl private Duration getDuration(final Reference reference, final String durationKey, final String millisKey) { final RefAddr duration = reference.get(durationKey); if (isNotEmpty(duration)) { - return Duration.parse(getStringContent(duration)); + return parseDuration(getStringContent(duration)); } final RefAddr millis = reference.get(millisKey); return isNotEmpty(millis) ? Duration.ofMillis(Long.parseLong(getStringContent(millis))) : null; @@ -627,6 +631,17 @@ public class DriverAdapterCPDS implements ConnectionPoolDataSource, Referenceabl return poolPreparedStatements; } + private Duration parseDuration(final String text) { + final Matcher matcher = NEGATIVE_ZERO_SECONDS_PATTERN.matcher(text); + if (matcher.find()) { + // Java 8 loses the sign of a "-0" seconds component. Parse "-1" instead, then compensate + // after applying any leading sign to the whole duration. This also handles mixed-sign components. + final Duration duration = Duration.parse(matcher.replaceFirst("-1$1S")); + return text.startsWith("-") ? duration.minusSeconds(1) : duration.plusSeconds(1); + } + return Duration.parse(text); + } + /** * Sets the value of the accessToUnderlyingConnectionAllowed property. It controls if the PoolGuard allows access to * the underlying connection. (Default: false) diff --git a/src/test/java/org/apache/commons/dbcp2/cpdsadapter/TestDriverAdapterCPDSReference.java b/src/test/java/org/apache/commons/dbcp2/cpdsadapter/TestDriverAdapterCPDSReference.java index 8963f727..d7c30596 100644 --- a/src/test/java/org/apache/commons/dbcp2/cpdsadapter/TestDriverAdapterCPDSReference.java +++ b/src/test/java/org/apache/commons/dbcp2/cpdsadapter/TestDriverAdapterCPDSReference.java @@ -34,10 +34,12 @@ import java.io.ObjectOutputStream; import java.sql.Connection; import java.sql.SQLException; import java.time.Duration; +import java.time.format.DateTimeParseException; import java.util.Base64; import java.util.Hashtable; import java.util.Properties; import java.util.stream.IntStream; +import java.util.stream.LongStream; import java.util.stream.Stream; import javax.naming.Context; @@ -363,13 +365,48 @@ class TestDriverAdapterCPDSReference { assertEquals(export, restored.getReference().get("password") != null); assertCanConnect(restored); } + static Stream<Arguments> durationReferences() { - return Stream.of(Duration.ZERO, Duration.ofNanos(1), Duration.ofNanos(-1), Duration.ofMillis(1_901), - Duration.ofDays(30), Duration.ofMillis(Long.MAX_VALUE), Duration.ofSeconds(Long.MAX_VALUE, 999_999_999), - Duration.ofSeconds(Long.MIN_VALUE)) + return Stream.concat(Stream.of(Duration.ZERO, Duration.ofNanos(1), Duration.ofMillis(1_901), + Duration.ofDays(30), Duration.ofMillis(Long.MIN_VALUE), Duration.ofMillis(Long.MAX_VALUE), + Duration.ofSeconds(Long.MAX_VALUE, 999_999_999)), + LongStream.of(-1, -2, -59, -60, -61, -3599, -3600, -3601, Long.MIN_VALUE) + .boxed().flatMap(seconds -> IntStream.of(0, 1, 500_000_000, 999_999_999) + .mapToObj(nanos -> Duration.ofSeconds(seconds, nanos)))) .flatMap(duration -> Stream.of(false, true).map(modernOnly -> Arguments.of(duration, modernOnly))); } + static Stream<Arguments> durationReferenceStrings() { + return Stream.of( + Arguments.of("PT-0.000000001S", Duration.ofNanos(-1)), + Arguments.of("PT-0.1S", Duration.ofMillis(-100)), + Arguments.of("PT-00.500000000S", Duration.ofMillis(-500)), + Arguments.of("PT-000.999999999S", Duration.ofNanos(-999_999_999)), + Arguments.of("+PT-0,1S", Duration.ofMillis(-100)), + Arguments.of("pt-0,000000001s", Duration.ofNanos(-1)), + Arguments.of("-PT-0.000000001S", Duration.ofNanos(1)), + Arguments.of("-pt-00,1s", Duration.ofMillis(100)), + Arguments.of("PT-0.S", Duration.ZERO), + Arguments.of("PT-0,S", Duration.ZERO), + Arguments.of("PT-0.000000000S", Duration.ZERO), + Arguments.of("PT-0S", Duration.ZERO), + Arguments.of("PT0.000000001S", Duration.ofNanos(1)), + Arguments.of("-PT0.000000001S", Duration.ofNanos(-1)), + Arguments.of("PT-1M-0.000000001S", Duration.ofSeconds(-60, -1)), + Arguments.of("PT1M-0.5S", Duration.ofMillis(59_500)), + Arguments.of("-PT1M-0.5S", Duration.ofMillis(-59_500)), + Arguments.of("PT-1H-0.1S", Duration.ofMillis(-3_600_100)), + Arguments.of("PT1H-1M-0.1S", Duration.ofMillis(3_539_900)), + Arguments.of("P-1DT2H-3M-0.1S", Duration.ofMillis(-79_380_100)), + Arguments.of("-P-1DT2H-3M-0.1S", Duration.ofMillis(79_380_100)), + Arguments.of("PT2562047788015215H30M-0.000000001S", Duration.ofSeconds(Long.MAX_VALUE - 8, 999_999_999)), + Arguments.of("-PT2562047788015215H30M-0.000000001S", Duration.ofSeconds(Long.MIN_VALUE + 8, 1)), + Arguments.of("PT-2562047788015215H-30M-0.000000001S", Duration.ofSeconds(Long.MIN_VALUE + 7, 999_999_999)), + Arguments.of("PT-9223372036854775808S", Duration.ofSeconds(Long.MIN_VALUE)), + Arguments.of("PT-9223372036854775807.999999999S", Duration.ofSeconds(Long.MIN_VALUE, 1)), + Arguments.of("PT9223372036854775807.999999999S", Duration.ofSeconds(Long.MAX_VALUE, 999_999_999))); + } + @ParameterizedTest @MethodSource("durationReferences") void testDurationReferenceRoundTrip(final Duration duration, final boolean modernOnly) throws Exception { @@ -386,6 +423,32 @@ class TestDriverAdapterCPDSReference { assertEquals(duration, restored.getMinEvictableIdleDuration()); } + @ParameterizedTest + @MethodSource("durationReferenceStrings") + void testDurationReferenceStrings(final String text, final Duration expected) throws Exception { + final Reference reference = new Reference(DriverAdapterCPDS.class.getName(), DriverAdapterCPDS.class.getName(), null); + reference.add(new StringRefAddr("durationBetweenEvictionRuns", text)); + reference.add(new StringRefAddr("minEvictableIdleDuration", text)); + reference.add(new StringRefAddr("timeBetweenEvictionRunsMillis", "0")); + reference.add(new StringRefAddr("minEvictableIdleTimeMillis", "0")); + final DriverAdapterCPDS restored = resolve(reference); + assertEquals(expected, restored.getDurationBetweenEvictionRuns()); + assertEquals(expected, restored.getMinEvictableIdleDuration()); + } + + @ParameterizedTest + @ValueSource(strings = {"PT-0.1234567890S", "PT--0.1S", "PT-0.-1S", "P-0.1S", "PT-0.1Sx", " PT-0.1S", "PT-0.1S\n", + "PT1M-0.1S-0.1S", "PT-0.1", "PT-0,1.2S", "PT9223372036854775808S", "PT-9223372036854775809S"}) + void testInvalidDurationReference(final String text) { + for (final String key : new String[] {"durationBetweenEvictionRuns", "minEvictableIdleDuration"}) { + final Reference reference = new Reference(DriverAdapterCPDS.class.getName(), DriverAdapterCPDS.class.getName(), null); + reference.add(new StringRefAddr(key, text)); + reference.add(new StringRefAddr("timeBetweenEvictionRunsMillis", "0")); + reference.add(new StringRefAddr("minEvictableIdleTimeMillis", "0")); + assertThrows(DateTimeParseException.class, () -> resolve(reference)); + } + } + @ParameterizedTest @ValueSource(longs = {Long.MIN_VALUE, -1, 0, 1, Integer.MAX_VALUE, 2_592_000_000L, Long.MAX_VALUE}) void testLegacyDurationReference(final long millis) throws Exception {
