Skip to content

fix: 修复 SimpleDateFormat 线程安全的问题#69

Open
gtn1024 wants to merge 1 commit intomasterfrom
getn/fix-simpledateformat-synchronization-issue
Open

fix: 修复 SimpleDateFormat 线程安全的问题#69
gtn1024 wants to merge 1 commit intomasterfrom
getn/fix-simpledateformat-synchronization-issue

Conversation

@gtn1024
Copy link
Contributor

@gtn1024 gtn1024 commented Nov 26, 2025

No description provided.

@gtn1024 gtn1024 requested review from aruis and Copilot November 26, 2025 06:51
@gtn1024 gtn1024 self-assigned this Nov 26, 2025
@gtn1024 gtn1024 added the bug Something isn't working label Nov 26, 2025
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a critical thread safety issue in the CustomDateSerializer class by removing static SimpleDateFormat instances and creating new instances per serialization call instead.

Key Changes

  • Removed static DATE_FORMAT and CALENDAR_FORMAT fields that caused thread safety issues
  • Refactored serialize() method to instantiate SimpleDateFormat locally for each call

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +48 to +54
SimpleDateFormat formatter;
if (value instanceof java.sql.Date) {
gen.writeString(CALENDAR_FORMAT.format(value));
formatter = new SimpleDateFormat("yyyy-MM-dd");
} else {
gen.writeString(DATE_FORMAT.format(value));
formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
}
gen.writeString(formatter.format(value));
Copy link

Copilot AI Nov 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While creating a new SimpleDateFormat instance per call fixes the thread safety issue, this approach has a performance cost. Consider using DateTimeFormatter instead, which is thread-safe and can be stored as a static final field (similar to CustomLocalDateTimeSerializer).

For example:

private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");
private static final DateTimeFormatter DATETIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

@Override
public void serialize(Date value, JsonGenerator gen, SerializerProvider provider) throws IOException {
    if (value instanceof java.sql.Date) {
        gen.writeString(DATE_FORMATTER.format(value.toInstant().atZone(ZoneId.systemDefault()).toLocalDate()));
    } else {
        gen.writeString(DATETIME_FORMATTER.format(value.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime()));
    }
}

Alternatively, if you need to stick with SimpleDateFormat, use ThreadLocal<SimpleDateFormat> to avoid creating new instances on every call while maintaining thread safety.

Copilot uses AI. Check for mistakes.
@gtn1024 gtn1024 marked this pull request as draft November 26, 2025 07:00
@gtn1024 gtn1024 marked this pull request as ready for review November 26, 2025 07:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants