Hi Everyone,
Let's discuss something on Optimistic Concurrency today.
What are Concurrent Transaction?
When two or more users need to update a same record at the same time, the changes made by the last user will be saved and this results data loss. The operations performing by users on the record at the same are known as Concurrent Transactions.
In general, there are two ways to control this behavior.
1) Pessimistic Concurrency Control
When a user is modifying the data, another user can’t access the same data at same time as the access has been locked on that data.
2) Optimistic Concurrency Control
This feature provides the ability for your applications to detect whether an entity 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.
On all entities, you will see a OOB filed "Version Number" of type Timestamp and it gets updated by the system every time when the record updates.
In the below code, I am retrieving the record and trying to update but before updating the record using the code I am going update the record manually in CRM. The below code will throw the exception as below.
"The version of the existing record doesn't match the RowVersion property provided."
Hope this helps.
--
Happy 365'ing
Let's discuss something on Optimistic Concurrency today.
What are Concurrent Transaction?
When two or more users need to update a same record at the same time, the changes made by the last user will be saved and this results data loss. The operations performing by users on the record at the same are known as Concurrent Transactions.
In general, there are two ways to control this behavior.
1) Pessimistic Concurrency Control
When a user is modifying the data, another user can’t access the same data at same time as the access has been locked on that data.
2) Optimistic Concurrency Control
This feature provides the ability for your applications to detect whether an entity 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.
On all entities, you will see a OOB filed "Version Number" of type Timestamp and it gets updated by the system every time when the record updates.
In the below code, I am retrieving the record and trying to update but before updating the record using the code I am going update the record manually in CRM. The below code will throw the exception as below.
"The version of the existing record doesn't match the RowVersion property provided."
try
{
//
Retrieving the account record.
Entity entAccount =
crmService.Retrieve("account", new Guid("4921991e-162d-ea11-a813-000d3a59f4b3"), new
ColumnSet(new string[] { "name" }));
//
Creating a new object and updating the account record that was retrieved above
by setting Concurrency Behavior to If RowVersionMatches.
Entity entAccountToUpdate = new Entity("account");
entAccountToUpdate.Id =
entAccount.Id;
entAccountToUpdate["name"] = "My Account Name";
//
Setting up the Row Version.
entAccountToUpdate.RowVersion =
entAccount.RowVersion;
UpdateRequest accountReq = new UpdateRequest()
{
Target =
entAccountToUpdate,
ConcurrencyBehavior =
ConcurrencyBehavior.IfRowVersionMatches
};
UpdateResponse
accountUpdateResponse = (UpdateResponse)crmService.Execute(accountReq);
}
catch (FaultException<OrganizationServiceFault> fx)
{
switch (fx.Detail.ErrorCode)
{
case -2147088254: // ConcurrencyVersionMismatch
case -2147088253: // OptimisticConcurrencyNotEnabled
throw new InvalidOperationException(fx.Detail.Message);
case -2147088243: // ConcurrencyVersionNotProvided
throw new ArgumentNullException(fx.Detail.Message);
default:
throw fx;
}
}
Hope this helps.
--
Happy 365'ing
Gopinath
where on earth did you get these codes from? I am scowering the internet trying to find them.
ReplyDeleteHi Michael,
DeleteHere is the link that provides you more information on the codes.
https://docs.microsoft.com/en-us/powerapps/developer/common-data-service/optimistic-concurrency#handle-exceptions
Stacydoe.com is the place where you can buy vouchers, coupons for all advertisement needs of online advertising.
ReplyDelete