groovy 中的多个文件访问(Groovy on Grails)
我有一个允许多个用户访问 1 个 xml 文件的应用程序。问题是,当所有用户同时保存时,其他用户的其他更改不会被保存。如何检测文件是否在 groovy 中的用户中?
谢谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这个问题与 groovy/grails 没有任何具体关系,而只是并发文件修改的一个基本问题。您应该以与 SVN 或 CVS 等源代码控制系统 (SCCS) 相同的方式处理此问题。
具体来说,存储文件上次修改的时间。这可以存储在文件本身、文件的元数据中或每次上传 XML 文件时提供的另一个文件中。当用户上传文件时,检查自他获得副本以来该文件是否已被(由其他用户)修改。如果有,你有多种选择:
一种方法(可能有点过分)是实际使用 SCCS 来处理此版本控制问题。有一个 用于 CVS 的 Java API(可能还用于其他 SCCS),它使您能够以编程方式签入、签出并合并一个文件。
此建议假设您需要允许并发修改文件。如果不这样做,那么使用禁止同时修改的方法来解决问题会更简单。
更新:
评论者之一提到的有关
version
属性的一些信息可在 Grails 参考手册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另一种方法:维护一个包含 file -> 的全局范围的哈希图。用户映射(如果与哪个用户锁定文件无关,则只是文件列表)。如果用户尝试打开文件:
这样的全局范围哈希映射可能作为属性存储在 ServletContext。另请参阅此。
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.