Unable to log a error record in DB within ItemProcessListener - onProcessError

北战南征 提交于 2021-02-08 11:51:13

问题


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.


回答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.



来源:https://stackoverflow.com/questions/19131204/unable-to-log-a-error-record-in-db-within-itemprocesslistener-onprocesserror

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!