pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Kris Jurka <books@ejurka.com>
To: Alastair Burr <alastair.burr@bluestar-software.co.uk>
Cc: Dave Cramer <pg@fastcrypt.com>
Cc: List <pgsql-jdbc@postgresql.org>
Subject: Re: JDBC - PreparedStatement reuse oddity when using setObject() with nulls and setTimestamp()
Date: Fri, 22 Feb 2013 13:34:24 -0500 (EST)
Message-ID: <alpine.BSO.2.00.1302221304500.26973@leary.csoft.net> (raw)
In-Reply-To: <5124F7C7.2040705@bluestar-software.co.uk>
References: <5123B72D.8020002@bluestar-software.co.uk>
	<CADK3HH+nmd8OSCN7PaEOVL2Smx5Dvc6uxq41=qz_EPnRP9W0FA@mail.gmail.com>
	<CADK3HH+BS38R9WN8hD_9PL5y36b4M4oKyRVoxTpiMpTbpte=pA@mail.gmail.com>
	<5124F7C7.2040705@bluestar-software.co.uk>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-jdbc>



On Wed, 20 Feb 2013, Alastair Burr wrote:

> Apologies, it looks like there is a subtelty here which I didn't realise /
> didn't mention before, you have to insert 4 records first before you insert
> the null record. I have tested with the latest driver and can now constantly
> reproduce this, more detail below:
> 

The attached patch fixes the provided test case for me.  The issue is 
somewhat complicated, so let's break it down...

Yes it is dependent on the number of previous prepared statement 
executions and the switchover point is the setting of prepareThreshold 
[1].  Once we reached the crossover point to use named statements as you 
saw in the server log, then the previous execution can affect subsequent 
executions of the same statement.

When sending time/timestamp/date objects to the server we do not 
explicitly tell the server what type we have because the server will do 
different conversions between the target type and the provided data 
depending on what source type we provide.  This is discussed in more 
detail in the comments in AbstractJdbc2Statement's setDate/setTimestamp.  
So we send these types as strings with "unknown" type to the server.

The above rule of passing unknown type for date/time/timestamp objects is 
bypassed when a PreparedStatement has exceeded prepareThreshold and we've 
received the true type information from the server.  In this case we pass 
the true information from the server back to the server.

So given all of the above, what's happening in this test case?  In this 
case, issuing setNull(x, Types.DATE) was providing the PreparedStatement 
with an explicit type of "date", not "unknown".  The PreparedStatement was 
taking this as truth with the expectation that it was recevied from the 
server and then using it for typing later executions that want to use a 
type of "unknown", resulting in later timestamps being truncated to dates.

The patch fixes the setNull(x, Types.DATE) call to pass "unknown" as the 
type.  This looks correct, but while reviewing the surrounding code, I 
think there may be more problems in this area.  Currently setDate with 
binary transfer binds an explicit date type.  Binary transfer requires 
explicit typing, but doesn't that suffer from the same problem posed here?  

[1] http://jdbc.postgresql.org/documentation/head/connect.html#connection-parameters

Kris Jurka

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

Attachments:

null-date-typing.patch (text/plain, 578B) download diff --git a/org/postgresql/jdbc2/AbstractJdbc2Statement.java b/org/postgresql/jdbc2/AbstractJdbc2Statement.java index 7671cf6..44f62b0 100644 --- a/org/postgresql/jdbc2/AbstractJdbc2Statement.java +++ b/org/postgresql/jdbc2/AbstractJdbc2Statement.java @@ -1189,8 +1189,6 @@ public abstract class AbstractJdbc2Statement implements BaseStatement oid = Oid.VARCHAR; break; case Types.DATE: - oid = Oid.DATE; - break; case Types.TIME: case Types.TIMESTAMP: oid = Oid.UNSPECIFIED;

view thread (7+ messages)

Message-ID: <alpine.BSO.2.00.1302221304500.26973@leary.csoft.net>
Permalink:  ../alpine.BSO.2.00.1302221304500.26973@leary.csoft.net/
Also on:    postgresql.org/message-id/alpine.BSO.2.00.1302221304500.26973@leary.csoft.net

 ·  · 

reply

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: books@ejurka.com, alastair.burr@bluestar-software.co.uk, pg@fastcrypt.com
  Subject: Re: JDBC - PreparedStatement reuse oddity when using setObject() with nulls and setTimestamp()
  In-Reply-To: <alpine.BSO.2.00.1302221304500.26973@leary.csoft.net>
  References: <5123B72D.8020002@bluestar-software.co.uk> <CADK3HH+nmd8OSCN7PaEOVL2Smx5Dvc6uxq41=qz_EPnRP9W0FA@mail.gmail.com> <CADK3HH+BS38R9WN8hD_9PL5y36b4M4oKyRVoxTpiMpTbpte=pA@mail.gmail.com> <5124F7C7.2040705@bluestar-software.co.uk> <alpine.BSO.2.00.1302221304500.26973@leary.csoft.net>

* 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