Results 1 to 6 of 6

Thread: Transaction Management

  1. #1

    Default Transaction Management

    Hi
    Im using hibernate and DB2 in my application.
    I've downloaded sample Workspace and tried implementing Spring Batch.
    I found that based on chunk size transactions are committed.
    Is there any way to maintain transaction in Step level?
    My requirement is transaction should rollback if there is any exception during execution of a step.
    It should not be based on chunk size.

    Im inserting into three tables in each step.
    Im calling session.flush for each insertion.
    When there is some exception in the third insertion, insertion happening in first two tables.
    I want to rollback the whole transaction if there is any exception in any one of the insertions.

  2. #2
    Join Date
    Dec 2006
    Posts
    1,061

    Default

    I'm a bit confused here, since that's the way the framework already works. Commits are done at 'chunk boundaries', that's true. However, if you throw an exception from an ItemWriter, the transaction will be rolledback immeadiately.

    One thing that might be causing you some confusion is flush policies. The problem is that hibernate generally flushes on commit, so you won't see any errors until all of the items in the chunk are written out simultaneously. The HibernateAwareItemWriter attempts to solve this by rollingback after the flush on commit, and then committing after each item. Another solution is to flush eagerly, after each write, but it all depends upon how you've configured Hibernate.
    Last edited by lucasward; May 30th, 2008 at 12:13 PM. Reason: transposed item and transaction

  3. #3

    Default

    Lucas,

    Thanks for the reply.
    Let me know whether i've gone wrong anywhere in the below implementations.

    Itemwriter

    public void writeObject(Object obj) {
    Session session = null;
    session = getHibernateTemplate().getSessionFactory().openSes sion();
    session.saveOrUpdate(obj);
    session.flush();
    session.clear();
    session.close();

    }

    *********
    datasource context
    **********
    <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryB ean">
    <property name="jndiName" value="jdbc/OMP"/>
    <property name="lookupOnStartup" value="false"/>
    <property name="cache" value="true" />
    <property name="proxyInterface" value="javax.sql.DataSource" />
    </bean>


    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSes sionFactoryBean">
    <property name="mappingResources">
    <list>
    <value>Repository.hbm.xml</value>
    </list>
    </property>
    <property name="hibernateProperties">
    <props>
    <prop key="hibernate.dialect">org.hibernate.dialect.DB23 90Dialect</prop>
    <prop key="hibernate.show_sql">true</prop>
    <prop key="hibernate.connection.pool_size">10</prop>
    <prop key="hibernate.jdbc.batch_size">50</prop>
    <!--<prop key="transaction.factory_class">org.hibernate.tran saction.JDBCTransactionFactory</prop>-->
    <prop key="hibernate.cache.provider_class">org.hibernate .cache.HashtableCacheProvider</prop>
    <prop key="hibernate.connection.isolation">2</prop>
    </props>
    </property>
    <property name="dataSource" ref="dataSource" />
    </bean>
    <bean id="sqlTransactionManager" class="org.springframework.orm.hibernate3.Hibernat eTransactionManager" lazy-init="true">
    <property name="sessionFactory" ref="sessionFactory" />
    </bean>

  4. #4
    Join Date
    Dec 2006
    Posts
    1,061

    Default

    If you're manually calling flush in your ItemWriter, then any problems should throw exceptions there, and as mentioned above, any exceptions thrown from write will cause the exception to be rolledback, and if you have configured it to skip (and have a correct equals implementation) the framework should recognize the item and not give it back to the writer. You may want to do some testing to ensure that the exception is actually being thrown on write though.

  5. #5
    Join Date
    Jun 2005
    Posts
    4,230

    Default

    If you use SessionFactory.openSession() you throw away the benefits of Spring-managed transactions. You should always use getCurrentSession() with the HibernateTransactionManager.

  6. #6

    Default

    Dave/Lucas,

    Thanks a lot for your replies.
    I used sessionFactory.getCurrentSession(), now its getting rolled back perfectly :-)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •