XAF Pessimistic Locking
What Locking is all about
Transactional isolation is usually implemented by locking whatever is accessed in a transaction. There are two different approaches to transactional locking: Pessimistic locking and optimistic locking.
The disadvantage of pessimistic locking is that a resource is locked from the time it is first accessed in a transaction until the transaction is finished, making it inaccessible to other transactions during that time. If most transactions simply look at the resource and never change it, an exclusive lock may be overkill as it may cause lock contention, and optimistic locking may be a better approach. With pessimistic locking, locks are applied in a fail-safe way. In the banking application example, an account is locked as soon as it is accessed in a transaction. Attempts to use the account in other transactions while it is locked will either result in the other process being delayed until the account lock is released, or that the process transaction will be rolled back. The lock exists until the transaction has either been committed or rolled back.
With optimistic locking, a resource is not actually locked when it is first is accessed by a transaction. Instead, the state of the resource at the time when it would have been locked with the pessimistic locking approach is saved. Other transactions are able to concurrently access to the resource and the possibility of conflicting changes is possible. At commit time, when the resource is about to be updated in persistent storage, the state of the resource is read from storage again and compared to the state that was saved when the resource was first accessed in the transaction. If the two states differ, a conflicting update was made, and the transaction will be rolled back.
In the banking application example, the amount of an account is saved when the account is first accessed in a transaction. If the transaction changes the account amount, the amount is read from the store again just before the amount is about to be updated. If the amount has changed since the transaction began, the transaction will fail itself, otherwise the new amount is written to persistent storage.
XAF Build in locking mechanism
XAF’s datalayer is based on XPO , which already has an optimistic locking implementation on its core. It is enabled by default for all objects that inherit XPBaseObjet and can be disabled by using the OptimisticLocking attribute .
[OptimisticLocking(false)]
public class Client:XPBaseObject {
public Client(Session session) : base(session) {
}
}
For objects that have that attribute XPO is going to create an extra service field, the OptimisticLockingField to store that state of the object.
To see the locking mechanism in action you can perform the following steps
User 1 | User 2 |
1. Run application | 1. Run application |
2. Go to Client object detail view | 2. Go to Client object detail view |
3. XPO reads the value from the optimistic field value (initial value is zero) and stores it to memory | 3. XPO reads the value from the optimistic field value (initial value is zero) and stores it to memory |
4. User is making a change and tries to save the object - XPO queries the the optimisticfield value and compares it with the one in memory
- 2 values are equal so transaction is commited and optimisticlocking fields is raized by one
| 4. User is making a change and tries to save the object - XPO queries the the optimisticfield value (now its one cause user 1 has save the record) and compares it with the one in memory
- 2 values are not equal so a locking exception is thrown by XPO giving information about the locked object
|
| 5. User reloads the object from the database, new optimisticlocking field value is store in memory (one), user makes changes and is able to save the record |
eXpand Pessimistic locking
XPO has no means to determine any information about users, but we are in XAF context and XAF has a build in security system and we know the current user. Lets try to implement a pessimistic locking feature for our XAF applications.
By carefully looking at “3. XPO reads the value from the optimistic field value (initial value is zero) and stores it to memory “ , we can see that there is a place to minimize the locking conflicts. What if we display the detailview in “ViewMode” and add an “Edit” action? And when the Edit action is executed the object will be reloaded from the db, also when is saved the DetailView will return is View mode.
I think we are going to gain something from that even if we use the default Xpo optimistic locking. Lucky us eXpand has already the ViewEditMode attribute that can do that job for us.
Now from the Pessimistic locking definition (when it is first is accessed by a transaction). We have to decide what that means in our case. A good idea would be to lock the object at the time that is changed by a user. Of course we can enhance that in the future upon your requests. We also need a special field that will store the user that locked the record and a special attribute to mark the object for using our pessimistic locking attribute. Finally its a good idea to disable default OptimisticLocking mechanism.
All the above should be transparent to the user of our feature, we need to spent zero time when using it in future projects
Customizing XAF types is very easy as you know , just some lines of code and can do the trick.
public override void CustomizeTypesInfo(DevExpress.ExpressApp.DC.ITypesInfo typesInfo) {
base.CustomizeTypesInfo(typesInfo);
var typeInfos = typesInfo.PersistentTypes.Where(info => info.FindAttribute<PessimisticLockingAttribute>() != null);
foreach (var typeInfo in typeInfos) {
typeInfo.AddAttribute(new OptimisticLockingAttribute(false));
var memberInfo = typeInfo.FindMember(LockedUser);
if (memberInfo == null) {
memberInfo = typeInfo.CreateMember(LockedUser, SecuritySystem.UserType);
memberInfo.AddAttribute(new BrowsableAttribute(false));
}
}
}
Great! Now we need to define our specifications. Remember we are dealing with data now and with a complex feature that may evolve from fellow developers request. For those cases at eXpand we use a BDD approach, and MSpec as our BDD framework.
Here are some specs
PessimisticLockingViewController, When Object Change
» should lock the object
PessimisticLockingViewController, When objectspace rollback
» should unlock object
PessimisticLockingViewController, When ospace commited
» should unlock object
PessimisticLockingViewController, When View CurrentObject Changing
» should unlock object
PessimisticLockingViewController, When View Is closing
» should unlock object
PessimisticLocker, When object is about to be unlocked
» should not unlock if current user does not match locked user
PessimisticLockingViewController, When a locked object is open by a second user
» should not allowedit on view
PessimisticLockingViewController, When 2 users open the same object and both try to change it
» should mark as readonly last user view
PessimisticLockingViewController, When editing a locked detailview
» should allow edit for the pessimistic locking context
PessimisticLocker, When unlocking new object
» should do nothing
PessimisticLocker, When locking new object
» should do nothing
PessimisticLocker, When new object locking state is queried
» should return unlocked
and in this file you can see the implementation of them
https://github.com/expand/eXpand/blob/master/Xpand/Xpand.Tests/Xpand.Tests/Xpand.ExpressApp/PessimisticLockingSpecs.cs
As you see from the specifications when an object is locked the detailview will be read only for a PessimisticLocking context. But real world is strange we have to cover exceptions as well. What will happen if an object was locked and our application was terminated abnormally? We need an action that will force unlock the object.
Maybe there is a need for some timeout implementation there but I do not have strong ideas on this, better wait for some feedback from out there first before spending any more resources. Anyway we are very close now. What we are missing is a message that will tell which user has locked an object when it is locked.
To display the message I think we can utilize our AdditionalViewControlsProvider module. That module allows to conditionally display (When our LockingUser field is different for the current user) a message. Also allows us to conditionalize/localize the message it self. Lets see how
First step will be to use the AdditionalViewControlsRule to display the message as bellow
[PessimisticLocking]
[Custom("ViewEditMode","View")]
[AdditionalViewControlsRule("teee", "LockedUser!='@CurrentUserID' AND LockedUser Is Not Null", "1=0", "Record is locked by user {0}", Position.Top, MessageProperty = "LockedUserMessage")]
public class Client : BaseObject {
public Client(Session session)
: base(session) {
}
private string _lockedUserMessage;
[NonPersistent][Browsable(false)]
public string LockedUserMessage {
get {
var memberValue = GetMemberValue("LockedUser");
if (_lockedUserMessage != null) {
return memberValue != null ? string.Format(_lockedUserMessage, memberValue) : null;
}
return null;
}
set { _lockedUserMessage = value; }
}
}
that will create a rule at our model like the following.
and will display the message
2nd step is to refactor the attribute to something easier to use like
public class PessimisticLockingMessageAttribute : AdditionalViewControlsRuleAttribute {
public PessimisticLockingMessageAttribute(string id)
: base(id, "LockedUser!='@CurrentUserID' AND LockedUser Is Not Null", "1=0", "Record is locked by user {0}", Position.Top) {
MessageProperty = "LockedUserMessage";
}
}
and 3rd step is to refactor the LockedUserMessage property. We have seen already that dynamically adding a property is a piece of cake, but how can we dynamically add a property that has behavior such as the LockedUserMessage property?
Easy as always :), we just have to create our Custom memberinfo like
public class LockedUserMessageXpMemberInfo : XPCustomMemberInfo {
string _theValue;
public LockedUserMessageXpMemberInfo(XPClassInfo owner)
: base(owner, "LockedUserMessage", typeof(string), null, true, false) {
}
public override object GetValue(object theObject) {
var typeInfo = XafTypesInfo.Instance.FindTypeInfo(theObject.GetType());
var memberValue = typeInfo.FindMember("LockedUser").GetValue(theObject);
if (_theValue != null) {
return memberValue != null ? string.Format(_theValue, memberValue) : null;
}
return null;
}
public override void SetValue(object theObject, object theValue) {
_theValue = theValue as string;
base.SetValue(theObject, theValue);
}
}
and register it on the system.
public override void CustomizeTypesInfo(DevExpress.ExpressApp.DC.ITypesInfo typesInfo) {
base.CustomizeTypesInfo(typesInfo);
var typeInfos = typesInfo.PersistentTypes.Where(info => info.FindAttribute<PessimisticLockingMessageAttribute>() != null);
foreach (var typeInfo in typeInfos) {
var memberInfo = typeInfo.FindMember("LockedUserMessage");
if (memberInfo == null) {
var xpClassInfo = XafTypesInfo.XpoTypeInfoSource.XPDictionary.GetClassInfo(typeInfo.Type);
var lockedUserMessageXpMemberInfo = new LockedUserMessageXpMemberInfo(xpClassInfo);
lockedUserMessageXpMemberInfo.AddAttribute(new BrowsableAttribute(false));
XafTypesInfo.Instance.RefreshInfo(typeInfo);
}
}
}
Conclusion
That was a long post but the result i believe is great. All the above are implemented in eXpand framework. Next time you want to use the pessimistic lock approach presented in this blog you only have to decorate your class with 3 attributes
[PessimisticLocking]
[Custom("ViewEditMode","View")]
[PessimisticLockingMessageAttribute("AnId")]
public class Client : BaseObject {
public Client(Session session)
: base(session) {
}
private string _name;
public string Name {
get {
return _name;
}
set {
SetPropertyValue("Name", ref _name, value);
}
}
}
Download expand from http://expandframework.com/downloads/download.html and sent use your feedback or report any problems you find at our forums http://expandframework.com/forum.html
eXpand FeatureCenter implementation contains a demo of this bolg under the Miscallenous/Pessimistic locking navigation menu

