You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@struts.apache.org by "Mike Calmus (JIRA)" <ji...@apache.org> on 2008/04/03 17:42:58 UTC

[jira] Commented: (WW-2567) DateTimePicker makes non-thread-safe use of SimpleDateFormat

    [ https://issues.apache.org/struts/browse/WW-2567?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=43576#action_43576 ] 

Mike Calmus commented on WW-2567:
---------------------------------

Looks like this is already fixed in 2.1.x. Perhaps it could be back-ported to 2.0.x?

> DateTimePicker makes non-thread-safe use of SimpleDateFormat
> ------------------------------------------------------------
>
>                 Key: WW-2567
>                 URL: https://issues.apache.org/struts/browse/WW-2567
>             Project: Struts 2
>          Issue Type: Bug
>          Components: Core Actions
>    Affects Versions: 2.0.12
>            Reporter: Mike Calmus
>            Priority: Critical
>
> DateTimePicker has an internal static final instance of a SimpleDateFormat it uses to format date strings.
> However, according to the SimpleDateFormat JavaDoc:
> Date formats are not synchronized. It is recommended to create separate format instances for each thread. If multiple threads access a format concurrently, it must be synchronized externally.
> Easy fix is to synchronize around all uses of the class.
> Index: src/main/java/org/apache/struts2/components/DateTimePicker.java
> ===================================================================
> --- src/main/java/org/apache/struts2/components/DateTimePicker.java     (revision 641326)
> +++ src/main/java/org/apache/struts2/components/DateTimePicker.java     (working copy)
> @@ -294,20 +294,25 @@
>              return null;
>          if(obj instanceof Date) {
> +          synchronized (RFC3339_FORMAT) {
>              return RFC3339_FORMAT.format((Date) obj);
> +          }
>          } else {
>              // try to parse a date
>              String dateStr = obj.toString();
>              if(dateStr.equalsIgnoreCase("today"))
> +              synchronized (RFC3339_FORMAT) {
>                  return RFC3339_FORMAT.format(new Date());
> -
> +              }
>              try {
>                  Date date = null;
>                  if(this.displayFormat != null) {
>                      SimpleDateFormat format = new SimpleDateFormat(
>                              (String) getParameters().get("displayFormat"));
>                      date = format.parse(dateStr);
> -                    return RFC3339_FORMAT.format(date);
> +                    synchronized (RFC3339_FORMAT) {
> +                      return RFC3339_FORMAT.format(date);
> +                    }
>                  } else {
>                      // last resource to assume already in correct/default format
>                      return dateStr;

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.