Spring – Hibernate spring annotation sessions not being closed/flushed

annotationshibernatesessionspring

I've 'inherited' a project which uses Spring annotations to manage transactions/sessions with Hibernate. Or at least it's meant to be. Currently the Hibernate sessions never get flushed (they are set to FLUSH_MODE_NEVER) and the DAO's need to be flushed by hand for any data to be written to the database.

Also all the DTO objects stay resident in hibernate's memory, eventually leading to an OutOfMemory error.

I believe I need to tell Spring/Hibernate to close the session or commit the transaction. In my controller class I have the annotated method for handling the requests:

@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
    ...
    ...
}

and in the applicationContetxt.xml file I believe I setup the hibernate transaction manager and tell spring to use the annotations:

<!-- hibernate3 transaction manager -->
<bean id="transactionManagerLocal" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="dataSource" ref="${local.data.source}" />
    <property name="sessionFactory" ref="localSessionFactory" />
</bean>

<!-- Demarcate using @Transactional annotation -->
<tx:annotation-driven transaction-manager="transactionManagerLocal" order="200" />

Not only am I pretty sure that the config is wrong by the way data doesn't get written to the DB without manually calling flush on each of the DAO's, from the log file we can see that transaction manager has it's flushing and session closing disabled:

INFO  [http-8080-2] TransactionFactoryFactory - Transaction strategy: org.springframework.orm.hibernate3.SpringTransactionFactory
INFO  [http-8080-2] TransactionManagerLookupFactory - No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
INFO  [http-8080-2] SettingsFactory - Automatic flush during beforeCompletion(): disabled
INFO  [http-8080-2] SettingsFactory - Automatic session close at end of transaction: disabled

What needs to be done to make Spring/hibernate automatically flush the DAOs and/or close the session to prevent huge amounts of memory being used by Hibernate?

cheers
Dan

<!-- MySQL/InnoDB session factory -->
<bean id="localSessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="${local.data.source}"/>
    <property name="mappingResources">
        <list>
            <value>net/company/projectname/domain/ExchangeRate.hbm.xml</value>
            <!-- Other  -->

        </list>
    </property>

   <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</prop>
            <prop key="hibernate.cache.provider_class">org.hibernate.cache.NoCacheProvider</prop>
            <prop key="hibernate.connection.zeroDateTimeBehavior">convertToNull</prop>
            <!-- 
            DOES NOTHING
            <prop key="hibernate.transaction.flush_before_completion">true</prop>
            <prop key="hibernate.transaction.auto_close_session">true</prop>
            -->
        </props>
    </property>
</bean>

<!-- hibernate3 transaction manager -->
<bean id="transactionManagerLocal" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="dataSource" ref="${local.data.source}" />
    <property name="sessionFactory" ref="localSessionFactory" />
</bean>


<!-- Demarcate using @Transactional annotation -->
<tx:annotation-driven transaction-manager="transactionManagerLocal" order="200" />

<bean id="exchangeRateDAO" class="net.company.project.dao.hibernate.impl.ExchangeRateDAOImpl">
     <property name="sessionFactory" ref="localSessionFactory"/>
 </bean>     

Best Answer

One of my cleverer colleagues discovered what the problem was.

The actual problem was that the method that you've declared as @Transactional is an inherited method that is called from a base class, which means that Spring is unable to intercept calls to the method and wrap it in a transaction.

Spring implements transaction management as aspects, and aspects are implemented with proxies. The limitation of this is that if an object calls a method on itself (which is what's happening here because of inheritance) then the proxy doesn't see the call (because it happens internally within the class, like calling a private method), and can't so anything about it.

Which makes sense but seems to be incredibly dangerous as it fails to write any data without any error message or warning.

Related Topic