• Post Reply Bookmark Topic Watch Topic
  • New Topic

Using SingleDateFormat in EJB  RSS feed

 
Elinor Chang
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • 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 ]
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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.
 
Ådne Brunborg
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
  • 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 ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!