Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1aEOZG-0005r2-4s for pgsql-jdbc@arkaria.postgresql.org; Wed, 30 Dec 2015 21:46:02 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84) (envelope-from ) id 1aEOZF-0004JF-Kh for pgsql-jdbc@arkaria.postgresql.org; Wed, 30 Dec 2015 21:46: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 1aEOYk-0003lD-3w for pgsql-jdbc@postgresql.org; Wed, 30 Dec 2015 21:45:30 +0000 Received: from mail-ig0-x22d.google.com ([2607:f8b0:4001:c05::22d]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84) (envelope-from ) id 1aEOYg-0006C1-0b for pgsql-jdbc@postgresql.org; Wed, 30 Dec 2015 21:45:29 +0000 Received: by mail-ig0-x22d.google.com with SMTP id to18so173256022igc.0 for ; Wed, 30 Dec 2015 13:45:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=labkey-com.20150623.gappssmtp.com; s=20150623; h=to:from:subject:message-id:date:user-agent:mime-version :content-type; bh=f4oSnpgzC0FiSqLfZP1+BrjziLu7urkLFymLEFwnE0g=; b=cHORFi0S7b8c3zkL850ytqTRyKa1q7aiZ9N9a0DpBWM4wz4lB75V1s8ELHtd+NEu49 FpoXza8cDAJGnP17erMnxpkti3oqYoiKJip0hd0LHsGcgt5MUIP90e7avI4WhJ6moN3A oB3vNPBy8pojNwJPHVmwchQv7q83Q8Wm27g8q4mZ6ztXcWrS2lNW1CcKHS9PdiNI0gsV 5gzft2gqQa6uyqFz4GOkwFerHgFJn4hr0k0fPbEn2c1oDfImP2+iTg/UIfZsFxR14r3y 31Z2cJZdK3oEA2Iw2kF0h0ifWybWQ5x/Xoz+e8RNXv0ICZx4dBddiqlV3EmzDFDWDP81 Hi9Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:to:from:subject:message-id:date:user-agent :mime-version:content-type; bh=f4oSnpgzC0FiSqLfZP1+BrjziLu7urkLFymLEFwnE0g=; b=QAV0UEcDnqqC6lQI+4d0Da7ATff7gL0D2wWOxB1UrlckkDNFRJYdl+ljAy5Avtzd7u xUandQz0GQzOh/UWSbiMPbnGBu8EPjKmXPTBNpfMqxXdvec+ECXlf16RIxM430QZo+zp LDi3PNDFcgFyhdlONJ5VyjMUL5VgZEN2h9NIno59/n9RS+CrXx76wls7qBssEbJbI9JQ dr01+nzgcMyvPZ1fuduKggrVwnHnqRyUBF6xGgUS+Plkw9Kmw66Bl5BByFVg2uJiew/h BcIQH68TwWQQ68D1cbSiMC1Le0sO4By3sj4Nz0yy+D6mWtUqEz7HJ4R6ojnsscZLRoXk NsLg== X-Gm-Message-State: ALoCoQn8MvIO0P3584PFXds06hdoIC+BtTVrDAUvBnv9OgoiSZENGi0RStbVf2YNJVGgymPKlzotZSAcgKPaBexruktrOQzRKA== X-Received: by 10.50.117.33 with SMTP id kb1mr65785879igb.89.1451511923973; Wed, 30 Dec 2015 13:45:23 -0800 (PST) Received: from [10.78.246.53] ([74.202.208.74]) by smtp.googlemail.com with ESMTPSA id g3sm23565997igg.3.2015.12.30.13.45.23 for (version=TLSv1/SSLv3 cipher=OTHER); Wed, 30 Dec 2015 13:45:23 -0800 (PST) To: pgsql-jdbc@postgresql.org From: Adam Rauch Subject: Recent backward compatibility break in PreparedStatement.setObject() Message-ID: <5684506D.4040006@labkey.com> Date: Wed, 30 Dec 2015 13:45:17 -0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.5.0 MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="------------060008060808010506000607" 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. --------------060008060808010506000607 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit 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 =new Object() {public String toString() {return "tables";}}; try (PreparedStatement stmt = conn.prepareStatement("SELECT *FROM information_schema.tables WHERE table_name = ?")) { stmt.setObject(1, param, Types.VARCHAR); try (ResultSet rs = stmt.executeQuery()) { while (rs.next()) System.out.println(rs.getString(1)); } } Thanks, Adam --------------060008060808010506000607 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 8bit 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 = new Object() {public String toString() {return "tables";}};

    try (PreparedStatement stmt = conn.prepareStatement("SELECT * FROM information_schema.tables WHERE table_name = ?"))
    {
        stmt.setObject(1, param, Types.VARCHAR);

        try (ResultSet rs = stmt.executeQuery())
        {
            while (rs.next())
                System.out.println(rs.getString(1));
        }
    }

Thanks,
Adam
--------------060008060808010506000607--