pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tilman Sandig <info@sandig.com>
To: 'Mikko Tiihonen' <Mikko.Tiihonen@nitorcreations.com>
Cc: pgsql-jdbc@postgresql.org
Subject: Re: JDBC 9.2: multiple execution of prepared statement-query delivers different double precision results via getString()
Date: Fri, 13 Sep 2013 19:47:23 +0200
Message-ID: <004601ceb0a9$4fab48b0$ef01da10$@com> (raw)
In-Reply-To: <56318f6405134b7faed245cea972e928@AMSPR07MB018.eurprd07.prod.outlook.com>
References: <004201ceb098$2d6d79f0$88486dd0$@com>
	<56318f6405134b7faed245cea972e928@AMSPR07MB018.eurprd07.prod.outlook.com>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-jdbc>

I think, you are right; but it might be a potential pitfall, since 

- the behavior is nondeterministic
- the results differ from documented sample codes and PgAdmin-query-results, especially when using the 
EXTRACT-function like 

    select EXTRACT(YEAR FROM now())

I didn't expect to retrieve "2013.0" with the 6th call...

Actually, my website crashed when processing the results in my xml-framework (that is the reason, why I use getString())
building a new date-string from extracted components.

-Tilman




-----Ursprüngliche Nachricht-----
Von: Mikko Tiihonen [mailto:Mikko.Tiihonen@nitorcreations.com] 
Gesendet: Freitag, 13. September 2013 18:12
An: Tilman Sandig; pgsql-jdbc@postgresql.org
Betreff: RE: [JDBC] JDBC 9.2: multiple execution of prepared statement-query delivers different double precision results via
getString()

The first few times the driver requests the server to return "1 as double precision" as a text value (thus using server side
toString function).

After that the driver evaluates the actual types of the columns and requests the server to send the value in binary format. Your
toString method will then invoke Double(1).toString on your client side.

If you modify your code to do
  System.out.println("Value: " + rs.getDouble(1)); the result should not change as statements change column values to binary
data encoding.

I do not think the driver can make guarantees about the format of getString values, unless the original database field is
textual.

-Mikko
________________________________________
From: pgsql-jdbc-owner@postgresql.org <pgsql-jdbc-owner@postgresql.org> on behalf of Tilman Sandig <info@sandig.com>
Sent: 13 September 2013 18:44
To: pgsql-jdbc@postgresql.org
Subject: [JDBC] JDBC 9.2: multiple execution of prepared statement-query delivers different double precision results via
getString()

Hello,

there might be a problem in the latest JDBC-driver (9.2 build 1003) when querying double precision-result columns multiple times
and retrieving the result with ResultSet.getString():

The first five queries work as expected, further calls append ".0" to the result-string.

Here is a sample code:

PreparedStatement stmt = myConnection.prepareStatement("select cast(1 as double precision)"); for (int x=0;x<12;x++){
    ResultSet rs = stmt.executeQuery();
    while (rs.next()){
        System.out.println("Value: " + rs.getString(1));
    }
}

-------
Output:
-------

Value: 1
Value: 1
Value: 1
Value: 1
Value: 1
Value: 1.0
Value: 1.0
Value: 1.0
Value: 1.0
Value: 1.0
Value: 1.0
Value: 1.0


The problem does not occur when using JDBC-driver version 9.1; I use PostgreSQL Version 9.3.0 (Win64/Vista)

Best regards,
  Tilman Sandig



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



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



view thread (4+ messages)  latest in thread

Message-ID: <004601ceb0a9$4fab48b0$ef01da10$@com>
Permalink:  ../004601ceb0a9$4fab48b0$ef01da10$@com/
Also on:    postgresql.org/message-id/004601ceb0a9$4fab48b0$ef01da10$@com

 · 

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: info@sandig.com, Mikko.Tiihonen@nitorcreations.com
  Subject: Re: JDBC 9.2: multiple execution of prepared statement-query delivers different double precision results via getString()
  In-Reply-To: <004601ceb0a9$4fab48b0$ef01da10$@com>
  References: <004201ceb098$2d6d79f0$88486dd0$@com> <56318f6405134b7faed245cea972e928@AMSPR07MB018.eurprd07.prod.outlook.com> <004601ceb0a9$4fab48b0$ef01da10$@com>

* 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