Java 离线/在线数据同步策略
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/271610/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Strategy for Offline/Online data synchronization
提问by Adisesha
My requirement is I have server J2EE web application and client J2EE web application. Sometimes client can go offline. When client comes online he should be able to synchronize changes to and fro. Also I should be able to control which rows/tables need to be synchronized based on some filters/rules. Is there any existing Java frameworks for doing it? If I need to implement on my own, what are the different strategies that you can suggest?
我的要求是我有服务器 J2EE Web 应用程序和客户端 J2EE Web 应用程序。有时客户端可以离线。当客户端上线时,他应该能够来回同步更改。此外,我应该能够根据某些过滤器/规则来控制需要同步哪些行/表。是否有任何现有的 Java 框架可以做到这一点?如果我需要自己实施,您可以建议哪些不同的策略?
One solution in my mind is maintaining sql logs and executing same statements at other side during synchronization. Do you see any problems with this strategy?
我认为的一个解决方案是在同步期间维护 sql 日志并在另一端执行相同的语句。你觉得这个策略有什么问题吗?
采纳答案by Dónal
There are a number of Java libraries for data synchronizing/replication. Two that I'm aware of are daffodiland SymmetricDS. In a previous life I foolishly implemented (in Java) my own data replication process. It seems like the sort of thing that should be fairly straightforward, but if the data can be updated in multiple places simultaneously, it's hellishly complicated. I strongly recommend you use one of the aforementioned projects to try and bypass dealing with this complexity yourself.
有许多用于数据同步/复制的 Java 库。我知道的两个是daffodil和SymmetricDS。在前世,我愚蠢地(用 Java)实现了我自己的数据复制过程。这看起来应该是相当简单的事情,但如果数据可以同时在多个地方更新,那就太复杂了。我强烈建议您使用上述项目之一来尝试自己绕过处理这种复杂性。
回答by Kieveli
The biggist issue with synchronization is when the user edits something offline, and it is edited online at the same time. You need to merge the two changed pieces of data, or deal with the UI to allow the user to say which version is correct. If you eliminate the possibility of both being edited at the same time, then you don't have to solve this sticky problem.
同步的最大问题是当用户离线编辑某些内容时,同时在线编辑它。您需要合并两个更改的数据,或者处理 UI 以让用户说出哪个版本是正确的。如果你消除了两者同时被编辑的可能性,那么你就不必解决这个棘手的问题。
The method is usually to add a field 'modified' to all tables, and compare the client's modified field for a given record in a given row, against the server's modified date. If they don't match, then you replace the server's data.
该方法通常是在所有表中添加一个字段“已修改”,并将给定行中给定记录的客户端修改字段与服务器的修改日期进行比较。如果它们不匹配,则替换服务器的数据。
Be careful with autogenerated keys - you need to make sure your data integrity is maintained when you copy from the client to the server. Strictly running the SQL statements again on the server could put you in a situation where the autogenerated key has changed, and suddenly your foreign keys are pointing to different records than you intended.
小心使用自动生成的密钥 - 当您从客户端复制到服务器时,您需要确保保持数据完整性。在服务器上再次严格运行 SQL 语句可能会使您处于自动生成的键已更改的情况,并且突然您的外键指向的记录与您预期的不同。
Often when importing data from another source, you keep track of the primary key from the foreign source as well as your own personal primary key. This makes determining the changes and differences between the data sets easier for difficult synchronization situations.
通常,当从另一个源导入数据时,您会跟踪来自外部源的主键以及您自己的个人主键。这使得在困难的同步情况下更容易确定数据集之间的变化和差异。
回答by Norman Ramsey
Your synchronizer needs to identify when data can just be updated and when a human being needs to mediate a potential conflict. I have written a paper that explains how to do this using logging and algebraic laws.
您的同步器需要确定何时可以更新数据以及何时需要人工调解潜在冲突。我写了一篇论文,解释了如何使用日志和代数定律来做到这一点。
回答by CruiZen
What is best suited as the client-side data store in your application? You can choose from an embedded database like SQLite or a message queue or some object store or (if none of these can be used since it is a web application) files/ documents saved on the client using Web DB or IndexedDBthrough HTML 5's LocalStorageAPI.
什么最适合作为应用程序中的客户端数据存储?您可以选择嵌入式数据库,如 SQLite 或消息队列或某些对象存储,或者(如果这些都不能使用,因为它是 Web 应用程序)使用 Web DB 或IndexedDB通过 HTML 5 的LocalStorageAPI保存在客户端上的文件/文档.
Check the paper Gold Rush: Mobile Transaction Middleware with Java-Object Replication. Microsoft's documentation of occasionally connected systems describes two approaches: service-oriented or message-oriented and data-oriented. Gold Rush takes the earlier approach. The later approach uses database merge-replication.
查看论文Gold Rush: Mobile Transaction Middleware with Java-Object Replication。Microsoft 偶尔连接系统的文档描述了两种方法:面向服务或面向消息和面向数据。淘金热采用了较早的方法。后一种方法使用数据库合并复制。