From: Kris Jurka <books@ejurka.com>
To: Jeenicke, Martti <martti.jeenicke@coremedia.com>
Cc: pgsql-jdbc@postgresql.org
Subject: Re: Strange behavior after setting timestamp field to null - A bug?
Date: Mon, 8 Feb 2010 14:34:12 -0500 (EST)
Message-ID: <alpine.BSO.2.00.1002081420001.21307@leary.csoft.net> (raw)
In-Reply-To: <AE2A8E488D9B26438919DF3C9C95528D03CE85F9@hermes.coremedia.com>
References: <AE2A8E488D9B26438919DF3C9C95528D03CE85F9@hermes.coremedia.com>
On Mon, 8 Feb 2010, Jeenicke, Martti wrote:
> Hi there,
>
> we have noticed an odd problem/bug when working with timestamp fields in
> prepared statements. The problem arises when setting the timestamp field
> to null. The test class to reproduce the behavior produces the following
> output:
>
> 08.02.2010 16:36:20
> 08.02.2010 16:36:20
> 08.02.2010 16:36:20
> 08.02.2010 17:36:20
I've looked into this a little bit. The problem is how the data gets
typed when it is sent to the server. When calling setTimestamp, the
driver doesn't know whether the server type that will be used will be with
or without a timezone. (The SQL Standard and the JDBC API don't match up
well here.) So it sends the data as type "unknown" and lets the server
figure out how to deal with it because it has additional type information.
When calling setNull, the driver thought it was safe to type it as
timestamp with timezone to try and help type inference because NULL values
look the same with or without timezones. This is looked OK, but you've
caught the case here where it is not. By default, the fifth execution of
a PreparedStatement will establish a more permanent execution plan that
will then be re-used for later executions. So the fifth execution in your
test is a setNull case and that is effectively establishing the types that
a later execution will use as well. So later executions fail to pass the
data as "unknown" and are instead passing it as "timestamp with tz" which
does not match up with your table, so you get the mystery drift.
The attached patch fixes things for me in a simple test, but I'd like to
look at it a little more before I commit it.
Kris Jurka
Attachments:fix-setnull-unknown.patch (text/plain, 1022B) download
Index: org/postgresql/jdbc2/AbstractJdbc2Statement.java
===================================================================
RCS file: /cvsroot/jdbc/pgjdbc/org/postgresql/jdbc2/AbstractJdbc2Statement.java,v
retrieving revision 1.115
diff -c -r1.115 AbstractJdbc2Statement.java
*** org/postgresql/jdbc2/AbstractJdbc2Statement.java 26 Sep 2009 15:21:21 -0000 1.115
--- org/postgresql/jdbc2/AbstractJdbc2Statement.java 8 Feb 2010 19:33:24 -0000
***************
*** 1109,1118 ****
oid = Oid.DATE;
break;
case Types.TIME:
- oid = Oid.TIME;- break;
case Types.TIMESTAMP:
! oid = Oid.TIMESTAMPTZ;
break;
case Types.BIT:
oid = Oid.BOOL;
--- 1109,1116 ----
oid = Oid.DATE;
break;
case Types.TIME:
case Types.TIMESTAMP:
! oid = Oid.UNSPECIFIED;
break;
case Types.BIT:
oid = Oid.BOOL;
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, martti.jeenicke@coremedia.com
Subject: Re: Strange behavior after setting timestamp field to null - A bug?
In-Reply-To: <alpine.BSO.2.00.1002081420001.21307@leary.csoft.net>
References: <AE2A8E488D9B26438919DF3C9C95528D03CE85F9@hermes.coremedia.com> <alpine.BSO.2.00.1002081420001.21307@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