Reputation: 58
When I start my Eclipse application (with my custom plugins) for the first time a .lock file is created in the workspace folder .metadata. If I try to start a second application I get the correct error message "Workspace cannot be locked". The .lock file prevents me from starting multiple applications with the same workspace.
And if I manually delete the .lock file while an application is running and then try to start a second one it works. That is ok since I manually removed the file that prevents me from that.
Here is the strange part:
When I close the application the .lock file is not removed, but I can still start another Eclipse application so it doesn't seem to care about the .lock file at that point.
Why isn't the .lock file removed after the application is shutdown and how does it know that the workspace is not in use?
How can I manually check if a workspace is in use? The .lock file is apparently not a safe indicator.
Upvotes: 2
Views: 426
Reputation: 111142
Eclipse does use the .lock
file but it also acquires an actual 'lock' on the file. This is usually done using the Java FileChannel
tryLock
method which acquires an operating system level lock on the file:
RandomAccessFile raFile = new RandomAccessFile(lockFile, "rw");
FileLock fileLock = raFile.getChannel().tryLock(0, 1, false);
The tryLock
method throws an OverlappingFileLockException
if the file is already locked by another Eclipse.
When Eclipse shuts down the lock is released and the file closed.
It is not necessary to delete the lock file so this is not done.
There is an environment variable osgi.locking
which can be used to change this behavior.
Full code is in org.eclipse.osgi.internal.location.Locker_JavaNio
Upvotes: 1
Reputation: 32895
My understanding of the lock file mechanism is this:
Upvotes: 1