call to close on RandomAccessFile

Andy Jung

Ranch Hand

Posts: 150

posted 6 years ago

Howdy ranchers!

When my server starts, there will be a reference to a permanently opened RandomAccessFile.
But where or when do I have to call the close operation on that file? Should I rely on the JVM to close this implicitly when the server shuts down, or should I explicitly place this operation in the finalize()-method of the singleton?

Depending on the approach you take, either doing read()/write()s all the time and hangling IOExceptions
or using a cache in memory (I am following this approach and I find it a lot easier and it was also used by
Roberto Perillo and Roel De Nijs) you will need to use some mechanism that will no longer accept operations
on the database file.

In the Monkhouse book there is an example of this Shutdown hook locking the Database file.

This should take care of closing the file.

I would not use a finalize() method since they are run by the GC thread in the JVM and when it runs
it is not deterministic.