編輯

Optimistic concurrency

On a multithreaded and multiuser 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 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 passes through to all of the UpdateRequest and DeleteRequest messages used by the OrganizationServiceContext when SaveChanges() is called.

You can only set optimistic concurrency behavior through an SDK API call. There's currently 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

When you use optimistic concurrency, the Web service call can return several error conditions in a fault exception <OrganizationServiceFault>.

  • ConcurrencyVersionMismatch (code=-2147088254)

    You get this error when you provide a row version and specify the IfVersionMatches behavior, but the existing record’s version doesn’t match the row version in your request.

  • ConcurrencyVersionNotProvided (code= -2147088253)

    You get this error when you specify the IfVersionMatches behavior but don’t provide a value for the row version.

  • OptimisticConcurrencyNotEnabled (code=-2147088243)

    You get this error when you specify the IfVersionMatches behavior on an update to a table, but optimistic concurrency isn’t enabled.

    Check the Code property of the returned fault to see if the fault is related to optimistic concurrency. The codes for these error conditions come from the ErrorCodes.cs helper code.