openjpa-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Heath Thomann (Created) (JIRA)" <>
Subject [jira] [Created] (OPENJPA-2067) A 'length' of '-1' passed to PreparedStatement.setBinaryStream can cause an exception on some, but not all, JDBC drivers.
Date Fri, 28 Oct 2011 20:47:32 GMT
A 'length' of '-1' passed to PreparedStatement.setBinaryStream can cause an exception on some,
but not all, JDBC drivers.

                 Key: OPENJPA-2067
             Project: OpenJPA
          Issue Type: Bug
          Components: jdbc, sql
    Affects Versions: 1.2.3, 2.0.2, 2.1.2, 2.2.0
            Reporter: Heath Thomann
            Assignee: Heath Thomann
            Priority: Minor

I recently worked on an issue where a JDBC driver is throwing an exception when attempting
to persists an InputStream:

Caused by: <openjpa-1.2.3-SNAPSHOT-r422266:1152904 nonfatal general error> org.apache.openjpa.persistence.PersistenceException:
String or buffer length not valid.
FailedObject: prepstmnt 1608671202 UPDATE PMYPJRK SET lastModificationDate = ?, fileLength
= ?, fileName = ?, inputStream = ? WHERE id = ? [org.apache.openjpa.jdbc.kernel.JDBCStoreManager$CancelPreparedStatement]
Caused by: java.sql.SQLException: String or buffer length not valid.
	at org.apache.openjpa.lib.jdbc.DelegatingPreparedStatement.setBinaryStream(
	at org.apache.openjpa.lib.jdbc.LoggingConnectionDecorator$LoggingConnection$LoggingPreparedStatement.setBinaryStream(
	at org.apache.openjpa.lib.jdbc.DelegatingPreparedStatement.setBinaryStream(
	at org.apache.openjpa.jdbc.sql.DBDictionary.setBinaryStream(
	at org.apache.openjpa.jdbc.sql.DBDictionary.setTyped(
	at org.apache.openjpa.jdbc.sql.RowImpl.flush(
	at org.apache.openjpa.jdbc.sql.RowImpl.flush(
	at org.apache.openjpa.jdbc.kernel.BatchingPreparedStatementManagerImpl.flushSingleRow(
	at org.apache.openjpa.jdbc.kernel.BatchingPreparedStatementManagerImpl.flushBatch(
	... 138 more

This exception occurs because the particular JDBC driver checks the value of the 'length'
variable passed to the 'PreparedStatement.setBinaryStream' method:

setBinaryStream(int parameterIndex, InputStream x, int length) 

The 'length' parameter of method 'setBinaryStream' is supposed to be the length of the InputStream
parameter.  OpenJPA code is passing a value of '-1' for the length as the length is not known.
 Some JDBC drivers allow this value.  Technically speaking the javadoc for 'PreparedStatement.setBinaryStream'
doesn't state that a negative value is not acceptable or what should happen when a negative
length is passed to 'setBinaryStream' (which is likely why some drivers allow a negative value
and others do not).  On the other hand, it doesn't make sense for a length of an InputStream
to be negative.  Given this, and the fact that OpenJPA is not aware of the length of the stream,
I'll work on a solution which uses a JDBC 4.0 version of 'setBinaryStream' which doesn't take
a length.  The fix will of course require JDK 6 + JDBC 4.0 driver runtime.

This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators:!default.jspa
For more information on JIRA, see:


View raw message