Thursday, November 25, 2010

java.lang.NumberFormatException: For input string: "4294967295"

Question:
I use a CachedRowSet instance with an Oracle 8i (i get the same result
with a 9i) datbase. if the table i do the query in contains any CLOB
or BLOB column, when i execute the populate(ResultSet) method i get the
following exception :
java.lang.NumberFormatException: For input string: "4294967295"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:438)
at java.lang.Integer.parseInt(Integer.java:476)
at oracle.jdbc.driver.OracleResultSetMetaData.getPrecision(OracleResultSetMetaData.java:331)
at sun.jdbc.rowset.CachedRowSet.initMetaData(CachedRowSet.java:681)
at sun.jdbc.rowset.CachedRowSet.populate(CachedRowSet.java:647)
at sun.jdbc.rowset.RowSetReaderImpl.readData(RowSetReaderImpl.java:146)
at sun.jdbc.rowset.CachedRowSet.execute(CachedRowSet.java:712)
at database.DBService.main(DBService.java:123)
There's a way to avoid this?
Answer:
Oracle's JDBC driver throws a NumberFormatException when calling ResultSetMetaData.getPrecision(colIndex) - which returns an "int" - passing in the column index of a LOB column. This is because it erroneously returns the LOB size as the precision value - the Oracle LOB size being 4Gb (4294967295 bytes). This is, of course, beyond the range of a Java "int" primitive type, hence the exception.
To avoid this annoying bug, ensure that your Java application runs with the following system property:
-Doracledatabasemetadata.get_lob_precision=false
Code:
e.g. java -Doracledatabasemetadata.get_lob_precision=false MyJavaMainClass
Note that this bug seems to be present in BOTH the 'thin' and 'OCI' drivers, versions 8.1.7.0.0 through to 10.1.0.2.0.
There is a dearth of information on the Net about this problem, however, I did manage to dig up this link:
http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&safe=off&threadm=3bd021a2.2426028%40forums.sybase.com&rnum=3&prev=/groups%3Fq%3D%252Bprecision%2B%252Bclob%2B%252Bjdbc%2B%252Boracle%26hl%3Den%26lr%3D%26ie%3DUTF-8%26safe%3Doff%26scoring%3Dd%26selm%3D3bd021a2.2426028%2540forums.sybase.com%26rnum%3D3