Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1aEYWg-0006oW-TO for pgsql-jdbc@arkaria.postgresql.org; Thu, 31 Dec 2015 08:24:03 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84) (envelope-from ) id 1aEYWf-0003ED-Ng for pgsql-jdbc@arkaria.postgresql.org; Thu, 31 Dec 2015 08:24:01 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84) (envelope-from ) id 1aEYWc-0003CQ-ER for pgsql-jdbc@postgresql.org; Thu, 31 Dec 2015 08:23:58 +0000 Received: from smtprelay03.ispgateway.de ([80.67.29.7]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84) (envelope-from ) id 1aEYWT-00024w-Ux for pgsql-jdbc@postgresql.org; Thu, 31 Dec 2015 08:23:57 +0000 Received: from [88.66.22.70] (helo=Zauselmaus) by smtprelay03.ispgateway.de with esmtpsa (TLSv1.2:DHE-RSA-AES256-GCM-SHA384:256) (Exim 4.84) (envelope-from ) id 1aEYV2-0005BA-Nx; Thu, 31 Dec 2015 09:22:20 +0100 From: "Markus KARG" To: "'Dave Cramer'" , "'Adam Rauch'" Cc: "'List'" References: <5684506D.4040006@labkey.com> In-Reply-To: Subject: Re: Recent backward compatibility break in PreparedStatement.setObject() Date: Thu, 31 Dec 2015 09:23:48 +0100 Organization: Markus KARG, Staatl. gepr. Inf. Message-ID: <00be01d143a4$92c77de0$b85679a0$@eu> MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_NextPart_000_00BF_01D143AC.F48BE5E0" X-Mailer: Microsoft Office Outlook 12.0 Thread-Index: AdFDTNKUsDS39utUQKe87MyQokaB9QAV6WAw Content-Language: de X-AVK-Virus-Check: AVA 25.4965;8ADD39BE X-AVK-Spam-Check: 1; str=0001.0A0C0201.5684E613.01AF,ss=1,re=0.000,recu=0.000,reip=0.000,cl=1,cld=1,fgs=0; F1206 X-Df-Sender: bWFya3VzQGhlYWRjcmFzaGluZy5ldQ== X-Pg-Spam-Score: -2.6 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-jdbc Precedence: bulk Sender: pgsql-jdbc-owner@postgresql.org This is a multi-part message in MIME format. ------=_NextPart_000_00BF_01D143AC.F48BE5E0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable I'm the original contributor of that changeset. This is simply a bug. = I'll fix it later today. Thanks for reporting! :-) -Markus =20 From: pgsql-jdbc-owner@postgresql.org = [mailto:pgsql-jdbc-owner@postgresql.org] On Behalf Of Dave Cramer Sent: Mittwoch, 30. Dezember 2015 22:55 To: Adam Rauch Cc: List Subject: Re: [JDBC] Recent backward compatibility break in = PreparedStatement.setObject() =20 No, this was not intentional. =20 Thanks for the report! Dave Cramer davec@postgresintl.com www.postgresintl.com =20 On 30 December 2015 at 16:45, Adam Rauch wrote: The PostgreSQL JDBC driver recently changed PreparedStatement behavior = with calls such as setObject(index, value, Types.VARCHAR). Previously, = the driver would call value.toString() before binding all VARCHAR = values... meaning any object could be passed as "value." In recent = updates (e.g., 9.4.1207), most non-String values now result in an = exception: Exception in thread "main" org.postgresql.util.PSQLException: Cannot = convert an instance of XXX to type String at = org.postgresql.jdbc.PgStatement.cannotCastException(PgStatement.java:1813= ) at = org.postgresql.jdbc.PgStatement.cannotCastException(PgStatement.java:1809= ) at = org.postgresql.jdbc.PgStatement.castToString(PgStatement.java:1805) at = org.postgresql.jdbc.PgStatement.setObject(PgStatement.java:1540) at = org.postgresql.jdbc.PgStatement.setObject(PgStatement.java:1818) at PGTest.main(PGTest.java:25) This seems to have broken during the refactor to support binary transfer = for setObject(), https://github.com/pgjdbc/pgjdbc/issues/328. Was the = behavior change intentional? If so, it should probably be documented = (e.g., I didn't see any mention in the change log). If not, I'd suggest = going back to calling value.toString() for all types. I'm sure ours is = not the only application that relies on this long-standing (>10 years) = PostgreSQL JDBC behavior. FWIW, other JDBC drivers call toString() in = this situation. Here's a contrived example that demonstrates the behavior change. This = code snippet runs fine in 1201 but fails with the exception above in = 1207. Object param =3D new Object() {public String toString() {return = "tables";}}; =20 try (PreparedStatement stmt =3D conn.prepareStatement("SELECT * FROM = information_schema.tables WHERE table_name =3D ?")) { stmt.setObject(1, param, Types.VARCHAR); =20 try (ResultSet rs =3D stmt.executeQuery()) { while (rs.next()) System.out.println(rs.getString(1)); } } Thanks, Adam =20 ------=_NextPart_000_00BF_01D143AC.F48BE5E0 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

I'm the original contributor of that changeset. This is simply a bug. = I'll fix it later today. Thanks for reporting! = :-)

-Markus

 

From:= = pgsql-jdbc-owner@postgresql.org [mailto:pgsql-jdbc-owner@postgresql.org] = On Behalf Of Dave Cramer
Sent: Mittwoch, 30. Dezember = 2015 22:55
To: Adam Rauch
Cc: = List
Subject: Re: [JDBC] Recent backward compatibility break = in PreparedStatement.setObject()

 

No, = this was not intentional.

 

Thanks for the = report!


 

On 30 December 2015 at 16:45, Adam Rauch <adam@labkey.com> wrote:

The PostgreSQL JDBC = driver recently changed PreparedStatement behavior with calls such as = setObject(index, value, Types.VARCHAR). Previously, the driver would = call value.toString() before binding all VARCHAR values... meaning any = object could be passed as "value." In recent updates (e.g., = 9.4.1207), most non-String values now result in an = exception:

    Exception in thread = "main" org.postgresql.util.PSQLException: Cannot convert an = instance of XXX to type String
        at = org.postgresql.jdbc.PgStatement.cannotCastException(PgStatement.java:1813= )
        at = org.postgresql.jdbc.PgStatement.cannotCastException(PgStatement.java:1809= )
        at = org.postgresql.jdbc.PgStatement.castToString(PgStatement.java:1805)<= br>        at = org.postgresql.jdbc.PgStatement.setObject(PgStatement.java:1540)
=         at = org.postgresql.jdbc.PgStatement.setObject(PgStatement.java:1818)
=         at = PGTest.main(PGTest.java:25)


This seems to have broken = during the refactor to support binary transfer for setObject(), https://github.com/pgjdbc/pgjdbc/issues/328. Was = the behavior change intentional? If so, it should probably be documented = (e.g., I didn't see any mention in the change log). If not, I'd suggest = going back to calling value.toString() for all types. I'm sure ours is = not the only application that relies on this long-standing (>10 = years) PostgreSQL JDBC behavior. FWIW, other JDBC drivers call = toString() in this situation.

Here's a contrived example that = demonstrates the behavior change. This code snippet runs fine in 1201 = but fails with the exception above in 1207.

=C2=A0=C2=A0=C2=A0 Object param =
=3D new =
Object() =
{public =
String toString() =
{return =
"tables";}};
 
=C2=A0=C2=A0=C2=A0 try =
(PreparedStatement stmt =3D =
conn.prepareStatement("SELECT * FROM information_schema.tables =
WHERE table_name =3D ?"))
=C2=A0=C2=A0=C2=A0 =
{
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=
=C2=A0 stmt.setObject(1, param, Types.VARCHAR);
 
=C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0=C2=A0try =
(ResultSet rs =3D =
stmt.executeQuery())
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=
=C2=A0 {
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=
=C2=A0 =C2=A0=C2=A0=C2=A0=C2=A0while =
(rs.next())
=
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 =
System.out.println(rs.getString(1));
=C2=A0 =
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0}
=C2=A0=C2=A0=C2=A0 =
}


Thanks,
Adam

 

------=_NextPart_000_00BF_01D143AC.F48BE5E0--