Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class LocksServiceImpl implements ILocksService {
- /**
- * acquire - Obtain a lock that prevents other transactions from executing
- */
- public void acquire(String lockItem) {
- Integer retryCount = 0;
- Exception error;
- while (retryCount < LocksService.MAX_RETRIES) {
- try {
- // FOR UPDATE will pause this Txn until lock is freed by other transaction
- Mutex__c[] mutexes = MutexesSelector.newInstance().selectForUpdateByItem(new Set<String> {lockItem});
- if (mutexes.isEmpty()) {
- insert new Mutex__c (Lockable_Item__c = lockItem);
- }
- return; // this Txn now has the lock on lockItem
- }
- catch (Exception e) {
- if (Util.isTransientException(e)) { // UNABLE_TO_LOCK_ROW
- error = e;
- retryCount++;
- }
- else {
- error = e;
- break;
- }
- }
- }
- throw new LocksService.MutexException('Unable to obtain a transaction lock on ' + lockItem +
- ' retries:' + retryCount + ' v. maxRetries:' + LocksService.MAX_RETRIES + ' exception:' + Util.showException(error));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement