tags:

views:

24

answers:

1

The Java manual says:

The locks held on a particular file by a single Java virtual machine do not overlap. The overlaps method may be used to test whether a candidate lock range overlaps an existing lock.

I guess that if I lock a file in a tomcat web application I can't be sure that every call to this application is done by a different JVM, can I? So how can I lock files within my tomcat application in a reliable way?

A: 

I was actually having the exact opposite problem -- my servlet was causing files to get locked that wouldn't clear when the context was reloaded. Of course the reason was because I was opening up InputStreams/BufferedReaders and not closing them. For you, opening up the files in this fashion might be a pretty low-tech solution to your problem as it should result in a lock at the O/S level, which is probably what you want.

Shaggy Frog
Opening a FileInputStreams does not automatically create a file lock, or am I wrong?If I create locks at O/S level, this means that the locks may be at process level and one application with several threads can still access the file concurrently. Can I count on tomcat executing each web request in a different process instead of using Threads?
yankee
Why don't you try it out?
Shaggy Frog
Because even if I find out that my OS is locking on thread level and not on process level I don't know whether this is true for a different operating system. Even if I find out that tomcat uses processes in my setup, how do I know whether this is still true if I deploy my application to a different server? That's all a little vague and that's why I'd like to read a specification that says for sure or a guide to lock files in a reliable portable way in tomcat web applications.
yankee