Changeset 8109


Ignore:
Timestamp:
Feb 8, 2023, 1:58:34 PM (8 months ago)
Author:
Nicklas Nordborg
Message:

References #2293: SimpleDateFormat? implementation is not thread-safe

Replaced SimpleDateFormat with FastDateFormat in several places where it was easy to do so. The DateFormatter implementation was solved with syncronization instead since we need the non-lenient feature of the SimpleDateFormat in some cases.

Location:
branches/3.19-stable
Files:
9 edited

Legend:

Unmodified
Added
Removed
  • branches/3.19-stable/.classpath

    r8102 r8109  
    3636  <classpathentry kind="lib" path="lib/dist/postgresql-42.5.1.jar"/>
    3737  <classpathentry kind="lib" path="lib/dist/zip4j-2.11.2.jar"/>
     38  <classpathentry kind="lib" path="lib/dist/commons-lang3-3.10.jar" sourcepath="C:/Users/thep-nni/AppData/Local/Temp/.org.sf.feeling.decompiler1675842571201/source/commons-lang3-3.10-sources.jar"/>
    3839  <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-11">
    3940    <attributes>
  • branches/3.19-stable/src/clients/web/net/sf/basedb/clients/web/servlet/RssNewsFeed.java

    r7962 r8109  
    2424import java.nio.charset.Charset;
    2525import java.nio.charset.StandardCharsets;
    26 import java.text.SimpleDateFormat;
    2726import java.util.ArrayList;
    2827import java.util.Date;
     
    3635import javax.servlet.http.HttpServletResponse;
    3736
     37import org.apache.commons.lang3.time.FastDateFormat;
    3838import org.jdom2.Document;
    3939import org.jdom2.Element;
     
    189189    // Generate the XML document
    190190    String channelTitle = "BASE news from " + host + contextPath;
    191     SimpleDateFormat rfc822 = new SimpleDateFormat("EEE', 'dd' 'MMM' 'yyyy' 'HH:mm:ss' 'Z", Locale.US);
     191    FastDateFormat rfc822 = FastDateFormat.getInstance("EEE', 'dd' 'MMM' 'yyyy' 'HH:mm:ss' 'Z", Locale.US);
    192192
    193193    // <rss version="2.0">
  • branches/3.19-stable/src/core/net/sf/basedb/core/DateUtil.java

    r7514 r8109  
    2323package net.sf.basedb.core;
    2424
    25 import java.text.SimpleDateFormat;
    2625import java.util.Calendar;
    2726import java.util.Date;
    2827import java.util.List;
     28
     29import org.apache.commons.lang3.time.FastDateFormat;
    2930
    3031/**
     
    105106  }
    106107 
    107   private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
     108  private static final FastDateFormat DATE_FORMAT = FastDateFormat.getInstance("yyyy-MM-dd");
    108109 
    109110  /**
     
    133134        catch (Exception ex2)
    134135        {
    135           throw new DateFormatException("The value "+value+" is not a valid date for format: " + DATE_FORMAT.toPattern());
     136          throw new DateFormatException("The value "+value+" is not a valid date for format: " + DATE_FORMAT.getPattern());
    136137        }
    137138      }
     
    152153 
    153154
    154   private static final SimpleDateFormat TIMESTAMP_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
     155  private static final FastDateFormat TIMESTAMP_FORMAT = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss");
    155156
    156157  /**
     
    181182        catch (Exception ex2)
    182183        {
    183           throw new DateFormatException("The value "+value+" is not a valid timestamp for format: " + TIMESTAMP_FORMAT.toPattern());
     184          throw new DateFormatException("The value "+value+" is not a valid timestamp for format: " + TIMESTAMP_FORMAT.getPattern());
    184185        }
    185186      }
  • branches/3.19-stable/src/core/net/sf/basedb/core/File.java

    r8017 r8109  
    6060import java.nio.charset.Charset;
    6161import java.nio.charset.StandardCharsets;
    62 import java.text.SimpleDateFormat;
    6362import java.util.Date;
    6463import java.util.LinkedList;
     
    6968
    7069import org.anarres.parallelgzip.ParallelGZIPOutputStream;
     70import org.apache.commons.lang3.time.FastDateFormat;
    7171
    7272import java.security.MessageDigest;
     
    15751575 
    15761576  private static final String separator = java.io.File.separator;
    1577   private static final SimpleDateFormat SUBDIR_FORMAT =
    1578     new SimpleDateFormat("yyyy-ww"+separator+"dd-ss");
     1577  private static final FastDateFormat SUBDIR_FORMAT = FastDateFormat.getInstance("yyyy-ww"+separator+"dd-ss");
    15791578
    15801579  /**
  • branches/3.19-stable/src/core/net/sf/basedb/core/migrate/DateWriter.java

    r5896 r8109  
    2727import java.sql.ResultSet;
    2828import java.sql.SQLException;
    29 import java.text.DateFormat;
     29
     30import org.apache.commons.lang3.time.FastDateFormat;
    3031
    3132
     
    3334  Generic writer implementation that writes date values from a result
    3435  set. This writer use {@link ResultSet#getDate(int)} to read data from
    35   the result set and the {@link DateFormat#format(java.util.Date)}
     36  the result set and the {@link FastDateFormat#format(java.util.Date)}
    3637  to format the value.
    3738 
     
    4344  extends AbstractResultWriter
    4445{
    45   private final DateFormat dateFormat;
     46  private final FastDateFormat dateFormat;
    4647 
    47   public DateWriter(ResultSet rs, int index, Writer writer, char[] nullEscape, DateFormat dateFormat)
     48  public DateWriter(ResultSet rs, int index, Writer writer, char[] nullEscape, FastDateFormat dateFormat)
    4849  {
    4950    super(rs, index, writer, nullEscape);
  • branches/3.19-stable/src/core/net/sf/basedb/core/migrate/TimestampWriter.java

    r5896 r8109  
    2727import java.sql.SQLException;
    2828import java.sql.Timestamp;
    29 import java.text.DateFormat;
     29
     30import org.apache.commons.lang3.time.FastDateFormat;
    3031
    3132
     
    3334  Generic writer implementation that writes timestamp values from a result
    3435  set. This writer use {@link ResultSet#getTimestamp(int)} to read data from
    35   the result set and the {@link DateFormat#format(java.util.Date)}
     36  the result set and the {@link FastDateFormat#format(java.util.Date)}
    3637  to format the value.
    3738 
     
    4344  extends AbstractResultWriter
    4445{
    45   private final DateFormat dateFormat;
     46  private final FastDateFormat dateFormat;
    4647 
    47   public TimestampWriter(ResultSet rs, int index, Writer writer, char[] nullEscape, DateFormat dateFormat)
     48  public TimestampWriter(ResultSet rs, int index, Writer writer, char[] nullEscape, FastDateFormat dateFormat)
    4849  {
    4950    super(rs, index, writer, nullEscape);
  • branches/3.19-stable/src/core/net/sf/basedb/core/migrate/postgres/PostgresResultWriterFactory.java

    r5896 r8109  
    2525import java.sql.ResultSet;
    2626import java.sql.Types;
    27 import java.text.DateFormat;
    28 import java.text.SimpleDateFormat;
     27
     28import org.apache.commons.lang3.time.FastDateFormat;
    2929
    3030import net.sf.basedb.core.BaseException;
     
    6262    PostgreSQL uses 'yyyy-MM-dd' for dates.
    6363   */
    64   private final DateFormat dateFormat;
     64  private final FastDateFormat dateFormat;
    6565  /**
    6666    PostgreSQL uses 'yyyy-MM-dd HH:mm:ss' for timestamps.
    6767   */
    68   private final DateFormat timestampFormat;
     68  private final FastDateFormat timestampFormat;
    6969 
    7070  private char[][] BINARY_ESCAPE;
     
    7575  {
    7676    this.NULL_ESCAPE = new char[] { '\\', 'N' };
    77     this.dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    78     this.timestampFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
     77    this.dateFormat = FastDateFormat.getInstance("yyyy-MM-dd");
     78    this.timestampFormat = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss");
    7979  }
    8080 
  • branches/3.19-stable/src/core/net/sf/basedb/util/HttpUtil.java

    r7461 r8109  
    2323
    2424import java.io.IOException;
    25 import java.text.SimpleDateFormat;
    2625import java.util.Date;
    2726import java.util.Locale;
    2827
    29 
    30 
     28import org.apache.commons.lang3.time.FastDateFormat;
    3129import org.apache.http.Header;
    3230import org.apache.http.HttpResponse;
     
    4745{
    4846 
    49   private static final SimpleDateFormat HTTP_DATE_FORMAT = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss z", Locale.ENGLISH);
     47  private static final FastDateFormat HTTP_DATE_FORMAT = FastDateFormat.getInstance("EEE, d MMM yyyy HH:mm:ss z", Locale.ENGLISH);
    5048
    5149  private static UserAgentAnalyzer userAgentAnalyzer;
  • branches/3.19-stable/src/core/net/sf/basedb/util/formatter/DateFormatter.java

    r7646 r8109  
    3535  Format a date for output in a client application. This implementation
    3636  uses the {@link SimpleDateFormat} standard formatting routines.
     37  <p>
     38  NOTE! The SimpleDateFormat implementation is not thread-safe which may
     39  have affected instances of this class before BASE 3.19.7. Since BASE 3.19.7
     40  this class has implemented synchronization and should be thread-safe.
     41  But beware of the {@link #getDateFormat()} which return the underlying
     42  formatter instance that is not thread-safe!
    3743 
    3844  @author nicklas
     
    8490  public String format(Date value)
    8591  {
    86     return value == null ? "" : dateFormat.format(value);
     92    if (value == null) return "";
     93    synchronized (dateFormat)
     94    {
     95      return dateFormat.format(value);
     96    }
    8797  }
    8898  @Override
     
    92102    try
    93103    {
    94       return dateFormat.parse(value);
     104      synchronized (dateFormat)
     105      {
     106        return dateFormat.parse(value);
     107      }
    95108    }
    96109    catch (ParseException ex)
Note: See TracChangeset for help on using the changeset viewer.