Skip to content

Commit fbce876

Browse files
committed
Use java.time.Instant precision in
org.apache.commons.pool2.impl.ThrowableCallStack.Snapshot throwable message
1 parent ef93c90 commit fbce876

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ The <action> type attribute can be add,update,fix,remove.
4747
<body>
4848
<release version="2.12.1" date="YYYY-MM-DD" description="This is a feature and maintenance release (Java 8 or above).">
4949
<!-- FIX -->
50+
<action type="fix" dev="ggregory" due-to="Gary Gregory">Use java.time.Instant precision in org.apache.commons.pool2.impl.ThrowableCallStack.Snapshot throwable message.</action>
5051
<!-- ADD -->
5152
<!-- UPDATE -->
5253
<action type="update" dev="ggregory">Bump org.apache.commons:commons-parent from 62 to 73.</action>

src/main/java/org/apache/commons/pool2/impl/ThrowableCallStack.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.io.PrintWriter;
2020
import java.text.DateFormat;
2121
import java.text.SimpleDateFormat;
22+
import java.time.Instant;
2223

2324
/**
2425
* CallStack strategy that uses the stack trace from a {@link Throwable}. This strategy, while slower than the
@@ -34,12 +35,31 @@ public class ThrowableCallStack implements CallStack {
3435
* A snapshot of a throwable.
3536
*/
3637
private static final class Snapshot extends Throwable {
38+
3739
private static final long serialVersionUID = 1L;
38-
private final long timestampMillis = System.currentTimeMillis();
40+
private final Instant timestamp;
41+
42+
/**
43+
* Constructs a new instance with its message set to the now instant.
44+
*/
45+
public Snapshot() {
46+
this(Instant.now());
47+
}
48+
49+
/**
50+
* Constructs a new instance and use the timestamp as the message with using {@link DateTimeFormatter#ISO_INSTANT} for more precision.
51+
*
52+
* @param timestamp normally the now instant.
53+
*/
54+
private Snapshot(final Instant timestamp) {
55+
super(timestamp.toString());
56+
this.timestamp = timestamp;
57+
}
3958
}
4059

4160
private final String messageFormat;
4261

62+
// We keep the SimpleDateFormat for backward compatibility instead of a DateTimeFormatter.
4363
//@GuardedBy("dateFormat")
4464
private final DateFormat dateFormat;
4565

@@ -77,7 +97,8 @@ public synchronized boolean printStackTrace(final PrintWriter writer) {
7797
message = messageFormat;
7898
} else {
7999
synchronized (dateFormat) {
80-
message = dateFormat.format(Long.valueOf(snapshotRef.timestampMillis));
100+
// The throwable message is in {@link DateTimeFormatter#ISO_INSTANT} format for more precision.
101+
message = dateFormat.format(Long.valueOf(snapshotRef.timestamp.toEpochMilli()));
81102
}
82103
}
83104
writer.println(message);

0 commit comments

Comments
 (0)