Hibernate - Out of memory inserting a large block

In POJO, I have:

public void setBlob(InputStream in, Long l) { this.blob = Hibernate.getLobCreator(SessionFactoryHelper.sessionFactory.getCurrentSession()).createBlob(in, l); } 

I made hibernate.jdbc.use_streams_for_binary true in config, but did it really change something?

when saving the object, on session.flush () I get:

  org.hibernate.engine.jdbc.batch.internal.AbstractBatchImpl release INFO: HHH000010: On release of batch it still contained JDBC statements Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space Heap dump file created [46180174 bytes in 1.662 secs] at java.util.Arrays.copyOf(Arrays.java:2271) at java.io.ByteArrayOutputStream.grow(ByteArrayOutputStream.java:113) at java.io.ByteArrayOutputStream.ensureCapacity(ByteArrayOutputStream.java:93) at java.io.ByteArrayOutputStream.write(ByteArrayOutputStream.java:140) at org.hibernate.type.descriptor.java.DataHelper.extractBytes(DataHelper.java:171) at org.hibernate.type.descriptor.java.BlobTypeDescriptor.unwrap(BlobTypeDescriptor.java:121) at org.hibernate.type.descriptor.java.BlobTypeDescriptor.unwrap(BlobTypeDescriptor.java:45) at org.hibernate.type.descriptor.sql.BlobTypeDescriptor$4$1.doBind(BlobTypeDescriptor.java:105) at org.hibernate.type.descriptor.sql.BasicBinder.bind(BasicBinder.java:92) at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:305) at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:300) at org.hibernate.type.AbstractSingleColumnStandardBasicType.nullSafeSet(AbstractSingleColumnStandardBasicType.java:57) at org.hibernate.persister.entity.AbstractEntityPersister.dehydrate(AbstractEntityPersister.java:2599) at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2853) at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3291) at org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:88) at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:362) at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:354) at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:275) at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:326) at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:52) at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1127) 

Sleep Mode 4.1. Why does sleep mode decompress a stream in bytes? How can I make hibernate use the prepared setBinaryStream statement? Actually need help with this.

+4
source share
2 answers

I believe the problem is that your object in memory still has an array of bytes. This is a separate issue from whether the stream is used to store it in the database.

I suggest a workaround: do not store blob in POJO. Store the blob separately, either in the database using direct JDBC or on disk, and then just save the link to blob in POJO (either the primary key of the database or the path to the file / file name).

Then, when you need blob back, get a link from POJO and use thread based methods to return them.

This works more to get / save a blob every time you need, but if the blob is too big for memory, you may have no other choice.

+1
source

I do not know why it does not work, and it is necessary, but this is what I did to save blob in the database using Hibernate:

in POJO mapped to table:

  private byte[] serializedDocx; // + getter and setter 

in the method:

  ... SessionFactory mf = ... sess = mf.openSession(); //open connection sess.beginTransaction(); ... ByteArrayOutputStream docxBos = new ByteArrayOutputStream(); [write to docxBos] someEntity.setDocx(docxBos.toByteArray()); sess.save(someEntity); 

in myEntity.hbm.xml

  <property name="serializedDocx" type="binary"> <column name="serialized_docx" not-null="true"/> </property> 

in MySQL:

 CREATE TABLE IF NOT EXISTS `docx` ( `serialized_docx` longblob NOT NULL, ... 

NTN.

0
source

Source: https://habr.com/ru/post/1400265/


All Articles