views:

658

answers:

5

Hi there,

Perhaps what I'm trying to explain here doesn't make any sense, so I'd like to apologize in advance. Anyway, I will try.

I'm trying to read from a file, perform some database operations and move the content to another file. I was wondering if it is possible to perform all this operations in an atomic way in Java, so if anything goes wrong in the list of actions, rollback the complete sequence and go back to the start point.

Thanks in advance for your help.

+6  A: 

Take a look at Apache Commons Transaction. It has the capability to manage files transactionally.

Here's an article detailing its use with file systems.

Brian Agnew
Nicer link than mine
pjp
Thanks for pointing me to right direction!
Giroscopio
+2  A: 

There is no standard Transaction File API however I beleive that there is an Apache project that implements what you want.

http://commons.apache.org/transaction/file/index.html

The transactional file package provides you with code that allows you to have atomic read and write operations on any file system. The file resource manager offers you the possibility to isolate a number of operations on a set of files in a transaction. Using the locks package it is able to offer you full ACID transactions including serializability. Of course to make this work all access to the managed files must be done by this manager. Direct access to the file system can not be monitored by the manager.

pjp
Thanks for pointing me to right direction!
Giroscopio
+1  A: 

No, at least not with a simple call. Filesystems in general (and Java filesystem operations in particular) do not support a "rollback".

You could however emulate this. A common way would be to first rename the file so that it is marked as being "in processing". Append some suffix for example.

Then process it, then change the file. If anything goes wrong, just rollback the DB, rename all the file(s) with suffixes back to their original names and you're set.

As a bonus, on some FS a rename is even atomic, so you'd be safe even with concurrent updates (don't know if this is relevant for you). I do not know whether file renaming is atomic in Java though; you'd need to check.

sleske
It seems a quite straight forward solution. I like KISS options so I'll try it.
Giroscopio
A: 

You can coordinate a distributed transaction using Two-Phase Commit. However, this is fairly complex to implement and an approach I've often seen taken is to use single-phase commit, building a stack of transactions and then committing them all in quick succession, generating an error if one of the commit attempts fails but others succeed.

If you chose to implement Two-Phase Commit you'd require Write-Ahead Logging for each participant in the transaction, where you log actions before you've taken them, allowing you to roll back any changes if the transaction fails. For example, you'd need to do this in order to potentially reverse any changes made to files (as sleske mentions).

Adamski
A: 

Hello, Recently, I have been working on a project that can hopefully match the requirement posted

above. This project, called XADisk, is hosted on an open source community (java.net) as

https://xadisk.dev.java.net/.

Thanks, Nitin.

nitin