Search code examples
javagrailsgroovy

creating blob - java heap space


I'm passing a multipartFile from controller to service and inside the service trying to create Blob from this multipartFile. The problem is that the files I'm using are really big (4-5 gb ) and I get "java heap space" error ( no I cannot allocate more memory to the application)

Is there any way to stream it or sth? I'm using java 7

service code:

def uploadFile(MultipartFile file){
    Sql sql = utilsService.retrieveSQLFromDataSource()
    Blob blob = dataSource.connection.createBlob()
    blob.setBytes(1, file.bytes)
    ...
}

Solution

  • Here is the relevant part - I'm simulation a large BLOB by writing repeatedly a small chunk.

    The important part ist the creation of the temporary BLOB.

    def bytes = data.getBytes()
    BLOB tempBlob = BLOB.createTemporary(con, true, BLOB.DURATION_SESSION);
    def os = tempBlob.setBinaryStream(0);
    20000.times { 
     os.write(bytes,0,bytes.size())
    }
    os.close()
    int len = tempBlob.length()
    println "length $len"
    
    stmt.setBLOB (1,tempBlob)
    
    stmt.execute()