How to fix Stale File handle Issue in java application?

2.9k Views Asked by At

I have a java application, which generates files (ranging from a few Mb to 100s of Mb). I am getting this error when I try to close the file in the "finally" block.
Flow:

  1. Application read the database, create a CSV file in the server in a specified folder and write to it (a new file for each table in the database).
  2. A cron job is running every few min to change the permission and zip all those unzipped files(new files) available in that specific folder.

So, here I am not able to zero down the root cause for that issue, as it's not happening for all files but a few.
There are a few possibilities I can think of are,

  • the cron job in the server tries to zip/zip the file before the application finishes writing to it, and when the java application tries to close it at last and it gives that stale file handle error.

  • Or, Another thing I noticed is, that it is happening for those files which have a huge count of records in the range of 400k to 500k. Could it be somewhere related to GC in Java?

Any suggestion would be a great help, Thanks in advance.

0

There are 0 best solutions below