Discussion (7)0
Log in or sign up to continue

Hello Kevin...

The problem is a byte[] and java heap space. I have 200Mb into a InputStream and convert this a byte[], cause java.lang.OutOfMemoryError: Java heap space

It's a piece of my code.... 

 

ByteArrayOutputStream buffer = new ByteArrayOutputStream();

int nRead;

byte[] data = new byte[1024];

while ((nRead = binaryStream.read(data, 0, data.length)) != -1) {

    buffer.write(data, 0, nRead);

}


SerialBlob blob = new SerialBlob(buffer.toByteArray());

Hello Kevin. Thank you for your attention.


I am trying to send an InputStream to the database.
My Stream is coming from a rest service (jax-rs, upload) I have an average file size of 200Mb.

I have a "prepareStatement" with sql query "Insert into <Class with %GlobalBinaryString property>" 

If you have a java.sql.PreparedStatement and what seems to be a java.io.InputStream subclass (since you can call read() on it), why not utilize java.sql.PreparedStatement.setBinaryStream(int parameterIndex, InputStream x)? I am not sure which version of com.intersys.jdbc.CacheDriver you are using, but it is likely that your driver version has that API supported.

try (
    java.sql.Connection        cnxn  = DriverMasnager.getConnection(url, cnxnProps);
    java.sql.PreparedStatement pstmt = cnxn.prepareStatement(
            "INSERT INTO tableWithBlobField VALUES(?)");
){
    pstmt.setBinaryStream(1, binaryStreamFromRestAPI);
    pstmt.executeUpdate();
}