My project needs RandomAccessFile
,and I have made it.But when testing the Mutiple Access,many problems found.It can not make sure the file access security , no ACID semantics.So I need a framework based on RandomAccessFile
to solve this problem.
views:
41answers:
2If your model is at the right level of abstraction, declaring a few synchronized methods should take care of integrity.
If you absolutely need to share file locks between JVMs or heavy processes, you might want to look into java.util.concurrent.locks.ReentrantReadWriteLock or java.nio.channels.FileLock (from the JavaDoc):
This file-locking API is intended to map directly to the native locking facility of the underlying operating system. Thus the locks held on a file should be visible to all programs that have access to the file, regardless of the language in which those programs are written.
Oh, and Java: thread-safe RandomAccessFile definitely deserves some atttention.
What exactly are the problems you encountered? Do you need full ACID support, with concurrent transaction isolation, rollback, etc. Or do you "only" need a bit more robustness to deal with some kinds failures? The solution will depend on your requirements.
Here are nevertheless a list of frameworks that tackles the problem of transactional file system.
If your app runs in a JEE application server, you can have a look at
- JBoss Transactional File I/O, looks promising.
- Filera: File resource adapter, but I don't think it's transactional
- JCA connector: a file system adapter, my own implementation
For plain Java, you can have a look at
- commons-transactions, but I feel like the project is dead
- maybe Java Content Repository (JSR-170 and JSR-283), but it's a high-level API
Generally speaking, having transactional file system is complicated. Otherwise you can craft a design yourself to provide some robustness. Here is an answer where I sketch a design that provide usually sufficient robustness. Make sure also you understand how flushing (e.g. FileOutputStream.flush
) works with the java File
API to increase robustness. If you want full ACID robustness it's almost easier to store the data in a database.
Again, the solution will depend on the exact level of robustness you need. Some problems can be addressed at the design level (locks, flush, etc.), some will require a third-party library to have real transactions.
Related: