Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java writing file on Linux which gets deleted but i can still write to it

I have a Java application which is writing files to disk. The issue is, Linux does not lock files as Windows does, so someone can delete the file as it is being written to. When this happens, my Java application keeps chugging along like the file still exists...even allows more writes, with no Exceptions.

I added a check for: file.exists(), however this kills my performance by almost 50%. Anyone have any clever ideas as to how i can work around this and keep my performance high?

like image 455
user671731 Avatar asked Dec 17 '22 14:12

user671731


1 Answers

It's funny to hear of this being considered a "problem" -- those of us who cut their teeth on UNIX think Windows approach to file locking is crude and painful.

If you can't use file permissions to (mostly) prevent this, I think File.exists() is the only solution here, but presumably you could call it less frequently if it's causing a performance problem -- maybe from a "reaper" thread that wakes up occasionally to check for this condition.

like image 145
Ernest Friedman-Hill Avatar answered Mar 01 '23 23:03

Ernest Friedman-Hill