views:

193

answers:

1

I am using RandomAccessFile to perform some writes to a file as part of a transaction. Before I commit my transaction, I want to be absolutely sure that the data is written to disk. Calling force(boolean) on the RAF's FileChannel appears to provide this guarantee, but is it called implicitly when I close the file, or do I have to call it manually?

Also, does anybody have any insight into what force() actually does, and how far it can be trusted? Is it possible that the OS may report that the data has been written to disk, when in fact it is still sitting in some cache somewhere? To what extent is this OS/HDD/filesystem-dependent?

A: 

This is completely FS-dependent. But if you do not get an (IO)Exception, you should trust the JVM and commit your transaction. Whether you can trust your FS is the other question. For example, if you use ext4 having delayed allocation activated, you might lose data on a instant power loss even after RandomAccessFile#force(boolean) returned successfully. The JVM relies on the FS-API of your operating system and does not care about the actual implementation and configuration.

And yes, you have to call RandomAccessFile#force(boolean) before closing. Whether #close invokes #force is up to the FileChannel's implementation.

oeogijjowefi