Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. public class LocksServiceImpl implements ILocksService {
  2. /**
  3. * acquire - Obtain a lock that prevents other transactions from executing
  4. */
  5.  
  6. public void acquire(String lockItem) {
  7.  
  8. Integer retryCount = 0;
  9. Exception error;
  10.  
  11. while (retryCount < LocksService.MAX_RETRIES) {
  12. try {
  13. // FOR UPDATE will pause this Txn until lock is freed by other transaction
  14. Mutex__c[] mutexes = MutexesSelector.newInstance().selectForUpdateByItem(new Set<String> {lockItem});
  15. if (mutexes.isEmpty()) {
  16. insert new Mutex__c (Lockable_Item__c = lockItem);
  17. }
  18. return; // this Txn now has the lock on lockItem
  19. }
  20. catch (Exception e) {
  21. if (Util.isTransientException(e)) { // UNABLE_TO_LOCK_ROW
  22. error = e;
  23. retryCount++;
  24. }
  25. else {
  26. error = e;
  27. break;
  28. }
  29. }
  30. }
  31. throw new LocksService.MutexException('Unable to obtain a transaction lock on ' + lockItem +
  32. ' retries:' + retryCount + ' v. maxRetries:' + LocksService.MAX_RETRIES + ' exception:' + Util.showException(error));
  33. }
  34.  
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement