Hello, I have an application that allows multiple users to access 1 xml file. The problem is that when all the users save at once the other changes by other users does not get saved. How do I detect if the file is in user in groovy?
Thanks!
Hello, I have an application that allows multiple users to access 1 xml file. The problem is that when all the users save at once the other changes by other users does not get saved. How do I detect if the file is in user in groovy?
Thanks!
This issue has nothing specifically to do with groovy/grails, but is just a fundamental problem of concurrent file modification. You should handle this problem in the same way as a source code control system (SCCS) such as SVN or CVS.
Specifically, store the time that a file was last modified. This could be stored in the file itself, in the file's metadata, or in another file that is is provided each time the XML file is uploaded. When a user uploads a file, check if it has been modified (by another user) since he obtained the copy. If it has, you have a number of choices:
One approach (which might be overkill) would be to actually use a SCCS to handle this versioning problem. There is a Java API for CVS (and probably for other SCCSs), that enables you to programatically checkin, checkout and merge a file.
This suggestion assumes that you need to allow concurrent modification of the file. If you don't then it would be simpler to solve the problem using an approach that prohibits simultaneous modification.
Update:
Some information about the version
property that one of the commenters mentioned is available in sections 5.3.5 and 5.5.2.7 of the Grails reference manual
Another approach: maintain a global scoped hashmap containing file -> user mappings (or just a list of files if it's not relevant which user has locked the file). If an user tries to open a file:
Such a global scoped hashmap might be stored as an attribute in the ServletContext. See also this.