Wednesday, July 4, 2018

Optimistic Concurrency Exceptions

The optimistic concurrency check (OCC) is a performance enhancing function
within Microsoft Dynamics AX. It presumes that any record retrieved from the
database is not updated, until it is actually proven to be updated by the database.
This means that fewer locks have to be placed on records in the database. This
allows for faster access for other users.
This also means that one user can update a record after another user has retrieved
it from the database. This can cause data inconsistency. If the second user then
also tries to update the record, an UpdateConflict exception is thrown. The
system does this by comparing the recVersion field on the record buffer at
runtime and the actual record in the database. The recVersion field value is
changed every time that an update is successfully made to a record.
The following code is an example of how to handle the UpdateConflict exception
that might be thrown.



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
try
{
  ...
}
  catch (Exception::UpdateConflict)
{
  //the TTS level is automatically reduced by 
  if (appl.ttsLevel() == 0)
{
  if (xSession::currentRetryCount() >= 
  #RetryNum)
{
  throw 
  Exception::UpdateConflictNotRecovered;
}
else
{
  retry;
}
}
else
{
  throw Exception::UpdateConflict;
  //thrown to the caller
}
}

If a conflict due to OCC occurs, the system throws the UpdateConflict exception
and it is caught by the catch statement.
The code checks the current TTS level. If it is not zero, in other words, it is still
in a TTS transaction, it throws another UpdateConflict exception to the next
Catch list of the next outer Try statement in scope. This continues until it is no
longer inside a TTS transaction. The developer must make sure that when the
code is retried, all data is refreshed.
As soon as the code is outside all transaction scope, the
retry statement is called.
The Try statement is then re-executed. If the problem continues for as many
retries as is specified in the #RetryNum macro (system default is 5), then the
UpdateConflictNotRecovered exception is thrown. This means the whole
transaction is aborted and stops retrying.


Best Regards,
Hossein Karimi

No comments:

Post a Comment

Configure the Firewall on the Enterprise Portal Server

After you install Enterprise Portal, enable Web Server (HTTP) in Windows Firewall. If you do not enable the web server in Windows Firewall...