Spring, Hibernate, Blob lazy loading

Posted by Alexey Khudyakov on Stack Overflow See other posts from Stack Overflow or by Alexey Khudyakov
Published on 2010-04-09T06:26:38Z Indexed on 2010/04/09 6:33 UTC
Read the original article Hit count: 235

Filed under:

Dear Sirs, I need help with lazy blob loading in Hibernate. I have in my web application these servers and frameworks: MySQL, Tomcat, Spring and Hibernate.

The part of database config.

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
    <property name="user" value="${jdbc.username}"/>
    <property name="password" value="${jdbc.password}"/>
    <property name="driverClass" value="${jdbc.driverClassName}"/>
    <property name="jdbcUrl" value="${jdbc.url}"/>

    <property name="initialPoolSize">
    <property name="minPoolSize">
    <property name="maxPoolSize">
    <property name="acquireRetryAttempts">
    <property name="acquireIncrement">
    <property name="idleConnectionTestPeriod">
    <property name="maxIdleTime">
    <property name="maxConnectionAge">
    <property name="preferredTestQuery">
    <property name="testConnectionOnCheckin">

<bean id="lobHandler" class="org.springframework.jdbc.support.lob.DefaultLobHandler" />

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="configLocation" value="/WEB-INF/hibernate.cfg.xml" />
    <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
    <property name="hibernateProperties">
            <prop key="hibernate.dialect">${hibernate.dialect}</prop>
    <property name="lobHandler" ref="lobHandler" />

<tx:annotation-driven transaction-manager="txManager" />

<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />

The part of entity class

@Column(name = "BlobField", columnDefinition = "LONGBLOB")
@Type(type = "org.springframework.orm.hibernate3.support.BlobByteArrayType")
private byte[] blobField;

The problem description. I'm trying to display on a web page database records related to files, which was saved in MySQL database. All works fine if a volume of data is small. But the volume of data is big I'm recieving an error "java.lang.OutOfMemoryError: Java heap space" I've tried to write in blobFields null values on each row of table. In this case, application works fine, memory doesn't go out of. I have a conclusion that the blob field which is marked as lazy (@Basic(fetch=FetchType.LAZY)) isn't lazy, actually! How can I solve the issie? Many thanks for advance.

© Stack Overflow or respective owner

Related posts about spring

Related posts about hibernate