Results 1 to 4 of 4

Thread: ItemWriter rollback

Hybrid View

  1. #1

    Default ItemWriter rollback

    Hello,

    I have a ItemWriter using a Dao to insert records in database.
    I built the writer like the TradeWriter from Spring-batch samples and use an existing dao (from my projet) based on Hibernate.
    Everything works fine and i really appreciate Spring-batch
    However, there is a problem when i try to test an exception from the writer.
    Chunk commit interval = 100
    I increment a counter for each item write in DB and send Exception after 40 records.

    The BATCH_STEP_EXECUTION (which is correct) indicates :
    READ_COUNT=100,
    WRITE_COUNT=0,
    ROLLBACK_COUNT=1

    But I still have 40 records in my database ... which means that the rollback didn't run through the dao.
    Do you have a clue about what i did wrong ???
    Thanks for help

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

    Default

    Can you show us some more detail? How did you configure the transaction manager? How did you implement the writer?

  3. #3
    Join Date
    Dec 2012
    Posts
    2

    Default

    Quote Originally Posted by LionelFromParis View Post
    Hello,

    I have a ItemWriter using a Dao to insert records in database.
    I built the writer like the TradeWriter from Spring-batch samples and use an existing dao (from my projet) based on Hibernate.
    Everything works fine and i really appreciate Spring-batch
    However, there is a problem when i try to test an exception from the writer.
    Chunk commit interval = 100
    I increment a counter for each item write in DB and send Exception after 40 records.

    The BATCH_STEP_EXECUTION (which is correct) indicates :
    READ_COUNT=100,
    WRITE_COUNT=0,
    ROLLBACK_COUNT=1

    But I still have 40 records in my database ... which means that the rollback didn't run through the dao.
    Do you have a clue about what i did wrong ???
    Thanks for help


    Did you find a solution? i have the exact same problem.

    Since you were asked for details, i will provide mines since the problem is very similar.

    This is my writer:


    @Component("transactionItemWriter")
    public class TransactionInteracItemWriter implements ItemStreamWriter<TransactionConciliationInterac> {
    private static final Logger log = Logger.getLogger(TransactionInteracItemWriter.clas s);
    @Autowired
    private ConciliationContext conciliationContext;
    @Autowired
    private ServiceTransactionConciliationInterac paiementService;
    private int transactionCount;

    /**
    * @see ItemWriter#write(java.util.List)
    */
    public void write(List<? extends TransactionConciliationInterac> data) throws Exception {

    int index = ++transactionCount - 1;
    //List<TransactionConciliationInterac> trxs = data.get(0).getPayments();

    while (index < data.size()) {
    TransactionConciliationInterac trx = data.get(index);
    paiementService.insertAndReturnKey(trx,conciliatio nContext.getIdSommaire().toString());
    index = ++transactionCount - 1;
    }
    }

    the paiementService is a spring service bean using JDBCTemplate.

    Here is the config:


    <batch:step id="traitementXmlIemtTransactionStep">
    <batch:tasklet transaction-manager="transactionManager" start-limit="10">
    <batch:chunk reader="IemtXmlTransactionReader" processor="IemtXmlTransactionConvertProcessor" writer="transactionItemWriter" commit-interval="5"/>
    <batch:transaction-attributes isolation="DEFAULT" propagation="REQUIRED"/>
    <batch:listeners>
    <batch:listener ref="transactionStepListener" />
    </batch:listeners>

    </batch:tasklet>
    </batch:step>

    I have set a commit-interval = 5 and in my xml read, i have intentionally entered bad data in the 5th element.

    From the output of Spring Batch, everything look great :

    Stacktrace of the exception:

    2012-12-11 09:50:28,936 DEBUG [org.springframework.batch.core.step.tasklet.Taskle tStep] - <Rollback for RuntimeException: org.springframework.dao.DataIntegrityViolationExce ption: PreparedStatementCallback; SQL []; Data truncation: Data too long for column 'TRANSACTIONTYPE' at row 1; nested exception is com.mysql.jdbc.MysqlDataTruncation: Data truncation: Data too long for column 'TRANSACTIONTYPE' at row 1>
    2012-12-11 09:50:28,949 DEBUG [org.springframework.batch.repeat.support.RepeatTem plate] - <Handling exception: org.springframework.dao.DataIntegrityViolationExce ption, caused by: org.springframework.dao.DataIntegrityViolationExce ption: PreparedStatementCallback; SQL []; Data truncation: Data too long for column 'TRANSACTIONTYPE' at row 1; nested exception is com.mysql.jdbc.MysqlDataTruncation: Data truncation: Data too long for column 'TRANSACTIONTYPE' at row 1>
    2012-12-11 09:50:28,949 DEBUG [org.springframework.batch.repeat.support.RepeatTem plate] - <Handling fatal exception explicitly (rethrowing first of 1): org.springframework.dao.DataIntegrityViolationExce ption: PreparedStatementCallback; SQL []; Data truncation: Data too long for column 'TRANSACTIONTYPE' at row 1; nested exception is com.mysql.jdbc.MysqlDataTruncation: Data truncation: Data too long for column 'TRANSACTIONTYPE' at row 1>
    2012-12-11 09:50:28,950 ERROR [org.springframework.batch.core.step.AbstractStep] - <Encountered an error executing the step>
    org.springframework.dao.DataIntegrityViolationExce ption: PreparedStatementCallback; SQL []; Data truncation: Data too long for column 'TRANSACTIONTYPE' at row 1; nested exception is com.mysql.jdbc.MysqlDataTruncation: Data truncation: Data too long for column 'TRANSACTIONTYPE' at row 1
    at org.springframework.jdbc.support.SQLStateSQLExcept ionTranslator.doTranslate(SQLStateSQLExceptionTran slator.java:101)


    Status of the step:

    <Step execution complete: StepExecution: id=3, version=2, name=traitementXmlIemtTransactionStep, status=FAILED, exitStatus=FAILED, readCount=5, filterCount=0, writeCount=0 readSkipCount=0, writeSkipCount=0, processSkipCount=0, commitCount=0, rollbackCount=1>

    Now i see that 5 elements were read, nothing was written and 1 rollback occurred... GREAT...

    BUT

    If i look in the database, the first 4 elements were committed!!!!!!!!!
    Last edited by Cygnusx1; Dec 11th, 2012 at 09:25 AM.

  4. #4
    Join Date
    Dec 2012
    Posts
    2

    Default

    Ok i found the problem. I was using MySql DB to run my batch and the schema was created with the default MyISam engine!!!!

    I found out this engine auto commit by default...so i changed the engine to innoDB and everything work great

Posting Permissions

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