WayTooSimpleDateFormatToBeBelieved

Why is it always the things that are supposed to be simple that come around and byte you in the behind? In my present (Java) project (a quasi-real-time data aquisition and archiving module), I had a SimpleDateFormat for parsing and formating dates in UTC format. Things were all cute and sweet, until I got more than one client loading the server. Then, all hell broke loose on the dates that got to the tags; here’s an example:

 

2008-07-14 10:33:52,valor_calculado1,40.0,1

2008-07-14 10:33:52,SetPointZito0,14.42,1

2008-07-14 10:33:52,SetPointZito1,50.0,1

2008-07-14 10:27:25,Posição do elevador A17,43,1

2040-01-31 10:27:00,Freq. oscilacao da plataforma A17,8,1

0037-01-01 00:27:00,Valvula IN 01V001 A50,0,1

2008-07-01 00:00:00,Valvula IN 01V004 A50,0,1

2008-06-30 00:00:00,Pressao Vapor Caldeira A17,1.256,1

2008-01-01 10:00:55,Consumo - Agua da Rede A17,3.74,1

2008-07-14 10:27:58,alarmezecovisual0,1,1

2008-07-01 00:00:00,valor_calculado0,10.25,2

2008-01-14 00:28:00,alarmezecovisual1,1,1

2008-07-14 10:28:00,valor_calculado1,25.0,1

2008-07-14 00:00:00,SetPointZito0,20.25,1

2008-01-01 10:28:25,SetPointZito1,35.0,1

2008-07-14 10:33:58,Posição do elevador A17,26,1

2008-07-14 10:33:58,Freq. oscilacao da plataforma A17,-20.0,2

2008-07-14 10:33:58,Valvula IN 01V001 A50,0,1

 

This is supposed to be the transition from 2008-07-14 10:33:52 UTC to 2008-07-14 10:33:58 UTC, but as you can see there are a lot of bogus dates in between… the problem was a transient one (and darn difficult to track down), apparently the SimpleDateFormat class is not Thread-safe (I have 7 different work threads on this baby, not counting the dynamic RMI ones of course). Oh, oh, wait, what’s this on the SimpleDateFormat API docs?

 

Synchronization

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.

 

RTFM situation, I guess. Oh well.

Leave a Reply