-1

I'm developing an application (Java & JavaFX) that writes/reads data (a file). The problem is I don't want to restrict user to run only one instance (of my app) at a time, as I really can't think of reliable way of doing that so it works on both Windows and Linuxes (e.g. server), heard of sockets and files - both are defective IMO. As user is able to run multiple instances, writing/reading data (from a file) seems really messy, because there's no guarantee that file locking will work reliably on Windows and Linuxes (FileLock documentation - click here).

To sum up: I can't restrict multiple instances of my app, but that leads to problem with writing/reading data (from a file).

Is there anything I missed? Maybe there's some other way to solve my problem I can't think of? How the "big" popular programs handle that?

6
  • What data are you reading/writing to the file? Can different instances of the app use different files?
    – mmathis
    Commented Mar 10, 2022 at 17:00
  • @mmathis progress, objectives of task, results of request, etc. A lot of data. About different files - I'm not sure how to handle it, all of the instances will be the same, so theoretically, if two instance will try to create the same file (as both instances will see no files when they start), that will lead to a data integrity problems
    – Wiktor
    Commented Mar 10, 2022 at 17:25
  • 1
    If you can't trust OS to do file locking you really out of luck (short of writing your own OS "with blackjack and ..." :) ). You may want to reconsider your stance on it - chance that thousands of people worked on Linux/Unix/Windows/Mac OSes got file access/sharing wrong and you will be able singlehandedly solve the problem is somewhat low. Commented Mar 10, 2022 at 19:15
  • You can use a mutex to make sure only one instance will run (look it up) and you can open files for writing, thus inhibiting other processes to change them. Commented Mar 10, 2022 at 20:38
  • Is is expected behavior that multiple instances will be running and accessing the same file? Or would normal usage of the app imply that each user runs a single instance and each instance accesses a different file? Commented Mar 23, 2022 at 14:29

2 Answers 2

1

When the application starts, generate a new filename based on a GUID or some other guaranteed-unique identifier. That particular instance of the application will then always read and write to a unique file, without worrying about file locks or conflicts. When the application is closed, it can delete the file.

If the application needs to resume reading/writing that file the next time it opens, however, you'll need to handle the file conflicts. Depending on how often the file is used and how many instances of the application you realistically expect to be open simultaneously, you can just add some re-try logic to your file IO.

0

You're somewhat misinterpreting the Java documentation. It merely states that file locks may have somewhat different semantics on different operating systems and file systems.

In general, if you treat the locks as advisory (i.e. whenever you access the file, you use the locking mechanism) they are reliable, unless there are true OS bugs affecting the locking behavior which should be rare nowadays (I remember reading about such bugs in networked file access some decades ago...).

Other programs that access the files without adhering to the locking protocol established by your application may be able to mess them up on some OSes, but they can do so too when the files aren't currently opened by your application, so you can't really blame the lock mechanism for this but only those other programs or the users who use them to access your application's files.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.