#1454 closed defect (fixed)

SimpleDateFormat implementation is not thread-safe

Reported by: Nicklas Nordborg Owned by: Nicklas Nordborg
Priority: critical Milestone: Reggie v4.44
Component: net.sf.basedb.reggie Keywords:
Cc:

Description

We use the java.text.SimpleDateFormat implementation to format and parse dates whenever we need to convert to or from a java.util.Date instance. In the Reggie class we have several static variants for date with/without separators and with/without time.

When modifying the release exporter plug-in to a multi-threaded implementation (see #1453) I noticed a few cases with strange dates in the output files. As it turns out the SimpleDateFormat is not thread-safe. When we are using the same instance with multiple threads at the same time the result can be mixed up.

Since Java 8 there is a newer implementation java.time.format.DateTimeFormatter that is supposed to be thread-safe. However, it is uncertain that it behaves exactly the same way when parsing dates. The new implementation can also only be used with the new Date/Time API which require that we convert back and forth.

Another option is to use the Apache Commons Lang FastDateFormat implementation. It has been designed to be a thread-safe drop-in replacement for the SimpleDateFormat implementation. https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/time/FastDateFormat.html We already have that library included in BASE so it would be an easy switch.

Change History (5)

comment:1 by Nicklas Nordborg, 13 months ago

In 7023:

References #1454: SimpleDateFormat implementation is not thread-safe

Added Apache Commons Lang 3 to the repository. We use the same version (3.10) as in BASE 3.19.

comment:2 by Nicklas Nordborg, 13 months ago

In 7024:

References #1454: SimpleDateFormat implementation is not thread-safe

Replaces SimpleDateFormat with FastDateFormat in DateToStringConverter and StringToDateConverter. The change affects a lot of other places that use the two converter implementations.

comment:3 by Nicklas Nordborg, 13 months ago

In 7025:

References #1454: SimpleDateFormat implementation is not thread-safe

Replaces SimpleDateFormat with FastDateFormat in most of the other places it is used (even though none of places are affected by mulit-threading).

comment:4 by Nicklas Nordborg, 13 months ago

In 7026:

References #1454: SimpleDateFormat implementation is not thread-safe

Fixed multi-threading issues in the DateValidator implementation. We need the non-lenient support in SimpleDateFormat (#1420) so we use synchronized block instead of the FastDateFormat implementation.

comment:5 by Nicklas Nordborg, 13 months ago

Resolution: fixed
Status: newclosed
Note: See TracTickets for help on using tickets.