From: | Genya <genyap(at)online(dot)ua> |
---|---|
To: | pgsql-jdbc(at)postgresql(dot)org |
Subject: | Timestamp parameter gets sent as date |
Date: | 2017-09-22 13:43:40 |
Message-ID: | 6945_1506087820@online.ua |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-jdbc |
<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:courier new,courier,monospace">SELECT * FROM test t INNER JOIN .... </span></p>
<p><span style="font-family:courier new,courier,monospace">WHERE t.id = ? AND (?::TIMESTAMP IS NULL OR t.date + t.time <= ?::TIMESTAMP WITHOUT TIME ZONE) </span></p>
<p> </p>
<p><span style="font-family:courier new,courier,monospace">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:courier new,courier,monospace">PreparedStatement statement = connection.prepareStatement(<strong><em>SQL</em></strong>);<br />
statement.setInt(<span style="color:rgb(0, 0, 255)">1</span>, id);</span></p>
<p><span style="font-family:courier new,courier,monospace">if (date== null) {<br />
statement.setNull(<span style="color:#0000CD">2</span>, Types.<em>DATE</em>);<br />
statement.setNull(<span style="color:#0000CD">3</span>, Types.<em>DATE</em>);<br />
}<br />
else {<br />
statement.setTimestamp(<span style="color:#0000CD">2</span>, Timestamp.<em>valueOf</em>(date));<br />
statement.setTimestamp(<span style="color:#0000CD">3</span>, Timestamp.<em>valueOf</em>(date));<br />
}</span></p>
<p><span style="font-family:courier new,courier,monospace">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:courier new,courier,monospace">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>
<p> </p>
<p>JDBC driver used: 42.1.4 (latest)</p>
<p>Server version: 9.5</p>
<p>Other related software: HikariCP</p>
<p> </p>
<p> </p>
<p> </p>
Attachment | Content-Type | Size |
---|---|---|
unknown_filename | text/html | 3.1 KB |
From | Date | Subject | |
---|---|---|---|
Next Message | Dave Cramer | 2017-09-22 14:04:30 | Re: Timestamp parameter gets sent as date |
Previous Message | Tom Dunstan | 2017-09-22 01:10:10 | Re: Reading and writing off-heap data |