0

我有一个允许多个用户访问 1 个 xml 文件的应用程序。问题是,当所有用户同时保存时,其他用户的其他更改不会被保存。如何检测文件是否在 groovy 中的用户中?

谢谢!

4

2 回答 2

2

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

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

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

一种方法(可能是矫枉过正)是实际使用 SCCS 来处理这个版本控制问题。有一个用于 CVS(可能也用于其他 SCCS)的 Java API,它使您能够以编程方式签入、签出和合并文件。

此建议假定您需要允许文件的并发修改。如果您不这样做,那么使用禁止同时修改的方法来解决问题会更简单。

更新:version关于其中一位评论者提到 的属性的一些信息可在Grails 参考手册的第 5.3.5 和 5.5.2.7 部分中找到

于 2010-04-19T07:38:30.167 回答
0

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.

于 2010-04-19T08:09:07.717 回答