andrewinblue
andrewinblue

Reputation: 31

Spring Batch CompositeItemWriter Transaction Roll back issue

All, I am experiencing a transaction roll back issue using Spring Batch CompositeItemWriter. The version of Spring batch is 2.2.7.

My configuration is identical to a post here : https://stackoverflow.com/questions/32432519/compositeitemwriter-doesnt-roll-back-in-spring-batch

<bean id="compositeItemWriter" class="org.springframework.batch.item.support.CompositeItemWriter">
    <property name="delegates">
    <list>
        <ref bean="firstItemWriter"/>
        <ref bean="secondItemWriter"/>
        <ref bean="thirdItemWriter"/>
    </list>
    </property>
</bean>

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" >
    <property name="dataSource" ref="dataSource"/>
</bean> 

<bean id="dataSource"
    class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="${batch.jdbc.driver}" />
    <property name="url" value="${batch.jdbc.url}" />
    <property name="username" value="${batch.jdbc.user}" />
    <property name="password" value="${batch.jdbc.password}" />
</bean>

When there's an exception thrown at thirdItemWriter, data wrote to database by previous writers are not rolling back.

Not sure what I am missing, but my understanding is the composite item writers share the same transaction, all data should be rolled back in the event of exception.

Any suggestion is appreciated

Upvotes: 1

Views: 3314

Answers (2)

julie
julie

Reputation: 1

make a custom compositeItemWrite. '@Transactional' works to my code.

package com.krafton.intraplatform.batch.job.step;

import java.util.Iterator;
import java.util.List;

import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.item.ItemStream;
import org.springframework.batch.item.ItemStreamException;
import org.springframework.batch.item.ItemStreamWriter;
import org.springframework.batch.item.ItemWriter;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

public class CustomCompositeWriter<T> implements ItemStreamWriter<T>, InitializingBean {
  private List<ItemWriter<? super T>> delegates;
  private boolean ignoreItemStream = false;

  public CustomCompositeWriter() {
  }

  public void setIgnoreItemStream(boolean ignoreItemStream) {
    this.ignoreItemStream = ignoreItemStream;
  }


  @Transactional
  public void write(List<? extends T> item) throws Exception {
    Iterator var2 = this.delegates.iterator();

    while(var2.hasNext()) {
      ItemWriter<? super T> writer = (ItemWriter)var2.next();
      writer.write(item);
    }

  }

  public void afterPropertiesSet() throws Exception {
    Assert.notNull(this.delegates, "The 'delegates' may not be null");
    Assert.notEmpty(this.delegates, "The 'delegates' may not be empty");
  }

  public void setDelegates(List<ItemWriter<? super T>> delegates) {
    this.delegates = delegates;
  }

  public void close() throws ItemStreamException {
    Iterator var1 = this.delegates.iterator();

    while(var1.hasNext()) {
      ItemWriter<? super T> writer = (ItemWriter)var1.next();
      if (!this.ignoreItemStream && writer instanceof ItemStream) {
        ((ItemStream)writer).close();
      }
    }

  }

  public void open(ExecutionContext executionContext) throws ItemStreamException {
    Iterator var2 = this.delegates.iterator();

    while(var2.hasNext()) {
      ItemWriter<? super T> writer = (ItemWriter)var2.next();
      if (!this.ignoreItemStream && writer instanceof ItemStream) {
        ((ItemStream)writer).open(executionContext);
      }
    }

  }

  public void update(ExecutionContext executionContext) throws ItemStreamException {
    Iterator var2 = this.delegates.iterator();

    while(var2.hasNext()) {
      ItemWriter<? super T> writer = (ItemWriter)var2.next();
      if (!this.ignoreItemStream && writer instanceof ItemStream) {
        ((ItemStream)writer).update(executionContext);
      }
    }

  }
}

Upvotes: 0

andrewinblue
andrewinblue

Reputation: 31

After few days worth of work on this, finally come with a solution. The key is put all writers in one transaction and turn off auto commit.

public class TransactionAwareCompositeItemWritter<T> extends CompositeItemWriter<T> {
private static Logger LOG = LoggerFactory.getLogger(TransactionAwareCompositeItemWritter.class);

private List<ItemWriter<? super T>> delegates;

private PlatformTransactionManager transactionManager;

private JdbcTemplate jdbcTemplate;

@Override
public void write(final List<? extends T> item) throws Exception {

    Connection con = DataSourceUtils.getConnection(jdbcTemplate.getDataSource());

    if (con.getAutoCommit()) {

        LOG.debug("Switching JDBC Connection [" + con + "] to manual commit");

        con.setAutoCommit(false);
    }

    DefaultTransactionDefinition def = new DefaultTransactionDefinition();
    def.setName("CompositeItemWriter Transaction");
    def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);

    TransactionStatus status = transactionManager.getTransaction(def);
    try {
        for (ItemWriter<? super T> writer : delegates) {
            writer.write(item);
        }
    } catch (Exception e) {
        transactionManager.rollback(status);
        throw e;
    }

    transactionManager.commit(status);

    if (!con.getAutoCommit()) {

        LOG.debug("Switching JDBC Connection [" + con + "] to auto commit");

        con.setAutoCommit(true);
    }

}

public void setDelegates(List<ItemWriter<? super T>> delegates) {
    super.setDelegates(delegates);
    this.delegates = delegates;
}

public PlatformTransactionManager getTransactionManager() {
    return transactionManager;
}

public void setTransactionManager(PlatformTransactionManager transactionManager) {
    this.transactionManager = transactionManager;
}

public JdbcTemplate getJdbcTemplate() {
    return jdbcTemplate;
}

public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
    this.jdbcTemplate = jdbcTemplate;
}

}

Upvotes: 2

Related Questions