1

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.

Hemant Kumar
  • 135
  • 1
  • 1
  • 12
  • 1
    **-1-** Check the code for silent exceptions; log sufficient info. SonaLint might surprise. **-2-** Write a small application and try to emulate the error you think might happen. **-3-** GC unlikely, there still is the `finalize` in `File` though. Maybe you should try `flush()` after every write, ugly and costly. **-4-** Writing your own cron job in java could coordinate things. Terrible too. – Joop Eggen Aug 05 '22 at 09:47
  • 1
    thanks @JoopEggen, after inspecting the logs carefully I suspect the cron job is messing things up. In application, I'm creating the file before getting data from DB(seems a bad decision now), and the repo call is taking a few mins when the record count is huge meanwhile the cronjob runs and java loses the file reference it seems. Because I'm getting a similar pattern at most places e.g. **08:04:14**-file created and repo method called->(cron job running every 5 min so 08:05:00 and 08:10:00 would run)-> **08:10:21**-repo returns data and write operation happens. Need to replicate and verify. – Hemant Kumar Aug 06 '22 at 21:13

0 Answers0