Search code examples
javatransactions

Transaction mode for file operations in Java


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.


Solution

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

    An archived article detailed its use with the file system.

    update

    Be aware that the status on the front page says:

    We have decided to move the project to dormant as we are convinced that the main advertised feature transactional file access can not be implemented reliably. We are convinced that no such implementation can be possible on top of an ordinary file system. Although there are other useful parts (as multi level locking including deadlock detection) the transactional file system is the main reason people use this library for. As it simply can not be made fully transactional, it does not work as advertised.