Jun 13th, 2011, 10:44 AM
Listen to commit errors?
Is there any listener that i can register to have access to the exception (preferably items that were part of the transaction too) while committing the transaction?
ItemWriterListener and ChunkListener error methods are called before the transaction is commited, so how can i report/log errors while committing?
Jun 14th, 2011, 12:59 AM
you can do something before and after commit, but not *while* committing. Spring Batch usually tries to call the listeners as close to the commit as possible, so such callbacks (before the commit) should be enough most of the time. Moreover, an error triggers a rollback (so no commit). What is your scenario exactly?
Jun 14th, 2011, 08:31 AM
I have my job configured to use a reader/processor/writer. Currently i have a commit size of 50 and i dont have any retry or non-rollback exceptions configured. Eventually i will set up retry to 2 probably but non-rollback exceptions will remain empty.
As for listeners i have jobListener, stepexecutionlistener, itemreadlistener, itemprocesslistener, itemwritelistener and skiplistener configured.
I am using hibernate.
What i want to achieve is to be able to log the exception when the application fails to commit the transaction, because currently if that happens none of my listeners works, they are all executed before the commit and since im filtering out all springbatch logs when this happens my job just stops executing without showing or printing anything, which is another concern, i think it should be able to at least keep processing the rest of the items (even if the current chunk failed).
I can post any code/config you need, just let me know.
Thanks in advance
Jun 14th, 2011, 08:48 AM
your listeners log something in the database and they get rollback with the whole chunk, right? If so, use PROPAGATION_REQUIRES_NEW as the propagation level in these listeners. Their work won't be rolled back then.
If you want to keep on processing items on an error, you should configure skippable exceptions.
Jun 14th, 2011, 09:20 AM
Thanks for the quick answer.
Currently the listeners only write somethign to a log file, but yea eventually they will write to a DB log/audit table.
The problem with that is if I log that X records were written to the DB and then the transaction rolls back i have incorrect log/audit information because in reality nothing was written.
Jun 14th, 2011, 10:20 AM
ok, so your problem will disappear as soon as you use the DB to log read/processed/written items. If you want to stick to a log file, you'll need to flush your logs after the commit, for example after the chunk completion (a chunk listener would be appropriate in this case).