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/03/26 16:55:59 UTC

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

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.


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

Posted by "Rene Gielen (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/struts/browse/WW-2567?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Rene Gielen reassigned WW-2567:
-------------------------------

    Assignee: James Mitchell

> 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.11.1
>            Reporter: Mike Calmus
>            Assignee: James Mitchell
>            Priority: Critical
>             Fix For: 2.0.12
>
>
> 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.


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

Posted by "Mike Calmus (JIRA)" <ji...@apache.org>.
    [ 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.


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

Posted by "James Mitchell (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/struts/browse/WW-2567?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

James Mitchell resolved WW-2567.
--------------------------------

    Resolution: Fixed

I have applied these changes to the STRUTS_2_0_X branch

> 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.11.1
>            Reporter: Mike Calmus
>            Priority: Critical
>             Fix For: 2.0.12
>
>
> 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.


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

Posted by "Don Brown (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/struts/browse/WW-2567?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Don Brown updated WW-2567:
--------------------------

    Affects Version/s:     (was: 2.0.12)
                       2.0.11.1
        Fix Version/s: 2.0.12

> 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.11.1
>            Reporter: Mike Calmus
>            Priority: Critical
>             Fix For: 2.0.12
>
>
> 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.