Questions asking us to recommend or find a tool, library or favorite off-site resource are off-top开发者_JAVA百科ic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Closed 8 years ago.
Improve this questionMy 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.
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 Java EE 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:
- How to manage transaction for database and file system in Java EE environment?
- How to bring coordination between file system and database?
- How to efficiently manage files on a filesystem in Java?
If 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.
As you are looking for having transactions (with ACID properties) over file-systems, XADisk can be helpful for you. Instead of the RandomAccessFile which you mentioned, it uses FileChannel (Java NIO) internally.
Please let me know if you have any questions.
Thanks, Nitin
精彩评论