Servlet File Locking issue where FileLock may not apply

I'm having issues with File Access Synchronization. The basic setup involves a servlet being used for uploading files to a website. The web page (Ajax-y goodness) prepares the upload form and upon submission of the form, repeatedly calls a progress-checking servlet. The upload form is sent to a separate servlet which, during the upload, creates and updates a temporary file containing the upload progress.

As far as I'm aware, these servlets are different threads within the same JVM, though they might be processes... If they ARE running in the same JVM, I CANNOT USE java.nio.FileLock. (As it mentions in the documentation, FileLock is not appropriate for multiple threads within one JVM as it gives file access to the entire JVM, not just one thread.) (I'm using Tomcat and am unsure whether calling a servlet spawns threads or processes.) Either way, the uploading servlet creates a user-unique temporary file based on the user id. (One progress file per user. A specific user's progress file will always have the same name, but we are assuming someone is only attempting one upload at a time...) The progress-reading servlet generates the same file name and reads the file, returning the results to the user.

Both servlets generate their own reference to the file, so I don't trust to synchronize on their File objects instances. [Would that work? Can 2 threads synchronize on independently generated file instances if they both point to the same file?] On the other hand, I don't want to synchronize on any single class as EVERY user would then synchronize on that class, which is far more restrictive than we need.

I guess I'm kind of stuck. Every solution I've found expects the two threads to be closely connected to the point that they can share the same file reference, or they expect the processes to be running on separate JVMs. Any assistance would be greatly appreciated.

[1921 byte] By [Syndica] at [2007-11-26 15:39:00]
# 1
I missed the part of the post where you explained why you need to lock and/or synchronize things. You've got one servlet doing an upload to a file and a second servlet finding out how far it's got, is that the design? If so then I don't see where locking comes into the picture.
DrClapa at 2007-7-8 21:57:18 > top of Java-index,Java Essentials,New To Java...
# 2

The reader keeps getting EOFExceptions. We assume it's because the reader is trying to read while the writer is updating the file. The EOFExceptions disappear once the writer stops updating the file, and they are intermittant while the writer is updating the file, leading me to believe that making the reader wait until the writer is finished with that write attempt will solve the EOFException problem.

Syndica at 2007-7-8 21:57:18 > top of Java-index,Java Essentials,New To Java...
# 3

What reader is this? Is that the thing you called the "progress-checking servlet" in your original post? If so, why is it reading the file?

Never mind, I just noticed that the uploading servlet is storing its progress information in a file. I wasn't expecting that. So that's the file the progress-checking servlet is having trouble reading. Now I get it. So is this "progress information" so extensive you couldn't just store it in the user's session? You wouldn't have these problems if you did that.

DrClapa at 2007-7-8 21:57:18 > top of Java-index,Java Essentials,New To Java...
# 4

We haven't used user sessions for anything yet. I've not specifically created a session at any point, so unless one is created by default, I'm unsure of how to proceed. Is there a good reference for servlet sessions that you would recommend?

(On a side note, I'm still interested in a general solution to file synchronization across separated threads within the same JVM. If anyone has any idea on that account I'd appreciate it greatly.)

Syndica at 2007-7-8 21:57:18 > top of Java-index,Java Essentials,New To Java...
# 5

Any book on servlets will cover the use of sessions very early on. Or any tutorial, I would expect. There's a lot of them.

And I would definitely recommend the use of sessions instead of temporary files in this case.

Edit: As for file synchronization between two threads in the same JVM, standard synchronization techniques should work perfectly well. Just pick a suitable object to synchronize on.

Message was edited by:

DrClap

DrClapa at 2007-7-8 21:57:18 > top of Java-index,Java Essentials,New To Java...