-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: OutOfMemory persisting byte[] into blob
PostPosted: Thu Oct 01, 2009 6:38 am 
Newbie

Joined: Thu Feb 12, 2009 10:00 am
Posts: 18
I am trying to persist a set of object containing a byte[]. The entities are very simple and I am just looping over a fixed number of these and persist each of them. However shortly into this process I get an OutOfMemory error. It seems that the problem is with hibernate trying to keep a reference to the byte[] beyond the transaction boundary. I have tried adding a flush, but this didn't help either. So far I have encountered the same problem using DB2 and SQL Server 2005. I am running this in stand-alone mode outside any container using the following:

Hibernate Annotations 3.3.1.GA
Hibernate 3.2.5
Hibernate EntityManager 3.3.2.GA

Entity
Code:
@Entity
public class DemoBlob implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    @Lob
    @Basic(fetch = FetchType.LAZY)
    @Column(length=10485760)
    private byte[] content;


Application
Code:
factory = Persistence.createEntityManagerFactory("DataBaseTestsDB2");
EntityManager em = factory.createEntityManager();
em.getTransaction().begin();
Demo demo = new Demo();
em.persist(demo);
System.out.println("Store Entity");
em.getTransaction().commit();

int size = 1024 * 1024 * 4; //4MB
byte[] payload = new byte[size];
for (int index = 0; index < size; index++) {
    payload[index] = new Byte("2");
}

int count = 0;
em.getTransaction().begin();
for (int index = 1; index <= 100; index++) {
    DemoBlob demoBlob = new DemoBlob();
    demoBlob.setContent(payload);
    em.persist(demoBlob);
    count++;
    System.out.print(">");
    if (index>0 && index % 5 == 0) {
        em.getTransaction().commit();
        System.out.println("Stored :"+count+" Blob");
        count=0;
        em.getTransaction().begin();
    }
}
em.getTransaction().commit();
System.out.println("Stored :"+count+" Blob");


persistence.xml
Code:
  <persistence-unit name="DataBaseTestsDB2" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <class>com.demo.entities.Demo</class>
    <class>com.demo.entities.DemoBlob</class>
    <properties>
      <property name="hibernate.connection.driver_class" value="com.ibm.db2.jcc.DB2Driver"/>
      <property name="hibernate.connection.url" value="jdbc:db2://server:50000/database"/>
      <property name="hibernate.connection.username" value="user"/>
      <property name="hibernate.connection.password" value="password"/>
      <property name="hibernate.hbm2ddl.auto" value="update"/>
      <property name="hibernate.dialect" value="org.hibernate.dialect.DB2Dialect"/>
      <property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/>
    </properties>
  </persistence-unit>


Last edited by ejb3workshop.com on Thu Oct 01, 2009 7:55 am, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: OutOfMemory persisting byte[] into blob
PostPosted: Thu Oct 01, 2009 6:40 am 
Newbie

Joined: Thu Feb 12, 2009 10:00 am
Posts: 18
StackTrace
Quote:
>>>Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at org.hibernate.type.ByteArrayBlobType.deepCopy(ByteArrayBlobType.java:76)
at org.hibernate.type.TypeFactory.deepCopy(TypeFactory.java:374)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:280)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:181)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:107)
at org.hibernate.ejb.event.EJB3PersistEventListener.saveWithGeneratedId(EJB3PersistEventListener.java:49)
at org.hibernate.event.def.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:131)
at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:87)
at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:38)
at org.hibernate.impl.SessionImpl.firePersist(SessionImpl.java:618)
at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:592)
at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:596)
at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:220)
at com.demo.main.EntityTests.execute(EntityTests.java:41)
at com.demo.main.EntityTests.main(EntityTests.java:81)


Last edited by ejb3workshop.com on Thu Oct 01, 2009 8:09 am, edited 2 times in total.

Top
 Profile  
 
 Post subject: Re: OutOfMemory persisting byte[] into blob
PostPosted: Thu Oct 01, 2009 7:06 am 
Newbie

Joined: Thu Feb 12, 2009 10:00 am
Posts: 18
A similar exception happens with the latest stable release:
Hibernate Annotations 3.4.0.GA
Hibernate 3.3.0.SP1
Hibernate EntityManager 3.4.0.GA
Quote:
>>>>>Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Arrays.java:2882)
at java.lang.AbstractStringBuilder.expandCapacity(AbstractStringBuilder.java:100)
at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:390)
at java.lang.StringBuilder.append(StringBuilder.java:119)
at java.util.AbstractMap.toString(AbstractMap.java:493)
at org.hibernate.pretty.Printer.toString(Printer.java:82)
at org.hibernate.pretty.Printer.toString(Printer.java:113)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:120)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:49)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1027)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:365)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:137)
at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:54)
at com.demo.main.EntityTests.execute(EntityTests.java:46)
at com.demo.main.EntityTests.main(EntityTests.java:81)


Top
 Profile  
 
 Post subject: [RESOLVED] OutOfMemory persisting byte[] into blob
PostPosted: Thu Oct 01, 2009 9:27 am 
Newbie

Joined: Thu Feb 12, 2009 10:00 am
Posts: 18
This issue was resolved by clearing out the EntitiManagers cache using the following after committing the transation
Code:
em.getTransaction().commit();
em.clear();
em.getTransaction().begin();


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.