Apex fflib_SObjectUnitOfWork SimpleDML UNABLE_TO_LOCK_ROW nested try catch

UNABLE_TO_LOCK_ROW issue is very common if you have multiple users updating the record at the same time .Or say a batch job is running and is updating a record and same record another trigger or code snippet (usually a future method) is updating. A way to solve this is to retry multiple times to see if the record has been unlocked for update.

Multi-try will try update 3 times before throwing and error

public class SimpleDML implements IDML
{
	public void dmlUpdate(List<SObject> objList){
		try {
			update objList;
		} catch (System.DMLException ex1){
			if (StatusCode.UNABLE_TO_LOCK_ROW==ex1.getDmlType(0)){
				try{
					update objList;
				} catch (System.DMLException ex2) {
					if (StatusCode.UNABLE_TO_LOCK_ROW==ex2.getDmlType(0)){
						update objList;
					} else {
						throw ex2;
					}
				}
			} else {
				throw ex1;
			}
		}
	}
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: