CreateBlob

Primary tabs

Java, Caché

Hi all....

I couldn't find documentation on how to create a blob using the cache JDBC driver.
I found only one reading example:


https://cedocs.intersystems.com/latest/csp/docbook/DocBook.UI.Page.cls?K...

The createBlob Method of the connection class was not implemented:

https://cedocs.intersystems.com/latest/csp/docbook/DocBook.UI.Page.cls?K...
 

Does anyone know how to create a blob without compromising the java heap space?

 

  • 0
  • 0
  • 53
  • 6
  • 1

Answers

Hello Marcio,

The answer you are looking for can be found in the SerialBlob javadocs: this is the only known provided implementation of the java.sql.Blob interface. In particular, you would like the the SerialBlob(byte[]b) constructor to create a blob in Java.

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());

Can I get more context about what you are trying to do? Are you trying to insert a stream to the datasource or retrieve a stream from the datasource or just make a blob in general?

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();
}

Comments

I am so sorry about the syntax error. I Edited my post