Hibernation and Spring Batch Inserts

My application is based on Hibernate 3.2 and Spring 2.5. Here is a snippet related to transaction management from the application context:

  <tx:annotation-driven transaction-manager="txManager"/>
    <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
          <property name="sessionFactory" ref="sessionFactory"/>
          <property name="nestedTransactionAllowed" value="true"/> 
    </bean> 
    <bean id="transactionTemplate"  classs="org.springframework.transaction.support.TransactionTemplate">
           <property name="transactionManager" ref="txManager"/>
    </bean>
    <bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/>
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="configLocation" value="classpath:/hibernate.cfg.xml"></property>
    </bean>

For all DAOs, there is an appropriate class of service, and transactions are processed there, using @Transactionalfor each method at the service level. However, there is now a scenario where a method in the DAO says that "parse ()" is called from a service level. At the service level, I indicated @Transactional(readOnly=false). This analysis method in the DAO calls another method: "save ()" in the same DAO, which stores a large number of rows (about 5000) in the database. The save method is now called in a loop from the parsing function. Now the problem is that after about 100 calls to the "save" method. Sometimes I get an OutOfMemory exception or sometimes the program stops responding.

At the moment, these are the changes that I made to the save method:

Session session = getHibernateTemplate().getSessionFactory().openSession();
            Transaction tx = session.beginTransaction();

            int counter = 0;
            if(books!=null && !books.isEmpty()){
                for (Iterator iterator = books.iterator(); iterator
                        .hasNext();) {
                    Book book = (Book) iterator.next();
                    session.save(book);
                    counter++;
                    if(counter % 20==0) {
                         session.flush();
                         session.clear();
                    }
                }
            }
            tx.commit();
        session.close();

, , , . getHibernateTemplate.save(). , DAO, @Transactional(readOnly=false, PROPOGATION=NEW) save(), ?

hibernate.jdbc.batch_size 20 hibernate.cfg.

?

+5
3

StatelessSession, - , OutOfMemory, :

if (books == null || books.isEmpty) {
    return;
}
StatelessSession session = getHibernateTemplate().getSessionFactory().openStatelessSession();
Transaction tx = session.beginTransaction();

for (Book each : books) {           
    session.insert(book);           
}
tx.commit();
session.close();

StatelessSession .

+5

. Spring. sessionFactory.getCurrentSession() , Spring . , Spring , HibernateTemplate Hibernate API. SessionFactory dao- bean.

+1

parse , save , . save .

, , -, .

0
source

All Articles