I am building a file upload package (or actually it has been built and I am expanding it). The file should be uploaded by a third party and when it’s done, then this code should kick off and upload the file to a clob for further processing.
The problem is that the third party can still be in the process of uploading the file while the package is trying to read it to upload it into a clob. How can you be sure the file is complete before you start reading it?
One option is to check for the existance of a different file. If this one exists, then the third party is still uploading. Another option is to try to rename the file. If this fails, then the third party is still uploading.
The idea I came up with, is to try to open the file for appending, effectively locking the file. If this works out, then close the file right away and execute the normal processing. If there is an error of some sort, then just try again. To make sure I don’t end up in an endless loop, I decided to exit my check loop, when 2 minutes or more have passed. (They must be using a really slow connection when this is the case, something like an ancient 1200baud modem 😉 )
Of course, this is not ‘waterproof’ but it seems to get the job done. The l_lock variable is declared as a boolean and is initialized to true before the loop starts.
I think I don’t need this many exceptions, but during development I decided to handle all exceptions separately so I had an easy template to do some logging for which I created a separate log package. When I had it working, I removed the log messages.