編輯

共用方式為


Optimistic concurrency

On a multi-threaded and multi-user system like Power Apps, operations and data changes often happen in parallel. A problem arises when two or more update or delete operations on the same piece of data happen at the same time. This situation could potentially result in data loss. The optimistic concurrency feature provides the ability for your applications to detect whether a table record has changed on the server in the time between when your application retrieved the record and when it tries to update or delete that record.

Optimistic concurrency is supported on all out-of-box tables enabled for offline sync and all custom tables. You can determine if a table supports optimistic concurrency by retrieving the table's metadata using code or by viewing the metadata using the Metadata Browser, and check if the column IsOptimisticConcurrencyEnabled is set to true. For custom tables, this property is set to true by default.

Enable optimistic concurrency

You can enable optimistic concurrency checking behavior when executing an UpdateRequest by setting the ConcurrencyBehavior property of the request to IfRowVersionMatches. Similarly, for a DeleteRequest, you would set the ConcurrencyBehavior property.

When using the SDK for .NET context to make data changes, set ConcurrencyBehavior on the OrganizationServiceContext object. This value will be passed through to all of the UpdateRequest and DeleteRequest messages used by the OrganizationServiceContext when SaveChanges() is called.

Optimistic concurrency behavior can only be set through an SDK API call. There is presently no setting for it in a form of the Web application.

Apply optimistic concurrency using Web API

For information about using Web API to apply optimistic concurrency, see Apply optimistic concurrency

Apply optimistic concurrency using SDK for .NET

For information about using SDK for .NET to apply optimistic concurrency, see Optimistic concurrency behavior

Handle exceptions

There are several error conditions that can be returned in a fault exception <OrganizationServiceFault> from the Web service call when using optimistic concurrency.

  • ConcurrencyVersionMismatch (code=-2147088254)

    When a row version is provided and the IfVersionMatches behavior is indicated, if the existing record’s version does not match the row version provided in the request, a fault is returned.

  • ConcurrencyVersionNotProvided (code= -2147088253)

    When the IfVersionMatches behavior is indicated, and no value for row version is provided, a fault is returned.

  • OptimisticConcurrencyNotEnabled (code=-2147088243)

    When the IfVersionMatches behavior is indicated on an update to a table, and where optimistic concurrency isn’t enabled, a fault is returned.

    You can check the Code property of the returned fault to determine if the fault is related to optimistic concurrency. The codes for the error conditions that were shown previously were obtained from the ErrorCodes.cs helper code.