OutOfMemory when inserting stream of unknown length - Mailing list pgsql-jdbc

From Mikko T.
Subject OutOfMemory when inserting stream of unknown length
Date
Msg-id Pine.OSF.4.60.0408191028070.368856@kosh.hut.fi
Whole thread Raw
Responses Re: OutOfMemory when inserting stream of unknown length
Re: OutOfMemory when inserting stream of unknown length
List pgsql-jdbc
When writing a java stream of unknown length to a bytea column with the
prepared statement setBinaryStream the jdbc drivers throws always an
OutOfMemoryException.

I'm calling the function with following parameters as I do not know the real
length of the stream:

stm.setBinaryStream(1, stream, Interger.MAX_VALUE);

and it seems that somewhere along the way the jdbc driver wrongly assumes that
the stream contains gigabytes of data and a memory allocation fails.

The other jdbc drivers I have tried (oracle 9.2, hypersonic 1.7.2.3) do accept
this.


The javadoc itself (reproduced at the bottom of the mail) is a bit
controversial as it at the same time says that the stream will have 'length'
number of bytes, but on the other hand says that all data will be read. I have
interpreted this so that the length is just a hint and jdbc driver must not
store more bytes than the length, but an end-of-file before the length bytes
is still valid and shouldn't cause any exceptions. And the javadoc might also
mean that if the stream hasn't reached end-of-file when length bytes have been
read the jdbc driver should still continue reading but discarding the
remaining bytes.

In summary my understanding:
stream length | length parameter | result
--------------+------------------+--------
  10           | 5                | 5 bytes inserted, 5 bytes discarded
  10           | 10               | 10 bytes inserted
  10           | 15               | 10 bytes inserted

--- javadoc snippet:

The PreparedStatement.setBinaryStream(int parameterIndex,
                                       InputStream x,
                                       int length)

Sets the designated parameter to the given input stream, which will
have the specified number of bytes... The data will be read from the stream as
needed until end-of-file is reached.

     Parameters:
         parameterIndex - the first parameter is 1, the second is 2, ...
         x - the java input stream which contains the binary parameter value
         length - the number of bytes in the stream

pgsql-jdbc by date:

Previous
From: Oliver Jowett
Date:
Subject: Re: Error on connection
Next
From: Kris Jurka
Date:
Subject: Re: OutOfMemory when inserting stream of unknown length