0

We have implemented the ItemProcessListener and the SkipListener in the Batch job, which is using the Spring batch. We are able to log the skipped items in the database, without creating a separate transaction. But the when the onProcessError method is invoked in the ItemProcessListener, the transaction is rolled back, due to the corresponding Runtime Exception.

We used @Transactional and propagation as REQUIRES_NEW, on the service interface for DB update, but it still rolled back the transaction.

Our objective is to log the exception details in the database whenever there is an error in process or writer components and the batch fails. As explained above, the logging is not working when we fire a DB insert from the onProcessError method or onWriteError method in the overridden listener. The transaction is rolled back.

We tried creating a new transaction using annotation on onProcessError , but it failed. Kindly provide some inputs for the same.

Hope this makes the problem clear.

4

1 回答 1

1

The Spring configuration requires us to enable the annotations. The annotations can be enabled by using the tx schema in the applicationContext.xml

As per spring documentation, http://docs.spring.io/spring/docs/2.5.6/reference/transaction.html#transaction-declarative-annotation. We must include the following namespaces,

xmlns:tx="http://www.springframework.org/schema/tx"
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd

Since the itemProcessorListener's onProcessError method is executed in the same transaction as the chunk which was being processed, the method is invoked before the RollBack. Handling the transaction using @Transactional (propagation = Propagation.REQUIRES_NEW) causes the new transaction to be created and the data persisted in the database.

于 2013-10-02T17:39:33.933 回答