Skip to content

Commit 5052ad1

Browse files
authored
Merge pull request #3 from johnpoth/master
Fix thread issue in DateTypeHandlerSupport
2 parents 0a3b5eb + 8832a28 commit 5052ad1

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

src/org/beanio/types/DateTypeHandlerSupport.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,12 @@
1515
*/
1616
package org.beanio.types;
1717

18-
import java.text.*;
19-
import java.util.*;
18+
import java.text.DateFormat;
19+
import java.text.ParsePosition;
20+
import java.text.SimpleDateFormat;
21+
import java.util.Date;
22+
import java.util.Properties;
23+
import java.util.TimeZone;
2024

2125
/**
2226
* This abstract type handler uses a <tt>SimpleDateFormat</tt> class to parse and format
@@ -38,7 +42,7 @@ public abstract class DateTypeHandlerSupport extends LocaleSupport implements Co
3842
// the same format instance can be reused if this type handler is not shared
3943
// by multiple unmarshallers/marshallers, this can lead to significant
4044
// performance improvements when parsing many records
41-
private transient DateFormat format;
45+
private transient ThreadLocal<DateFormat> format = new ThreadLocal<>();
4246

4347
/**
4448
* Constructs a new AbstractDateTypeHandler.
@@ -81,7 +85,7 @@ protected String formatDate(Date date) {
8185
}
8286

8387
private DateFormat getFormat() {
84-
return this.format != null ? this.format : createDateFormat();
88+
return this.format.get() != null ? this.format.get() : createDateFormat();
8589
}
8690

8791
/**
@@ -128,7 +132,8 @@ public DateTypeHandlerSupport newInstance(Properties properties) throws IllegalA
128132
handler.setPattern(pattern);
129133
handler.lenient = this.lenient;
130134
handler.timeZone = this.timeZone;
131-
handler.format = handler.createDateFormat();
135+
handler.format = new ThreadLocal<>();
136+
handler.format.set(handler.createDateFormat());
132137
return handler;
133138
}
134139
catch (CloneNotSupportedException e) {

0 commit comments

Comments
 (0)