From: | Genya <genyap(at)online(dot)ua> |
---|---|
To: | "Dave Cramer" <pg(at)fastcrypt(dot)com> |
Cc: | "List" <pgsql-jdbc(at)postgresql(dot)org> |
Subject: | Re: Re: [JDBC] Timestamp parameter gets sent as date |
Date: | 2017-09-22 15:55:04 |
Message-ID: | 6945_1506095704@online.ua |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-jdbc |
<p>Updated with Types.TIMESTAMP. Did not help. Though, I did not expect it to, since it's different algorithm branch.</p>
<p> </p>
<blockquote>
<div>
<div>
<div>
<div>
<div>
<div>On 22 September 2017 at 06:43, Genya <span dir="ltr"><<a href="mailto:genyap(at)online(dot)ua" target="_blank">genyap(at)online(dot)ua</a>></span> wrote:</div>
</div>
</div>
</div>
<div>
<blockquote>
<p>I'm facing quite weird situation working on pure JDBC Java code connected to Postgres DB.</p>
<p>The SQL used for PreparedStatement is relatively simple:</p>
<p><span style="font-family:&">SELECT * FROM test t INNER JOIN .... </span></p>
<p><span style="font-family:&">WHERE <a href="http://t.id" target="_blank">t.id</a> = ? AND (?::TIMESTAMP IS NULL OR t.date + t.time <= ?::TIMESTAMP WITHOUT TIME ZONE) </span></p>
<p> </p>
<p><span style="font-family:&">t.date is [date NOT NULL]<br />
t.time is [time without time zone NOT NULL]</span></p>
<p> </p>
<p>So, Java code to prepare and set parameters is as following:</p>
<p><span style="font-family:&">PreparedStatement statement = connection.prepareStatement(<strong><em>SQL</em></strong>);</span> </p>
</blockquote>
<blockquote>
<p><span style="font-family:&">statement.setInt(<span style="color:rgb(0,0,255)">1</span>, id);</span></p>
<p><span style="font-family:&">if (date== null) {</span></p>
</blockquote>
<div>you are changing the type of the parameter here when you setNull Types.DATE. use Types.TIMESTAMP. </div>
<blockquote> </blockquote>
<div> </div>
<blockquote>
<p><span style="font-family:&"> statement.setNull(<span style="color:rgb(0,0,205)">2</span>, Types.<em>DATE</em>);<br />
statement.setNull(<span style="color:rgb(0,0,205)">3</span>, Types.<em>DATE</em>);<br />
}</span></p>
</blockquote>
<blockquote>
<p><span style="font-family:&">else {<br />
statement.setTimestamp(<span style="color:rgb(0,0,205)">2</span>, Timestamp.<em>valueOf</em>(date));<br />
statement.setTimestamp(<span style="color:rgb(0,0,205)">3</span>, Timestamp.<em>valueOf</em>(date));<br />
}</span></p>
<p><span style="font-family:&">ResultSet rs = statement.executeQuery();</span></p>
<p> </p>
<p>The issue is: though Java code sets date parameters to PreparedStatement correctly (checked with logging) i.e. $1 = '29', $2 = '2017-01-14:13:00', $3 = '2017-01-14:13:00' , but actual query performed by PostgreSQL server (logging) <strong>SOMETIMES </strong>shows parameters used <span style="font-family:&">GMT DETAIL: parameters: $1 = '29', $2 = '2017-01-14', $3 = '2017-01-14'</span>. Strangest thing that in 95% cases of same request with different params the output (behaviour and parameters) is correct, i.e. parameters are set and seen as DATE + TIME both from API and PostgreSQL. Moreover, repeteadly passing problematic parameters finally gets right query's behavior and output, sometimes after few tries or Java application restart. Worth mentioning, Java app does not do any "caching" or data modification - it's simple REST API for DB calls.</p>
<p>I'm not sure if it might be something very simple (JDBC/DB configuration) or a bug in postgres JDBC driver - since there is no other sofware to blame. Java code seemingly does everything right but PostgresSQL <strong>SOMETIMES </strong>receives different parameters (date instead of date + time).</p>
<p>I've tried to enable postgres JDBC driver logging as it's said in documentation but did not manage it to operate with Spring Boot logging.</p>
<p> </p>
</blockquote>
<div> </div>
<div>
<div>
<div>
<div>Dave Cramer<br />
<br />
<a href="mailto:davec(at)postgresintl(dot)com" target="_blank">davec(at)postgresintl(dot)com</a></div>
<div><a href="http://www.postgresintl.com/" target="_blank">www.postgresintl.com</a></div>
</div>
</div>
</div>
<div> </div>
</div>
</div>
</div>
</blockquote>
<p> </p>
<p> </p>
Attachment | Content-Type | Size |
---|---|---|
unknown_filename | text/html | 4.0 KB |
From | Date | Subject | |
---|---|---|---|
Next Message | Vladimir Sitnikov | 2017-09-22 17:45:37 | Re: Re: Re: [JDBC] Timestamp parameter gets sent as date |
Previous Message | Dave Cramer | 2017-09-22 14:04:30 | Re: Timestamp parameter gets sent as date |