groovy 中的多个文件访问(Groovy on Grails)

发布于 2024-08-29 12:26:23 字数 101 浏览 7 评论 0原文

我有一个允许多个用户访问 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不念旧人 2024-09-05 12:26:23

这个问题与 groovy/grails 没有任何具体关系,而只是并发文件修改的一个基本问题。您应该以与 SVN 或 CVS 等源代码控制系统 (SCCS) 相同的方式处理此问题。

具体来说,存储文件上次修改的时间。这可以存储在文件本身、文件的元数据中或每次上传 XML 文件时提供的另一个文件中。当用户上传文件时,检查自他获得副本以来该文件是否已被(由其他用户)修改。如果有,你有多种选择:

  • 给他最新的副本并告诉他自己合并更改(这是蹩脚的 SCCS 会做的事情)
  • 尝试并在不冲突的情况下自动合并更改(这是更好的 SCCS 会做什么)。如果存在冲突,则需要用户手动解决。

一种方法(可能有点过分)是实际使用 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:

  • Give him the most recent copy and tell him to merge the changes himself (this is what a crappy SCCS will do)
  • Try and automatically merge the changes if they don't conflict (this is what a better SCCS will do). If there are conflicts, these will need to be resolved manually by the user.

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

小矜持 2024-09-05 12:26:23

另一种方法:维护一个包含 file -> 的全局范围的哈希图。用户映射(如果与哪个用户锁定文件无关,则只是文件列表)。如果用户尝试打开文件:

  1. 检查该文件是否已包含在映射中
  2. (如果包含),则拒绝尝试并执行一些错误处理
  3. (如果不包含),向映射添加一个条目
  4. 执行工作
  5. 打开文件并在保存文件后 ,应从映射中删除相应的条目。

这样的全局范围哈希映射可能作为属性存储在 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:

  1. check if this file is already contained in the map
  2. if so, deny the attempt and do some error handling
  3. if not, add an entry to the map
  4. open the file and do the work
  5. after saving the file, the respective entry should be removed from the map

Such a global scoped hashmap might be stored as an attribute in the ServletContext. See also this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文