Class DefaultLockRepository

java.lang.Object
org.springframework.integration.jdbc.lock.DefaultLockRepository
All Implemented Interfaces:
Closeable, AutoCloseable, Aware, InitializingBean, SmartInitializingSingleton, ApplicationContextAware, Lifecycle, Phased, SmartLifecycle, LockRepository

The default implementation of the LockRepository based on the table from the script presented in the org/springframework/integration/jdbc/schema-*.sql.

This repository can't be shared between different JdbcLockRegistry instances. Otherwise, it opens a possibility to break Lock contract, where JdbcLockRegistry uses non-shared ReentrantLocks for local synchronizations.

This class implements SmartLifecycle and calls SELECT COUNT(REGION) FROM %sLOCK query according to the provided prefix on start() to check if required table is present in DB. The application context will fail to start if the table is not present. This check can be disabled via setCheckDatabaseOnStart(boolean).

Since:
4.3
Author:
Dave Syer, Artem Bilan, Glenn Renfro, Gary Russell, Alexandre Strubel, Ruslan Stelmachenko
  • Field Details

    • DEFAULT_TABLE_PREFIX

      public static final String DEFAULT_TABLE_PREFIX
      Default value for the table prefix property.
      See Also:
    • DEFAULT_TTL

      public static final Duration DEFAULT_TTL
      Default value for the time-to-live property.
  • Constructor Details

    • DefaultLockRepository

      public DefaultLockRepository(DataSource dataSource)
      Constructor that initializes the client id that will be associated for all the locks persisted by the store instance to a random UUID.
      Parameters:
      dataSource - the DataSource used to maintain the lock repository.
    • DefaultLockRepository

      public DefaultLockRepository(DataSource dataSource, String id)
      Constructor that allows the user to specify a client id that will be associated for all the locks persisted by the store instance.
      Parameters:
      dataSource - the DataSource used to maintain the lock repository.
      id - the client id to be associated with locks handled by the repository.
      Since:
      4.3.13
  • Method Details

    • setRegion

      public void setRegion(String region)
      A unique grouping identifier for all locks persisted with this store. Using multiple regions allows the store to be partitioned (if necessary) for different purposes. Defaults to DEFAULT.
      Parameters:
      region - the region name to set
    • setPrefix

      public void setPrefix(String prefix)
      Specify the prefix for target database table used from queries.
      Parameters:
      prefix - the prefix to set (default INT_).
    • setTimeToLive

      public void setTimeToLive(int timeToLive)
      Specify the time (in milliseconds) to expire deadlocks.
      Parameters:
      timeToLive - the time to expire deadlocks.
    • setTransactionManager

      public void setTransactionManager(PlatformTransactionManager transactionManager)
      Set a PlatformTransactionManager for operations. Otherwise, a primary PlatformTransactionManager bean is obtained from the application context.
      Parameters:
      transactionManager - the PlatformTransactionManager to use.
      Since:
      6.0
    • setApplicationContext

      public void setApplicationContext(ApplicationContext applicationContext) throws BeansException
      Specified by:
      setApplicationContext in interface ApplicationContextAware
      Throws:
      BeansException
    • setUpdateQuery

      public void setUpdateQuery(String updateQuery)
      Set a custom UPDATE query for a lock record. The getUpdateQuery() can be used as a template for customization. The default query is:
       
        UPDATE %sLOCK
       			SET CLIENT_ID=?, CREATED_DATE=?
       			WHERE REGION=? AND LOCK_KEY=? AND (CLIENT_ID=? OR CREATED_DATE<?)
       
       
      Parameters:
      updateQuery - the query to update a lock record.
      Since:
      6.1
      See Also:
    • getUpdateQuery

      public String getUpdateQuery()
      Return the current update query. Can be used in a setter as a concatenation of the default query and some extra hint.
      Returns:
      the current update query.
      Since:
      6.1
      See Also:
    • setInsertQuery

      public void setInsertQuery(String insertQuery)
      Set a custom INSERT query for a lock record. The getInsertQuery() can be used as a template for customization. The default query is INSERT INTO %sLOCK (REGION, LOCK_KEY, CLIENT_ID, CREATED_DATE) VALUES (?, ?, ?, ?). For example a PostgreSQL ON CONFLICT DO NOTHING hint can be provided like this:
       
        lockRepository.setInsertQuery(lockRepository.getInsertQuery() + " ON CONFLICT DO NOTHING");
       
       
      Parameters:
      insertQuery - the insert query for a lock record.
      Since:
      6.1
      See Also:
    • getInsertQuery

      public String getInsertQuery()
      Return the current insert query. Can be used in a setter as a concatenation of the default query and some extra hint.
      Returns:
      the current insert query.
      Since:
      6.1
      See Also:
    • setRenewQuery

      public void setRenewQuery(String renewQuery)
      Set a custom INSERT query for a lock record. The getRenewQuery() can be used as a template for customization. The default query is:
       
        UPDATE %sLOCK
       			SET CREATED_DATE=?
       			WHERE REGION=? AND LOCK_KEY=? AND CLIENT_ID=?
       
       
      Parameters:
      renewQuery - the update query to renew a lock record.
      Since:
      6.1
      See Also:
    • getRenewQuery

      public String getRenewQuery()
      Return the current renew query. Can be used in a setter as a concatenation of a default query and some extra hint.
      Returns:
      the current renew query.
      Since:
      6.1
      See Also:
    • afterPropertiesSet

      public void afterPropertiesSet()
      Specified by:
      afterPropertiesSet in interface InitializingBean
    • afterSingletonsInstantiated

      public void afterSingletonsInstantiated()
      Specified by:
      afterSingletonsInstantiated in interface SmartInitializingSingleton
    • setCheckDatabaseOnStart

      public void setCheckDatabaseOnStart(boolean checkDatabaseOnStart)
      The flag to perform a database check query on start or not.
      Parameters:
      checkDatabaseOnStart - false to not perform the database check.
      Since:
      6.2
    • isAutoStartup

      public boolean isAutoStartup()
      Specified by:
      isAutoStartup in interface SmartLifecycle
    • start

      public void start()
      Specified by:
      start in interface Lifecycle
    • stop

      public void stop()
      Specified by:
      stop in interface Lifecycle
    • isRunning

      public boolean isRunning()
      Specified by:
      isRunning in interface Lifecycle
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Specified by:
      close in interface LockRepository
    • delete

      public void delete(String lock)
      Description copied from interface: LockRepository
      Remove a lock from this repository.
      Specified by:
      delete in interface LockRepository
      Parameters:
      lock - the lock to remove.
    • acquire

      public boolean acquire(String lock)
      Description copied from interface: LockRepository
      Acquire a lock for a key.
      Specified by:
      acquire in interface LockRepository
      Parameters:
      lock - the key for lock to acquire.
      Returns:
      acquired or not.
    • isAcquired

      public boolean isAcquired(String lock)
      Description copied from interface: LockRepository
      Check if a lock is held by this repository.
      Specified by:
      isAcquired in interface LockRepository
      Parameters:
      lock - the lock to check.
      Returns:
      acquired or not.
    • deleteExpired

      public void deleteExpired()
      Description copied from interface: LockRepository
      Remove all the expired locks.
      Specified by:
      deleteExpired in interface LockRepository
    • renew

      public boolean renew(String lock)
      Description copied from interface: LockRepository
      Renew the lease for a lock.
      Specified by:
      renew in interface LockRepository
      Parameters:
      lock - the lock to renew.
      Returns:
      renewed or not.