Save blob using stream from EJB to database (in memory efficient mode)

I want to save large attachments (500 MB, possibly, if possible, even> 2 GB) in the database. I know that the pros and cons of this matter are discussed very often, but this is not at the center of my question.

The traditional way to store Blob fields in EJB3 with JPA is to use the code as follows:

@Lob private byte[] data; 

This becomes a problem when working with huge data sources, since the entire array of bytes is stored in memory.

I tried changing it to Blob:

 @Lob private Blob data; 

But this leads to the same problem. On call

 // session: Hibernate session. // Actually, I'd like to stay with JPA abstraction and not // reference Hibernate directly, if possible Blob data = session.createBlob(inputStream, lengthOfStream); 

The createBlob method creates a byteArray from inputStream.

Due to ORM mapping, I also wonder how to handle data insertion. One idea was to create a variable entity

 @Lob private byte[] data; 

That I will never use. Thus, the database schema will be built. But since the @Lob annotation is lazy, it will not inflate my memory.

And then write

 entityManager.persist(dataObject); // The following lines are _completely_ imaginatory!! query.prepare("update DataObject d set d.data = :dataInputStream where d = :dataObject"); query.setProperty("dataObject", dataObject); query.setProperty("dataInputStream", someDataInputStream); 

One solution that I came across looks beautiful, but doesn't use JPA: The easiest way to store BLOBs in a database?

Does anyone have any advice on how to do this?

+7
source share
1 answer

@Lob annotation can be applied to a Serializable object. Then you can declare:

 @Lob private MySmartLOB data; 

and

 public class MySmartLOB implements Serializable { private void writeObject(java.io.ObjectOutputStream out) throws IOException { // tranfer data from local storage to 'out' } private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException { // tranfer data from 'in' to local storage } } 

This might work, hoping the underlying JPA implementation is smart enough to deliver an ObjectOutputStream object directly linked to a JDBC stream, rather than some ugly ByteArrayOutputStream.

+1
source

All Articles