Class JpaCustomizedResourceLockRepository

    • Constructor Detail

      • JpaCustomizedResourceLockRepository

        public JpaCustomizedResourceLockRepository​(org.springframework.transaction.support.TransactionTemplate template,
                                                   ResourceLockProperties resourceLockProperties)
    • Method Detail

      • lockResource

        public String lockResource​(@NonNull
                                   @NonNull String contextId,
                                   @NonNull
                                   @NonNull Class<?> type,
                                   @Nullable
                                   String sandboxId,
                                   @Nullable
                                   String conceptKey)
        Description copied from interface: ResourceLockRepository
        Lock a resource indefinitely based on identifying information. Will return a token representing the lock that is suitable to be passed to ResourceLockRepository.unlockResource(String) when the work requiring the lock is complete. If unable to acquire the lock, a ResourceLockException is thrown.
        Specified by:
        lockResource in interface ResourceLockRepository<JpaResourceLock,​UUID>
        Parameters:
        contextId - a unique id of the obtain a lock for
        type - grouping for the contextId
        sandboxId - sandbox associated with the entity
        conceptKey - a generally descriptive key that identifies the overall concept for which the lock is taking place
        Returns:
        The token that identifies the lock
      • lockResourceTemporarily

        public String lockResourceTemporarily​(@NonNull
                                              @NonNull String contextId,
                                              @NonNull
                                              @NonNull Class<?> type,
                                              @Nullable
                                              String sandboxId,
                                              @Nullable
                                              String conceptKey,
                                              @NonNull
                                              @NonNull Duration stagnationThreshold)
        Description copied from interface: ResourceLockRepository
        Lock a resource temporarily based on identifying information. Will return a token representing the lock that is suitable to be passed to ResourceLockRepository.unlockResource(String) when the work requiring the lock is complete. If unable to acquire the lock, a ResourceLockException is thrown.
        Specified by:
        lockResourceTemporarily in interface ResourceLockRepository<JpaResourceLock,​UUID>
        Parameters:
        contextId - a unique id of the obtain a lock for
        type - grouping for the contextId
        sandboxId - sandbox associated with the entity
        conceptKey - a generally descriptive key that identifies the overall concept for which the lock is taking place
        stagnationThreshold - how long the lock should be held
        Returns:
        The token that identifies the lock
      • purgeLocks

        public void purgeLocks​(@NonNull
                               @NonNull Class<?> type,
                               @NonNull
                               @NonNull Duration lockTtl)
        This table will be highly susceptible to deadlocks. Therefore, we should execute small delete transactions in the same order that the locks were created should avoid these issues. Additionally, the failure to release a single lock should not stop us from releasing all other identified locks.
        Specified by:
        purgeLocks in interface ResourceLockRepository<JpaResourceLock,​UUID>
        Parameters:
        type - The type of lock that is to be removed
        lockTtl - How long the lock should be held. If the lock is too old, then it should be removed.
      • identifyLocksToBePurged

        protected List<JpaResourceLock> identifyLocksToBePurged​(@NonNull
                                                                @NonNull Class<?> type,
                                                                @NonNull
                                                                @NonNull Duration lockTtl,
                                                                @Nullable
                                                                String startingId)
        Identifies locks that are eligible to be purged
        Parameters:
        type - The type of lock that should be identified
        lockTtl - How long the lock should be held. If the lock is too old, then it should be removed.
        startingId - The id from which to start the retrieval of a batch of candidate records.
        Returns:
        The list of locks that are eligible to be purged
      • releaseLock

        protected void releaseLock​(@NonNull
                                   @NonNull JpaResourceLock lock)
        Deletes the provided lock
        Parameters:
        lock - The lock that is to be released
      • getSandboxIdOrDefault

        protected String getSandboxIdOrDefault​(@Nullable
                                               String sandboxId)
      • getConceptKeyOrDefault

        protected String getConceptKeyOrDefault​(@Nullable
                                                String conceptKey)