Java - write two files atomically

I ran into a problem for which I do not have a clean solution. I am writing a Java application and the application stores certain data in a limited set of files. We do not use any database, just files. Due to some user initiated actions, some files need to be changed. I need this to be an all-or-nothing operation. That is, either all files are updated, or none of them. This is catastrophic if, for example, 2 out of 5 files are changed, and the remaining 3 are not due to some kind of IOException exception.

What is the best strategy for this? Is embedding a database in memory like hsqldb a good reason to get this kind of atomicity / transactional behavior?

Thanks a lot!

+6
source share
5 answers

Safe IMO Approach:

  • Backup
  • Keeping a list of processed files
  • In case of an exception, restore those that were processed using the backup.

It depends on how heavy it is, and for time limits, etc.

+5
source

What is the best strategy for this? Is embedding a database in memory like hsqldb a good reason to get this kind of atomicity / transactional behavior?

Yes. If you require transactional behavior, use a well-tested system that has been designed with this in mind, rather than trying to topple your own on top of an untrusted substrate.


File systems generally do not support multi-file transactions.

Non-Windows file systems and NTFS typically have the property that you can replace an atomic file, so if you cannot use a database and

  • all files are in one fairly small directory
  • who owns your application and
  • which is stored on one physical disk:

then you can do the following:

  • Copy directory contents using hard links.
  • Change 5 files.
  • Atomic replacement of a modified copy of the catalog with the original
+2
source

Ive successfully used the apache transaction library for transactions with atomic files. This allows you to modify files in a transaction and potentially roll back from failures.

Here's the link: http://commons.apache.org/transaction/

+1
source

My approach would be to use locks in your Java code. Thus, only one process could write a file at a time. I assume that your application is the only one who writes files. If even in this case there is a problem with recording "rollback" of your files, you need to save a copy of the files, for example, the one suggested above.

+1
source

Can you lock all the files and write them only after all the files are locked?

0
source

Source: https://habr.com/ru/post/911362/


All Articles