JdbcDate.getString() is not thread-safe Patch by Dave Brosius; reviewed by eevans for CASSANDRA-3822
Project: http://git-wip-us.apache.org/repos/asf/cassandra/repo Commit: http://git-wip-us.apache.org/repos/asf/cassandra/commit/bf40da4b Tree: http://git-wip-us.apache.org/repos/asf/cassandra/tree/bf40da4b Diff: http://git-wip-us.apache.org/repos/asf/cassandra/diff/bf40da4b Branch: refs/heads/trunk Commit: bf40da4bed66730bb867d43505a52dbede95cd6d Parents: 000078e Author: Eric Evans <[email protected]> Authored: Wed Feb 1 16:50:36 2012 -0600 Committer: Eric Evans <[email protected]> Committed: Wed Feb 1 17:02:16 2012 -0600 ---------------------------------------------------------------------- .../org/apache/cassandra/cql/jdbc/JdbcDate.java | 12 +++++++++--- 1 files changed, 9 insertions(+), 3 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cassandra/blob/bf40da4b/src/java/org/apache/cassandra/cql/jdbc/JdbcDate.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/cassandra/cql/jdbc/JdbcDate.java b/src/java/org/apache/cassandra/cql/jdbc/JdbcDate.java index 44c2274..c612c28 100644 --- a/src/java/org/apache/cassandra/cql/jdbc/JdbcDate.java +++ b/src/java/org/apache/cassandra/cql/jdbc/JdbcDate.java @@ -43,7 +43,13 @@ public class JdbcDate extends AbstractJdbcType<Date> "yyyy-MM-ddZ" }; static final String DEFAULT_FORMAT = iso8601Patterns[3]; - static final SimpleDateFormat FORMATTER = new SimpleDateFormat(DEFAULT_FORMAT); + static final ThreadLocal<SimpleDateFormat> FORMATTER = new ThreadLocal<SimpleDateFormat>() + { + protected SimpleDateFormat initialValue() + { + return new SimpleDateFormat(DEFAULT_FORMAT); + } + }; public static final JdbcDate instance = new JdbcDate(); @@ -76,7 +82,7 @@ public class JdbcDate extends AbstractJdbcType<Date> public String toString(Date obj) { - return FORMATTER.format(obj); + return FORMATTER.get().format(obj); } public boolean needsQuotes() @@ -96,7 +102,7 @@ public class JdbcDate extends AbstractJdbcType<Date> } // uses ISO-8601 formatted string - return FORMATTER.format(new Date(bytes.getLong(bytes.position()))); + return FORMATTER.get().format(new Date(bytes.getLong(bytes.position()))); } public Class<Date> getType()
