Gentle.NET笔记(五)-同步控制
Gentle support row-level concurrency control, that is, it provides a means of ensuring data integrity when multiple processes may be accessing the same data (typical for web applications).
Note:Gentle支持低级的同步控制,提供了在多线程环境中能够确保所有的线程访问相同的数据。
To add concurrency control to your solution you must decorate an integer field (member or property) with the Concurrency attribute (alongside the TableColumn attribute). This designates the field as a control column in which Gentle will store a revision number for the given row.
Note:加入同步控制,需要用Concurrency特性对一个数字型字段进行修饰(成员或属性)
The revision counter is used as an additional criteria in the WHERE clause of any single-row update SQL statement against the table. Additionally, it is incremented by 1 on every update (both in-memory and in the database). This ensures that old data cannot be used to update a row.When you try to update a row using outdated data, Gentle throws a GentleException whose Error property will be set to Error.RecordChanged. You should wrap all updates to check for this specific error in your code. If you use the Persistent as base class, this can be done by creating your own base class and overriding the Update method.
You can use the Refresh method to recover from concurrency errors (available on Persistent, PersistenceBroker and Broker). This will update the current object with the database values, and potentially allow you to re-execute the update.
Note: The revision counter is not checked when deleting one or more rows
Note:每次使用Update时,都会使版本字段的值增1(内存与数据库中同时增加),这可以确保老的数据不会在被更新后再次使用。当使用一个过期的数据时,Gentle会抛出一个GentleException异常,通过捕获这个特定异常,就可以知道数据版本是否发生了冲突。同时,可以使用Refresh方法对数据进行刷新,从而解决同步错误。