Changing the tracking structure

We aim to implement optimistic locking in our WCF / WPF application. So far, the best way I have come up with is to implement a generic Optimistic that will keep a copy of the original and any changes (so it will keep two copies: the original and the modified one) of any value object that can be modified. Is this the best way to do it?

For example: UserVO will be wrapped generically as Optimistic. When making changes to Optimistic, the changes will be made to the modified copy saved in Optimistic, while the original, also saved in Optimistic, will remain unchanged. The main problem is that it will use twice as much space and therefore bandwidth.

thank

EDIT . The solution should be database agnostic and it would be useful to be able to specify a conflict resolution policy for a single value object. (for example, a custom object might try to merge if the updated rows have not changed, but the transaction object will always require user interaction).

0


source to share


3 answers


If you are using SQL server you can use the timestamp column. The timestamp column changes when the row changes. Basically, when you update the DB, you can check if the timestamp column is the same as when the client first received the data, so no one has modified the data.

Edit

If you want to minimize bandwidth, you can emulate the concept of timestamping by adding a version number for each object. For example:



  • Client 1 requests an object, the server returns an object V1
  • Client 2 requests an object, the server returns an object v2
  • Client 1 modifies the object sending it back to the server as V1
  • The server compares the version and sees v1 = v1, so it commits the change
  • The server is increasing the version of the object, so now its v2
  • Modifis 2 client object sending it back to server as v1
  • The server compares the version and sees v1! = V2, so it enforces its policy

To set up a policy, you can define in the configuration a specific object that will handle policy errors depending on the type of root object. You can hack the IOptomisticCheckFailurePolicy interface and you can confidently use one of the DI libraries, such as a structural map, to create an object when you need it (although you could just as easily load it using reflection)

0


source


One way to implement optimistic locking logic is to base it on the last modified row timestamp. Therefore, the update will look like this:

UPDATE .... WHERE id = x AND last_updated = t

x: write the identifier. t: last updated timestamp of the row when it was loaded from the database (i.e. before the changes).

Note that one of the fields that needs to be updated, either directly or indirectly, is the timestamp that will be set at this time (or UtcNow).



Thus, the update will fail if the entry is modified in the background. After an update has failed, you can ask additional queries to determine the cause of the failure. For example,

  • The entry has been deleted.
  • The entry has been changed.

This simple approach provides optimistic row-level locking, is not column-based, and has no conflict resolution.

0


source


Have you reviewed Microsoft Sync Framework ?

0


source







All Articles