Java ClassCastException using JDBC driver and selecting smallint arrays - Mailing list pgsql-bugs

From Michael Wimmer
Subject Java ClassCastException using JDBC driver and selecting smallint arrays
Date
Msg-id 56B20029.5010605@salzburgresearch.at
Whole thread Raw
List pgsql-bugs
Hello,

I am developing a Java application using PostgreSQL 9.4 and the current JDBC driver in version 9.4.1207.
I have a simple select statement on a table having a column of type smallint[].
The Java code in the row mapper after query execution looks like:

Array accessTypeArray = rs.getArray("accesstypes");
if (accessTypeArray != null) {
  Integer[] accessTypeIds = (Integer[]) accessTypeArray.getArray();
  ...
}

In some cases of execution this code works fine, in other case (depending on the implementation of the underlying
datasource,don't know exactly the 
reason) the code ends up in:

java.lang.ClassCastException: [Ljava.lang.Short; cannot be cast to [Ljava.lang.Integer;

I have debugged for some time and found an inconsistent implementation in the class PgArray. Calling the method
"getArray()"returns an array of Short 
in case of "fieldBytes" not null (calling submethod "readBinaryArray()") or an array of Integer in the other case
(callingsubmethod "buildArray()"). 

Is it correct that smallint arrays are mapped to Short[]? (see
http://docs.oracle.com/javase/6/docs/technotes/guides/jdbc/getstart/mapping.html#table3)

Regards,
Michael

pgsql-bugs by date:

Previous
From: jpaul@aconex.com
Date:
Subject: BUG #13911: pg_upgrade from 8.4 to 9.5 broken
Next
From: John R Pierce
Date:
Subject: Re: BUG #13911: pg_upgrade from 8.4 to 9.5 broken