Methods on net.sourceforge.jtds.jdbc.DateTime
class such as toDate()
, toTime()
and toTimestamp()
create new instance of the java.util.GregorianCalendar
. But creating java.util.GregorianCalendar
is very costly operation that translates into poor performance when unpacking dates from ResultSet
.
Moreover the DateTime
class creates GregorianCalendar
in other places such as constructors and packTime()
method.
To solve the problem I suggest to re-use Calendar
instance. The easiest is to use ThreadLocal
to store GregorianCalendar
. And that's exactly what is done in the attached patch. Another option would be to pass GregorianCalendar
to DateTime
when it is created but that would require to update all places where DateTime
is used.
NOTE: the patch does not cleanup ThreadLocal
but one can easily add a removeCalendar
static method which can be called for example when underlying ResultSet
is closed and will remove calendar instance from ThreadLocal
.
I'm also attaching the DateTimeBenchmark
class which is a micro-benchmark written using JMH framework. The results of running benchmark on original code and code with the patch included are shown below (time is nanoseconds per operation):
[-img src=DateTime_benchmark_results.png alt=Benchmark results: missing =-]
Anonymous
Patch and benchmark results attached
Benchmark results:

The patch was created against
DateTime
class from version 1.3.1.Attached GregorianCalendar.patch that replaces all occurrences of the
new GregorianCalendar
calls inside jTDS 1.3.1 code, which is onlyDateTime
andSupport
classes.