pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Timestamp parameter gets sent as date
6+ messages / 3 participants
[nested] [flat]

* Timestamp parameter gets sent as date
@ 2017-09-22 13:43 Genya <genyap@online.ua>
  2017-09-22 14:04 ` Re: Timestamp parameter gets sent as date Dave Cramer <pg@fastcrypt.com>
  0 siblings, 1 reply; 6+ messages in thread

From: Genya @ 2017-09-22 13:43 UTC (permalink / raw)
  To: pgsql-jdbc

<p>I&#39;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&nbsp; t.id = ? AND (?::TIMESTAMP IS NULL OR t.date + t.time &lt;= ?::TIMESTAMP WITHOUT TIME ZONE) </span></p>

<p>&nbsp;</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>&nbsp;</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 />
&nbsp;&nbsp; statement.setNull(<span style="color:#0000CD">2</span>, Types.<em>DATE</em>);<br />
&nbsp;&nbsp; statement.setNull(<span style="color:#0000CD">3</span>, Types.<em>DATE</em>);<br />
}<br />
else {<br />
&nbsp;&nbsp; statement.setTimestamp(<span style="color:#0000CD">2</span>, Timestamp.<em>valueOf</em>(date));<br />
&nbsp;&nbsp; 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>&nbsp;</p>

<p>The issue is: though Java code sets date parameters to PreparedStatement correctly (checked with logging) i.e. $1 = &#39;29&#39;, $2 = &#39;2017-01-14:13:00&#39;, $3 = &#39;2017-01-14:13:00&#39; , but actual query performed by PostgreSQL server (logging) <strong>SOMETIMES </strong>shows parameters used <span style="font-family:courier new,courier,monospace">GMT DETAIL:&nbsp; parameters: $1 = &#39;29&#39;, $2 = &#39;2017-01-14&#39;, $3 = &#39;2017-01-14&#39;</span>.&nbsp; 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&#39;s behavior and output, sometimes after few tries or Java application restart. Worth mentioning, Java app does not do any &quot;caching&quot; or data modification - it&#39;s simple REST API for DB calls.</p>

<p>I&#39;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&#39;ve tried to enable postgres JDBC driver logging as it&#39;s said in documentation but did not manage it to operate with Spring Boot logging.</p>

<p>&nbsp;</p>

<p>&nbsp;</p>

<p>JDBC driver used: 42.1.4 (latest)</p>

<p>Server version: 9.5</p>

<p>Other related software: HikariCP</p>

<p>&nbsp;</p>

<p>&nbsp;</p>

<p>&nbsp;</p>



^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: Timestamp parameter gets sent as date
  2017-09-22 13:43 Timestamp parameter gets sent as date Genya <genyap@online.ua>
@ 2017-09-22 14:04 ` Dave Cramer <pg@fastcrypt.com>
  0 siblings, 0 replies; 6+ messages in thread

From: Dave Cramer @ 2017-09-22 14:04 UTC (permalink / raw)
  To: Genya <genyap@online.ua>; +Cc: pgsql-jdbc

On 22 September 2017 at 06:43, Genya <genyap@online.ua> wrote:

> I'm facing quite weird situation working on pure JDBC Java code connected
> to Postgres DB.
>
> The SQL used for PreparedStatement is relatively simple:
>
> SELECT * FROM test t INNER JOIN ....
>
> WHERE  t.id = ? AND (?::TIMESTAMP IS NULL OR t.date + t.time <=
> ?::TIMESTAMP WITHOUT TIME ZONE)
>
>
>
> t.date is [date NOT NULL]
> t.time is [time without time zone NOT NULL]
>
>
>
> So, Java code to prepare and set parameters is as following:
>
> PreparedStatement statement = connection.prepareStatement(*SQL*);
>
statement.setInt(1, id);
>
> if (date== null) {
>
you are changing the type of the parameter here when you setNull
Types.DATE. use Types.TIMESTAMP.

>

>    statement.setNull(2, Types.*DATE*);
>    statement.setNull(3, Types.*DATE*);
> }
>
else {
>    statement.setTimestamp(2, Timestamp.*valueOf*(date));
>    statement.setTimestamp(3, Timestamp.*valueOf*(date));
> }
>
> ResultSet rs = statement.executeQuery();
>
>
>
> 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) *SOMETIMES *shows parameters used GMT DETAIL:  parameters: $1 =
> '29', $2 = '2017-01-14', $3 = '2017-01-14'.  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.
>
> 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 *SOMETIMES
> *receives different parameters (date instead of date + time).
>
> 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.
>
>
Dave Cramer

davec@postgresintl.com
www.postgresintl.com

^ permalink  raw  reply  [nested|flat] 6+ messages in thread

*  Re: Re: [JDBC] Timestamp parameter gets sent as date
@ 2017-09-22 15:55 Genya <genyap@online.ua>
  2017-09-22 17:45 ` Re: Re: Re: [JDBC] Timestamp parameter gets sent as date Vladimir Sitnikov <sitnikov.vladimir@gmail.com>
  0 siblings, 1 reply; 6+ messages in thread

From: Genya @ 2017-09-22 15:55 UTC (permalink / raw)
  To: "Dave Cramer" <pg@fastcrypt.com>; +Cc: pgsql-jdbc

<p>Updated with Types.TIMESTAMP. Did not help. Though, I did not expect it to, since it&#39;s different algorithm branch.</p>

<p>&nbsp;</p>

<blockquote>
<div>
<div>&nbsp;
<div>
<div>
<div>
<div>On 22 September 2017 at 06:43, Genya <span dir="ltr">&lt;<a href="mailto:genyap@online.ua" target="_blank">genyap@online.ua</a>&gt;</span> wrote:</div>
</div>
</div>
</div>

<div>
<blockquote>
<p>I&#39;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:&amp">SELECT * FROM test t INNER JOIN .... </span></p>

<p><span style="font-family:&amp">WHERE&nbsp; <a href="http://t.id"; target="_blank">t.id</a> = ? AND (?::TIMESTAMP IS NULL OR t.date + t.time &lt;= ?::TIMESTAMP WITHOUT TIME ZONE) </span></p>

<p>&nbsp;</p>

<p><span style="font-family:&amp">t.date is [date NOT NULL]<br />
t.time is [time without time zone NOT NULL]</span></p>

<p>&nbsp;</p>

<p>So, Java code to prepare and set parameters is as following:</p>

<p><span style="font-family:&amp">PreparedStatement statement = connection.prepareStatement(<strong><em>SQL</em></strong>);</span>&nbsp;</p>
</blockquote>

<blockquote>
<p><span style="font-family:&amp">statement.setInt(<span style="color:rgb(0,0,255)">1</span>, id);</span></p>

<p><span style="font-family:&amp">if (date== null) {</span></p>
</blockquote>

<div>you are changing the type of the parameter here when you setNull Types.DATE. use Types.TIMESTAMP.&nbsp;</div>

<blockquote>&nbsp;</blockquote>

<div>&nbsp;</div>

<blockquote>
<p><span style="font-family:&amp">&nbsp;&nbsp; statement.setNull(<span style="color:rgb(0,0,205)">2</span>, Types.<em>DATE</em>);<br />
&nbsp;&nbsp; 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:&amp">else {<br />
&nbsp;&nbsp; statement.setTimestamp(<span style="color:rgb(0,0,205)">2</span>, Timestamp.<em>valueOf</em>(date));<br />
&nbsp;&nbsp; statement.setTimestamp(<span style="color:rgb(0,0,205)">3</span>, Timestamp.<em>valueOf</em>(date));<br />
}</span></p>

<p><span style="font-family:&amp">ResultSet rs = statement.executeQuery();</span></p>

<p>&nbsp;</p>

<p>The issue is: though Java code sets date parameters to PreparedStatement correctly (checked with logging) i.e. $1 = &#39;29&#39;, $2 = &#39;2017-01-14:13:00&#39;, $3 = &#39;2017-01-14:13:00&#39; , but actual query performed by PostgreSQL server (logging) <strong>SOMETIMES </strong>shows parameters used <span style="font-family:&amp">GMT DETAIL:&nbsp; parameters: $1 = &#39;29&#39;, $2 = &#39;2017-01-14&#39;, $3 = &#39;2017-01-14&#39;</span>.&nbsp; 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&#39;s behavior and output, sometimes after few tries or Java application restart. Worth mentioning, Java app does not do any &quot;caching&quot; or data modification - it&#39;s simple REST API for DB calls.</p>

<p>I&#39;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&#39;ve tried to enable postgres JDBC driver logging as it&#39;s said in documentation but did not manage it to operate with Spring Boot logging.</p>

<p>&nbsp;</p>
</blockquote>

<div>&nbsp;</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>
&nbsp;

<div>&nbsp;</div>
</div>
</div>
</div>
</blockquote>

<p>&nbsp;</p>

<p>&nbsp;</p>



^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: Re: Re: [JDBC] Timestamp parameter gets sent as date
  2017-09-22 15:55  Re: Re: [JDBC] Timestamp parameter gets sent as date Genya <genyap@online.ua>
@ 2017-09-22 17:45 ` Vladimir Sitnikov <sitnikov.vladimir@gmail.com>
  0 siblings, 0 replies; 6+ messages in thread

From: Vladimir Sitnikov @ 2017-09-22 17:45 UTC (permalink / raw)
  To: Genya <genyap@online.ua>; +Cc: pgsql-jdbc; \"Dave Cramer\" <pg@fastcrypt.com>

Genya>*SOMETIMES *shows parameters used GMT

It might be related with "user.timezone" Java property.

I would either recommend using Java 8's java.time API (it has notion for
timestamp+timezone) or use setTimestamp(.., Timestamp, Calendar).
There's PGTimestamp, however it is not much better than java.time.

In case of "setTimestamp(3, Timestamp)" the value that is sent to the DB
depends on the Java's default time zone.

Would you please check that?

Vladimir

>

^ permalink  raw  reply  [nested|flat] 6+ messages in thread

*  Re: [JDBC] Timestamp parameter gets sent as date
@ 2017-09-25 07:20 Genya <genyap@online.ua>
  2017-09-25 08:13 ` Re: Timestamp parameter gets sent as date Vladimir Sitnikov <sitnikov.vladimir@gmail.com>
  0 siblings, 1 reply; 6+ messages in thread

From: Genya @ 2017-09-25 07:20 UTC (permalink / raw)
  To: Vladimir Sitnikov <sitnikov.vladimir@gmail.com>; +Cc: pgsql-jdbc; Dave Cramer <pg@fastcrypt.com>

Thanks Vladimir for a hint. 
I have managed to make it work only adding Calendar.getInstance() as a 3d parameter to setTimestamp method. For record, DATE parameter is (was always) LocalDateTime class. Debugger always has shown date parameter set as 2017-01-10 16:45:00+02 so time zone was present (though I don't use it). Still don't understand why it's happening SOMETIMES (not always), seems like a bug ?

Regarding logging. Configured Postgres JDBC driver logging as described in documentation I have noticed that logging is active during Spring initialization (JDBC pool init etc.) but it does not log anything when the application is up. Is it Spring related ? Can anyone share complete configuration excerpt with STATEMENTS logging ?


-- 
Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-jdbc



^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: Timestamp parameter gets sent as date
  2017-09-25 07:20  Re: [JDBC] Timestamp parameter gets sent as date Genya <genyap@online.ua>
@ 2017-09-25 08:13 ` Vladimir Sitnikov <sitnikov.vladimir@gmail.com>
  0 siblings, 0 replies; 6+ messages in thread

From: Vladimir Sitnikov @ 2017-09-25 08:13 UTC (permalink / raw)
  To: Genya <genyap@online.ua>; +Cc: pgsql-jdbc; Dave Cramer <pg@fastcrypt.com>

>Debugger always has shown date parameter set as 2017-01-10 16:45:00+02 so
time zone was present (though I don't use it)
>Still don't understand why it's happening SOMETIMES (not always), seems
like a bug ?

Suppose the statement is "select function(?)".
In this case pgjdbc has no clue if backend wants "with time zone" or
"without time zone".
This is why pgjdbc passes the timestamp as string with timezone info like
"2017-01-10 16:45:00+02".
In case backend wants "with time zone" it uses the timezone from the
string, and in case backend wants "without time zone" it just silently
ignores the timezone.

Regarding "sometimes", I've no idea (yet).

>it does not log anything when the application is up. Is it Spring related ?

I've no idea. A reproducer would probably help to analyze the issue.

Vladimir

^ permalink  raw  reply  [nested|flat] 6+ messages in thread


end of thread, other threads:[~2017-09-25 08:13 UTC | newest]

Thread overview: 6+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2017-09-22 13:43 Timestamp parameter gets sent as date Genya <genyap@online.ua>
2017-09-22 14:04 ` Dave Cramer <pg@fastcrypt.com>
2017-09-22 15:55  Re: Re: [JDBC] Timestamp parameter gets sent as date Genya <genyap@online.ua>
2017-09-22 17:45 ` Vladimir Sitnikov <sitnikov.vladimir@gmail.com>
2017-09-25 07:20  Re: [JDBC] Timestamp parameter gets sent as date Genya <genyap@online.ua>
2017-09-25 08:13 ` Vladimir Sitnikov <sitnikov.vladimir@gmail.com>

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