<?xml version="1.0" encoding="utf-8"?>
<feed xml:lang="en" xmlns="http://www.w3.org/2005/Atom"><title>Recent changes to 128: DateTime conversion to java.sql types is slow</title><link href="https://sourceforge.net/p/jtds/patches/128/" rel="alternate"/><link href="https://sourceforge.net/p/jtds/patches/128/feed.atom" rel="self"/><id>https://sourceforge.net/p/jtds/patches/128/</id><updated>2014-10-02T08:54:08.741000Z</updated><subtitle>Recent changes to 128: DateTime conversion to java.sql types is slow</subtitle><entry><title>#128 DateTime conversion to java.sql types is slow</title><link href="https://sourceforge.net/p/jtds/patches/128/?limit=25#7022" rel="alternate"/><published>2014-10-02T08:54:08.741000Z</published><updated>2014-10-02T08:54:08.741000Z</updated><author><name/><uri>https://sourceforge.net</uri></author><id>https://sourceforge.netca45c0f36f0778eb979f5a2e822630464ea853e6</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;Attached &lt;strong&gt;GregorianCalendar.patch&lt;/strong&gt; that replaces all occurrences of the &lt;code&gt;new GregorianCalendar&lt;/code&gt; calls inside jTDS 1.3.1 code, which is only &lt;code&gt;DateTime&lt;/code&gt; and &lt;code&gt;Support&lt;/code&gt; classes.&lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>#128 DateTime conversion to java.sql types is slow</title><link href="https://sourceforge.net/p/jtds/patches/128/?limit=25#1847" rel="alternate"/><published>2014-10-01T09:25:26.166000Z</published><updated>2014-10-01T09:25:26.166000Z</updated><author><name/><uri>https://sourceforge.net</uri></author><id>https://sourceforge.net35177a5640c1c7b8ce6bffa5d7d6a18b9cb9dad6</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;The patch was created against &lt;code&gt;DateTime&lt;/code&gt; class from version &lt;strong&gt;1.3.1&lt;/strong&gt;.&lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>#128 DateTime conversion to java.sql types is slow</title><link href="https://sourceforge.net/p/jtds/patches/128/?limit=25#1463" rel="alternate"/><published>2014-10-01T09:24:11.829000Z</published><updated>2014-10-01T09:24:11.829000Z</updated><author><name/><uri>https://sourceforge.net</uri></author><id>https://sourceforge.neteaec5d3e9d8ec2fc121a2894d5938c67b6850d41</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;&lt;strong&gt;Benchmark results&lt;/strong&gt;:&lt;br /&gt;
&lt;img src="https://sourceforge.net/p/jtds/patches/_discuss/thread/fdb18a35/4eaf/attachment/DateTime_benchmark_results.png" /&gt;&lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>#128 DateTime conversion to java.sql types is slow</title><link href="https://sourceforge.net/p/jtds/patches/128/?limit=25#4eaf" rel="alternate"/><published>2014-10-01T09:21:35.471000Z</published><updated>2014-10-01T09:21:35.471000Z</updated><author><name/><uri>https://sourceforge.net</uri></author><id>https://sourceforge.net527c46ba97ef367382820440d6f6b19f9a15d1ac</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;Patch and benchmark results attached&lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>DateTime conversion to java.sql types is slow</title><link href="https://sourceforge.net/p/jtds/patches/128/" rel="alternate"/><published>2014-10-01T09:20:46.393000Z</published><updated>2014-10-01T09:20:46.393000Z</updated><author><name/><uri>https://sourceforge.net</uri></author><id>https://sourceforge.net8c44d12a1cf209c12a28f7670655faa0bb8ee706</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;Methods on &lt;code&gt;net.sourceforge.jtds.jdbc.DateTime&lt;/code&gt; class such as &lt;code&gt;toDate()&lt;/code&gt;, &lt;code&gt;toTime()&lt;/code&gt; and &lt;code&gt;toTimestamp()&lt;/code&gt; create new instance of the &lt;code&gt;java.util.GregorianCalendar&lt;/code&gt;. But creating &lt;code&gt;java.util.GregorianCalendar&lt;/code&gt; is very costly operation that translates into poor performance when unpacking dates from &lt;code&gt;ResultSet&lt;/code&gt;.&lt;br /&gt;
Moreover the &lt;code&gt;DateTime&lt;/code&gt; class creates &lt;code&gt;GregorianCalendar&lt;/code&gt; in other places such as constructors and &lt;code&gt;packTime()&lt;/code&gt; method.&lt;/p&gt;
&lt;p&gt;To solve the problem I suggest to re-use &lt;code&gt;Calendar&lt;/code&gt; instance. The easiest is to use &lt;code&gt;ThreadLocal&lt;/code&gt; to store &lt;code&gt;GregorianCalendar&lt;/code&gt;. And that's exactly what is done in the attached patch. Another option would be to pass &lt;code&gt;GregorianCalendar&lt;/code&gt; to &lt;code&gt;DateTime&lt;/code&gt; when it is created but that would require to update all places where &lt;code&gt;DateTime&lt;/code&gt; is used.&lt;br /&gt;
&lt;strong&gt;NOTE:&lt;/strong&gt; the patch does not cleanup &lt;code&gt;ThreadLocal&lt;/code&gt; but one can easily add a &lt;code&gt;removeCalendar&lt;/code&gt; static method which can be called for example when underlying &lt;code&gt;ResultSet&lt;/code&gt; is closed and will remove calendar instance from &lt;code&gt;ThreadLocal&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;I'm also attaching the &lt;code&gt;DateTimeBenchmark&lt;/code&gt; class which is a micro-benchmark written using &lt;a class="" href="http://openjdk.java.net/projects/code-tools/jmh" rel="nofollow"&gt;JMH&lt;/a&gt; framework. The results of running benchmark on original code and code with the patch included are shown below (&lt;em&gt;time is nanoseconds per operation&lt;/em&gt;):&lt;br /&gt;
[-img src=DateTime_benchmark_results.png alt=Benchmark results: missing =-]&lt;/p&gt;&lt;/div&gt;</summary></entry></feed>