OutOfMemory when inserting stream of unknown length

From: "Mikko T(dot)" <mtiihone(at)cc(dot)hut(dot)fi>
To: pgsql-jdbc(at)postgresql(dot)org
Subject: OutOfMemory when inserting stream of unknown length
Date: 2004-08-19 10:44:06
Message-ID: Pine.OSF.4.60.0408191028070.368856@kosh.hut.fi
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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

Responses

Browse pgsql-jdbc by date

  From Date Subject
Next Message Kris Jurka 2004-08-19 18:42:39 Re: OutOfMemory when inserting stream of unknown length
Previous Message Oliver Jowett 2004-08-19 10:43:09 Re: Error on connection