From: Genya <genyap@online.ua>
To: "Dave Cramer" <pg@fastcrypt.com>
Cc: "List" <pgsql-jdbc@postgresql.org>
Subject: Re: Re: [JDBC] Timestamp parameter gets sent as date
Date: Fri, 22 Sep 2017 18:55:04 +0300
Message-ID: <6945_1506095704@online.ua> (raw)
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-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@online.ua" target="_blank">genyap@online.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@postgresintl.com" target="_blank">davec@postgresintl.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>
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-jdbc@postgresql.org
Cc: genyap@online.ua, pg@fastcrypt.com
Subject: Re: [JDBC] Timestamp parameter gets sent as date
In-Reply-To: <6945_1506095704@online.ua>
References: <6945_1506095704@online.ua>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox