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
EntityCode:
@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;
ApplicationCode:
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.xmlCode:
<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>