• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Using SingleDateFormat in EJB

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As we all know SingleDateFormat is not thread safe. I have a project where I created a static instance of SimpleDateFormat and multiple MDBs are using the instance to format a String to a Date type. I got the following exception at times:

For input string: ""
- java.lang.NumberFormatException.forInputString(Unknown Source)
- java.lang.Long.parseLong(Unknown Source)
- java.lang.Long.parseLong(Unknown Source)
- java.text.DigitList.getLong(Unknown Source)
- java.text.DecimalFormat.parse(Unknown Source)
- java.text.SimpleDateFormat.subParse(Unknown Source)
- java.text.SimpleDateFormat.parse(Unknown Source)
- java.text.DateFormat.parse(Unknown Source)

EJB is known to be thread safe. I am using the SingleDateFormat in an EJB, why I am getting this exception?

Thanks
[ November 01, 2006: Message edited by: Elinor Chang ]
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You said it yourself: SimpleDateFormat is not thread-safe. And using a reference to a non-thread-safe object from an object that is thread-safe does not magically change that condition.

So, stop trying to share a single SimpleDateFormat object between multiple threads. The simplest way to do that would be to create a new SimpleDateFormat object every time you need it.
 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Elinor Chang:
EJB is known to be thread safe.



That depends on the developer of the EJB. There is nothing to stop me from creating any number of threads inside an EJB, and stopping, waiting, etc... nothing, that is, except my own knowledge that I shouldn't do it. The EJB would compile, would be deployable, and might even run, without being threadsafe.

So, EJBs should be thread safe, but it is up to the developer to make it so. It doesn't happen automagically.
[ November 02, 2006: Message edited by: �dne Brunborg ]
reply
    Bookmark Topic Watch Topic
  • New Topic