Reputation: 7705
I have a situation where I have to use a Calendar object when retrieving a Timestamp from an Oracle ResultSet. I would like to define the Calendar object at the class level so that a single instance of it can be utilized in this manner by multiple threads. I know that Calendar itself is not thread safe, but I am unclear as to how it is used behind the scenes by Oracle's implementation of ResultSet.getTimestamp(String, Calendar). Looking at the source code for MySQL, the Calendar is strictly used to pull the TimeZone object (which doesn't appear to be altered either), so for MySQL it appears to be thread safe. Can anyone shed any light on whether or not a single instance of Calendar is safe to use with Oracle's implementation of this method in a multi-threaded environment?
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
Timestamp ts = rs.getTimestamp("example",cal);
Upvotes: 2
Views: 1162
Reputation: 403551
Since Oracle driver isn't open-source, it's hard to say. You could, I suppose, decompile the driver to see what it does, but I wouldn't risk it.
Firstly, I'd benchmark your app to determine just how expensive it is to create a Calendar
. It may be cheap enough that you don't need to worry about it - just create one on demand. If you still think it's too expensive, then use a ThreadLocal
to hold them. Assuming you use a thread-pool, then you get re-used Calendar
objects without worrying about thread-safety.
Upvotes: 3