peter-lawrey / Java-Chronicle

Java Indexed Record Chronicle
1.22k stars 193 forks source link

File handles opened in IndexedChronicle constructor cannot be closed #37

Open agaluzzi opened 10 years ago

agaluzzi commented 10 years ago

I am having issues with being able to delete .data/.index files, due to file handles that remain open.

I believe the problem is that an IndexedChronicle is 'opened' during initialization (i.e. in the constructor). If the constructor throws an exception, there is no way to then close the chronicle and release the resources. Specifically, I have seen this in a scenario where the thread creating the chronicle is interrupted during construction, in which case a call to FileChannel.size() throws an exception while trying to find the last record. This is after the data/index file channels have been opened, and there seems to be no way to close them.

I would propose that either...

1) the chronicle constructor should be more careful to close all resources in the case that an exception is raised

2) the chronicle should have a separate 'open()' method, in which case 'close()' could be called after a failed opening of the chronicle

\ some might suggest that this could be done in a finalize() method, but that is generally not recommended. (https://www.securecoding.cert.org/confluence/display/java/MET12-J.+Do+not+use+finalizers)