Skip to content

Commit d6a9f45

Browse files
authored
Merge pull request #10366 from emirmx/run/4756-document-optimistic-locking
Document optimistic locking
2 parents c8d1d7d + 6ca187c commit d6a9f45

4 files changed

Lines changed: 116 additions & 0 deletions

File tree

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
---
2+
title: "Optimistic Locking"
3+
url: /refguide/optimistic-locking/
4+
description: "Describes optimistic locking support."
5+
---
6+
7+
## Introduction
8+
9+
{{% alert color="info" %}}
10+
This feature is available from Mendix 11.5.0.
11+
{{% /alert %}}
12+
13+
Optimistic locking is a strategy used in concurrent systems to prevent lost updates when multiple users or processes try to modify the same piece of data at the same time. Instead of locking the data immediately, and preventing other users from accessing it, optimistic locking allows multiple users to read, and potentially modify, the same data concurrently. In optimistic locking, the assumption is that conflicts are rare, so it checks for conflicts only at the very last moment, when an update is attempted.
14+
15+
If a conflict is detected—meaning someone else has modified the data since you last read it—your update is rejected, and you typically have to re-read the data and try again.
16+
17+
You can decide whether optimistic locking is enabled or disabled for your app.
18+
19+
## Behavior of App with Optimistic Locking Disabled
20+
21+
When two modifications are saved, they are applied in the order of processing. Only changed attributes are written to the database. This means that if the two commits change different attributes or associations of an object, the changes are not overwritten.
22+
23+
For example, if one user commits changes for `AttributeA` and `AttributeB` and another user commits changes for `AttributeB` and `AttributeC` for the same object, then both `AttributeA` and `AttributeC` are committed according to both users' changes. `AttributeB` is committed based on whichever change was committed later.
24+
25+
## Behavior of App with Optimistic Locking Enabled
26+
27+
The Mendix runtime implements optimistic locking by tracking the version of all objects using the attribute `MxObjectVersion` with type `Long`. Although the `MxObjectVersion` attribute is not write-protected, setting this value does not result in it being saved to the database. Its current value is compared with the value for the same object in the database.
28+
29+
### How to Enable and Use Optimistic Locking
30+
31+
You can enable optimistic locking for your Mendix application in the `Runtime` tab in the **App Settings** dialog:
32+
33+
{{< figure src="/attachments/refguide/runtime/optimistic-locking/runtime-settings-dialog.png" >}}
34+
35+
After optimistic locking is enabled, whenever a commit is executed, the Mendix runtime automatically ensures that the object that is committed was not already changed by another party after the committer retrieved the object.
36+
37+
### New Projects and Migration
38+
39+
If an existing app already has the `MxObjectVersion` attribute, a duplicate attribute is reported in Studio Pro when optimistic locking is enabled. This must be fixed by renaming the existing attribute. The system attribute cannot be renamed.
40+
41+
### Behavior
42+
43+
The following runtime actions are influenced by optimistic locking.
44+
45+
#### Create Object
46+
47+
Initializes the `MxObjectVersion` attribute to `0`.
48+
49+
#### Commit Object
50+
51+
When the `MxObjectVersion` attribute in the object being committed is different from the value in the database, or the object was deleted from the database, the runtime throws a `ConcurrentModificationRuntimeException`. Otherwise, it proceeds with the commit while incrementing `MxObjectVersion` by one.
52+
53+
#### Delete Object
54+
55+
When the `MxObjectVersion` attribute in the object being deleted is different from the value in the database, the runtime throws a `ConcurrentModificationRuntimeException`. Otherwise, it proceeds with the delete. If the object has already been deleted, no error occurs.
56+
57+
## Performance Impact
58+
59+
Because of the version check performed during commit and delete, optimistic locking incurs some minor performance impact.
60+
61+
## Handling Optimistic Locking Errors in Microflows{#microflow-errors}
62+
63+
When an optimistic locking error occurs, the runtime log contains an entry similar to the following:
64+
65+
```
66+
com.mendix.modules.microflowengine.MicroflowException:
67+
com.mendix.core.CoreRuntimeException:
68+
com.mendix.systemwideinterfaces.MendixRuntimeException:
69+
com.mendix.core.CoreException:
70+
com.mendix.core.CoreRuntimeException:
71+
com.mendix.systemwideinterfaces.MendixRuntimeException:
72+
com.mendix.systemwideinterfaces.connectionbus.data.ConcurrentModificationRuntimeException:
73+
Object of type 'MyFirstModule.MyEntity' with guid '3940649673949185' cannot be updated, as it is modified by someone else at MyFirstModule.MyMicroflow (Change : 'Change 'MyEntity'')
74+
```
75+
76+
This error shows that there was a `ConcurrentModificationRuntimeException` while executing the `Change 'MyEntity'` change action of the `MyFirstModule.MyMicroflow` microflow. The object had the `3940649673949185` ID and was of type `MyFirstModule.MyEntity`.
77+
78+
If committing an object causes an optimistic locking error, trying to commit the same object without reloading always results in an optimistic locking error.
79+
80+
If the changes are still deemed valid, you can retry the action causing the optimistic locking error by doing the following:
81+
82+
1. Change the error handling for that action to either `Custom with Rollback` or `Custom without Rollback`, and do the following steps in the error flow.
83+
2. If `$latestError/ErrorType` is `com.mendix.systemwideinterfaces.connectionbus.data.ConcurrentModificationRuntimeException`.
84+
3. Retrieve a fresh copy of the object from the database.
85+
4. Apply the original changes to the retrieved copy.
86+
5. Perform the operation again.
87+
88+
You can see an example implementation in the following image. The change action has error handling `Custom without Rollback`. If an optimistic locking error is detected, the latest version of the object is retrieved from the database, changes are applied again, and the change action is retried.
89+
90+
{{< figure src="/attachments/refguide/runtime/optimistic-locking/retry-example.png" >}}
91+
92+
Check the [documentation](https://docs.mendix.com/refguide/error-handling-in-microflows/) about error handling to find more information about differences between `Custom with Rollback` and `Custom without Rollback`.
93+
94+
## Handling Optimistic Locking Errors in Java Actions
95+
96+
You can handle optimistic locking errors in Java actions using similar steps to those described in [Handling Optimistic Locking Errors in Microflows](#microflow-errors). However, the `com.mendix.systemwideinterfaces.connectionbus.data.ConcurrentModificationRuntimeException` exception will not be the top level exception thrown by the Mendix runtime. It will be wrapped in another exception, so you need to inspect the `cause` chain of caught exceptions.
97+
98+
## Handling Optimistic Locking Errors in the Client
99+
100+
If the optimistic locking error is propagated to the client, the following error is displayed: "The data you're trying to save has already been modified by another user. Please refresh the page and try again."
101+
102+
{{< figure src="/attachments/refguide/runtime/optimistic-locking/optimistic-locking-error-dialog.png" >}}
103+
104+
You can implement one of the following approaches to handle the optimistic locking error:
105+
106+
### Single `Save` Button
107+
108+
In this case the user can refresh the whole page, but this causes all their changes to be lost.
109+
110+
### `Save` Button With a Separate `Refresh` Button
111+
112+
Add a separate `Refresh` button that retrieves the latest object state from the database and applies the original changes to the retrieved object. After clicking the `Refresh` button, the user can inspect the latest state and can click the `Save` button again. This would be similar to a single retry from [Handling Optimistic Locking Errors in Microflows](#microflow-errors).
113+
114+
### Custom `Save` Button
115+
116+
Implement a custom `Save` button that retries saving object using similar steps described in [Handling Optimistic Locking Errors in Microflows](#microflow-errors), to retry until successful.
27.8 KB
Loading
40 KB
Loading
468 KB
Loading

0 commit comments

Comments
 (0)