user1284566
user1284566

Reputation: 167

java - write two files atomically

I am facing a problem for which I don't have a clean solution. I am writing a Java application and the application stores certain data in a limited set of files. We are not using any database, just plain files. Due to some user-triggered action, certain files needs to be changed. I need this to be a all-or-nothing operation. That is, either all files are updated, or none of them. It is disastrous if for example 2 of the 5 files are changed, while the other 3 are not due to some IOException.

What is the best strategy to accomplish this? Is embedding an in-memory database, such as hsqldb, a good reason to get this kind of atomicity/transactional behavior?

Thanks a lot!

Upvotes: 7

Views: 1978

Answers (5)

user1200281
user1200281

Reputation: 13

My approach would be to use a lock, in your java code. So only one process could write some file at each time. I'm assuming your application is the only which writes the files. If even so some write problem occurs to "rollback" your files you need to save a copy of files like upper suggested.

Upvotes: 1

John Ericksen
John Ericksen

Reputation: 11113

Ive used the apache commons transactions library for atomic file operations with success. This allows you to modify files transactionally and potentially roll back on failures.

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

Upvotes: 1

Jonatan
Jonatan

Reputation: 2886

Can't you lock all the files and only write to them once all files have been locked?

Upvotes: 0

Mike Samuel
Mike Samuel

Reputation: 120586

What is the best strategy to accomplish this? Is embedding an in-memory database, such as hsqldb, a good reason to get this kind of atomicity/transactional behavior?

Yes. If you want transactional behavior, use a well-tested system that was designed with that in mind instead of trying to roll your own on top of an unreliable substrate.


File systems do not, in general, support transactions involving multiple files.

Non-Windows file-systems and NTFS tend to have the property that you can do atomic file replacement, so if you can't use a database and

  • all of the files are under one reasonably small directory
  • which your application owns and
  • which is stored on one physical drive:

then you could do the following:

  1. Copy the directory contents using hard-links as appropriate.
  2. Modify the 5 files.
  3. Atomically swap the modified copy of the directory with the original

Upvotes: 2

Sully
Sully

Reputation: 14943

A safe approach IMO is:

  1. Backup
  2. Maintain a list of processed files
  3. On exception, restore the ones that have been processed with the backed up one.

It depends on how heavy it is going to be and the limits for time and such.

Upvotes: 5

Related Questions