0

In my use case, i would like to use a buffer writer to handle the storing of strings.

  • Only when the commit interval has been met, it will flush the writes

This is to ensure that when there is a error at reading, the buffer writer is able to handle the roll back.

Are there any examples or help on this, as it is my first time doing spring boot applications.

Thanks!

1 Answer 1

0

Unless you have a transactional file system, you cannot rollback a disk flush operation. Transactional file systems have never been mainstream because of the difficulty of the problem. Microsoft attempted to provide one, but this was abandoned quickly. There used be a couple of APIs for that as well, like Apache commons-transaction in the Java world, but those are not maintained anymore due to the lack of transactional file systems.

ACID compliant databases are the way to go to add transactional semantics on top of regular file systems. SQLite is the most widely used one, which is an amazing piece of engineering IMO. You can see how SQLite can be used as an application file format.

That's why Spring Batch cannot really rollback a flush operation, which brings me to the next point of how it handles rollbacks..

i would like to use a buffer writer to handle the storing of strings. Only when the commit interval has been met, it will flush the writes

This is already implemented in the FlatFileItemWriter provided by Spring Batch, which uses a TransactionawareBufferedWriter for that by default. This buffered writer is aware of the currently active transaction, and buffers items before flushing them to disk when the chunk size is met.

In the unlikely case where the transaction is rolled back after the buffer is flushed, the job will be marked as failed and you can restart it. On the restart, Spring Batch will truncate the corrupted file to the last known "good" byte offset, and restart a clean write of the last failed chunk, then continue from there.

Sign up to request clarification or add additional context in comments.

2 Comments

What if I am just trying to store lines being read by the item reader. So that once the commit interval is being met then I flush out what was in the buffer writer
This is what I'm saying, the provided writer already does that so you don't have to, see how it stores/buffers items here: github.com/spring-projects/spring-batch/blob/… and then flushes the buffer to disk here: github.com/spring-projects/spring-batch/blob/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.