数据同步框架MS Sync Framework [术语、例子、参考资料、Tips]

在现在的应用中,一些临时脱机(Offline)应用,需要和主数据库进行定期的数据同步,采用MS Sync Framework可以快速的实现一个解决方案,而且这个框架本身有很大的扩展性,可以用于数据库(ADO.net兼容的)、文件,Web Feed等同步。

术语

[具体的翻译在MSDN]

synchronization scope

A synchronization scope is a logical grouping of objects that must be synchronized as a unit.

knowledge

The metadata that is maintained by each participant that describes all the changes it has seen. In its simplest form, known as a watermark, a knowledge item is a clock vector that consists of pairs of replica keys and replica tick counts.

provision

After you define the synchronization scope, you provision the database to create a change-tracking and metadata management infrastructure that consists of metadata tables, triggers, and stored procedures.

参考资料

http://msdn.microsoft.com/sync

http://msdn.microsoft.com/en-us/sync

官方博客 http://blogs.msdn.com/b/sync/

http://social.msdn.microsoft.com/Forums/en-US/synclab/threads

How To: Provision for Synchronization Using Snapshot Initialization

http://msdn.microsoft.com/en-us/library/gg294193(v=SQL.110).aspx

Sync Framework Tips and Troubleshooting

http://social.technet.microsoft.com/wiki/contents/articles/sync-framework-tips-and-troubleshooting.aspx

Database Synchronization

  • Use snapshot initialization to create and initialize multiple SQL Server Compact databases in your synchronization community. Snapshot initialization offers much faster performance for setting up multiple SQL Server Compact databases than provisioning each one individually. And the API is simple, too! Here's an article that tell you how to do this: http://msdn.microsoft.com/en-us/library/gg294193(v=SQL.110).aspx.
  • Use backup and restore (with post-restore fixup) to create and initialize multiple large SQL Server databases in your synchronization community. If you are setting up a synchronization community that contains copies of a large SQL Server database, performance will be better if you use backup and restore to create the database copies instead of an initial synchronization. Here's an article that tells you how to do this: http://msdn.microsoft.com/en-us/library/ee617375(v=SQL.110).aspx.
  • Don't use TRUNCATE TABLE to modify a table that is synchronized. This is because, while TRUNCATE TABLE is in effect a DELETE statement, it does not activate the trigger used to update the change tracking metadata, so the deletion is invisible to Sync Framework and will not be sent to other replicas.
  • To find a list of the scopes that are currently provisioned in a database, query the scope_info table. The scope_info table contains information about each scope, including the scope name.
  • When a table in a SQL Server Compact database is initialized through synchronization, the seed and increment of its identity columns are always set to 0 and 1, respectively, regardless of how they are set on the server database. If you need to insert rows locally in the client database you must first set the identity seed and increment to appropriate values or manually specify the identity column value, otherwise new rows will have duplicate identity values and row insertion will fail. The identity seed and increment can be set with the following query, which sets the seed value to 10 and the increment to 1: ALTER TABLE ExampleTable ALTER COLUMN idColIDENTITY(10,1)

Sync Framework 4.0

这个是目前的版本,现在是才CTP版本

如下是安装后其中的一个工具

C:\Program Files (x86)\Microsoft SDKs\Microsoft Sync Framework\4.0\bin

SyncSvcUtilHelper.exe

SyncSvcUtil.exe

数据同步框架MS Sync Framework [术语、例子、参考资料、Tips]

可见常用的功能通过这些工具可以自动生成,更简化的处理的步骤。

其中例子目录iPhoneSample,是一个使用Odata协议的数据服务同步程序,很酷。

估计正式版本对开发更简化

例子

三种同步形式

Sync Framework provides snapshot, download-only, upload-only, and bidirectional synchronization for client and server scenarios:

Ø Snapshot and download-only synchronization are typically used to store and update reference data, such as a product list, on a client. Data changes that are made at the server are downloaded to the client database during synchronization. Snapshot synchronization completely refreshes data every time that the client is synchronized. This is appropriate when you do not want to track incremental changes or the server cannot do so. Download-only synchronization downloads only the incremental changes that have occurred since the previous synchronization.

Ø Upload-only synchronization is typically used to insert data, such as a sales order, on a client. Inserts and other changes to data that are made in the client database are uploaded to the server during synchronization.

Ø Bidirectional synchronization is typically used for data, such as customer contact information, that can be updated at the client and server. Any conflicting changes must be handled during synchronization.

SDK本身有例子,其他的比较好的参考资源

Additional samples are available in the Sync Framework SDK and on Code Gallery.

http://go.microsoft.com/fwlink/?LinkId=123328

这个地址上有Orcale ,SQL Express等的例子

http://archive.msdn.microsoft.com/Release/ProjectReleases.aspx?ProjectName=sync&ReleaseId=3422

Database Sync - SQL Server and SQL Compact 2-Tier

http://archive.msdn.microsoft.com/sync/Release/ProjectReleases.aspx?ReleaseId=3761

Database Sync - Oracle and SQL Compact 2-Tier

下文我把根据SDK测试的几个例子分析一下,对这个框架有更详细的了解

上一篇:excel定义函数操作文档


下一篇:【.Net MF网络开发板研究-03】获取雅虎天气(HttpClient示例)