Configuring optimistic locking
ADFbc can be configured with different locking modes, in particular pessimistic and optimistic locking. Since pessimistic locking might create pending row-level locks on the database, the general recommendation is to use optimistic locking for web applications, to achieve a good performance. For more information, see Fusion Application Developers Guide, 40.11 Keeping Pending Changes in the Middle Tier.
To configure optimistic locking, either set the jbo.locking.mode property of your application module configuration to "optimistic" or configure the locking mode globally in .adf/META-INF/adf-config.xml:
... <amconfig-overrides> <config:Database jbo.locking.mode="optimistic"/> </amconfig-overrides> ...
If configured, the setting from adf-config.xml takes precedence over the application module property.
Detecting lost updates
With optimistic locking, when more than one user is working on the same set of data, it can happen that the same attribute is modified by more than one user at the same time. When the first user commits his transaction, the updated value is written back to the database - other users would never see these (probably important) modifications, and when the next user also commits the transaction, his modifications would be written back to the database, overwriting the modifications from the first user. This phenomenon is also known as "lost update". ADF has the ability to detect attempts to update data which has been modified in parallel. When the data is committed (e.g. by executing the "commit" action on the application module), ADF performs the following actions:
1. The row is locked on the database by executing an SELECT ... FOR UPDATE NOWAIT query, like
SELECT DATA, ID FROM SIMPLE SimpleEO WHERE ID=:1 FOR UPDATE NOWAIT
2. ADF then compares the values selected through this query against the current values stored in the entity object. If any of the attribute values have changed, ADF will display an error dialog:
Improving performance by using a change indicator
Comparing all attributes in the EO in step 2 above can be time consuming, especially if the EO contains many attributes. To improve the performance, it is possible to define one of the attributes as a change indicator:
In several books, it says "one or more attributes can be selected as change indicator" - however, at least with JDeveloper 184.108.40.206, only one attribute of an EO can be selected as change indicator. If setting the flag on one attribute, JDeveloper automatically resets the flag on the other attribute where it has previously been set. This is also consistent with the Javadoc, where it says
"This method uses the ChangeIndicator attribute, if it is present, for the comparison. If ChangeIndicator is not present, then this method compares the data for overlapping columns between the entity and the given array."
When a change indicator is defined on the EO, the framework only performs the comparison for the attribute marked as such, instead of for every entity attribute. Note however that the SQL statement still queries all attributes!
An important detail is that the change indicator attribute needs to be a system field which is automatically updated in the background whenever the row in the database is modified, for example through an ON UPDATE database trigger. Do not use an attribute which can be edited by the user - otherwise, ADF assumes that the record has been modified by another user even though that is not the case. This check is not done when committing the data, but already when changing the attribute value (means, when calling the setAttribute() method on the EO). This might result in the following error message on the ADF rich client UI (note the different representation as a popup window, not as a dialog like above):
See also this article from Chris Muir for more information on optimistic locking: The case of the phantom ADF developer (and other yarns)