Class JDBCStateRepository

  • All Implemented Interfaces:
    StateRepository

    public class JDBCStateRepository
    extends Object
    implements StateRepository

    This repository implementation can be used to store the feature state in SQL database using the standard JDBC API.

    JDBCStateRepository stores the feature state in a single database table. You can choose the name of this table using a constructor argument. If the repository doesn't find the required table in the database, it will automatically create it.

    The database table has the following format:

     CREATE TABLE <table> (
       FEATURE_NAME VARCHAR(100) PRIMARY KEY,
       FEATURE_ENABLED INTEGER,
       STRATEGY_ID VARCHAR(200),
       STRATEGY_PARAMS VARCHAR(2000)
     )
     

    The class provides a builder which can be used to configure the repository:

     StateRepository repository = JDBCStateRepository.newBuilder(dataSource)
         .tableName("features")
         .createTable(false)
         .serializer(DefaultMapSerializer.singleline())
         .noCommit(true)
         .build();
     

    Please note that the structure of the database table changed with version 2.0.0 because of the new extensible activation strategy mechanism. The table structure will be automatically migrated to the new format.

    Author:
    Christian Kaltepoth
    • Field Detail

      • dataSource

        protected final DataSource dataSource
      • tableName

        protected final String tableName
      • noCommit

        protected final boolean noCommit
      • usePostgresTextColumns

        protected final boolean usePostgresTextColumns
    • Constructor Detail

      • JDBCStateRepository

        public JDBCStateRepository​(DataSource dataSource,
                                   String tableName)
        Constructor of JDBCStateRepository. The database table will be created automatically for you.
        Parameters:
        dataSource - The JDBC DataSource to obtain connections from
        tableName - The name of the database table to use
      • JDBCStateRepository

        @Deprecated
        public JDBCStateRepository​(DataSource dataSource,
                                   String tableName,
                                   boolean createTable)
        Deprecated.
        use newBuilder(DataSource) to create a builder that can be used to configure all aspects of the repository in a fluent way
        Constructor of JDBCStateRepository.
        Parameters:
        dataSource - The JDBC DataSource to obtain connections from
        tableName - The name of the database table to use
        createTable - If set to true, the table will be automatically created if it is missing
      • JDBCStateRepository

        @Deprecated
        public JDBCStateRepository​(DataSource dataSource,
                                   String tableName,
                                   boolean createTable,
                                   MapSerializer serializer)
        Deprecated.
        use newBuilder(DataSource) to create a builder that can be used to configure all aspects of the repository in a fluent way
        Constructor of JDBCStateRepository.
        Parameters:
        dataSource - The JDBC DataSource to obtain connections from
        tableName - The name of the database table to use
        createTable - If set to true, the table will be automatically created if it is missing
        serializer - The MapSerializer for storing parameters
      • JDBCStateRepository

        public JDBCStateRepository​(DataSource dataSource,
                                   String tableName,
                                   boolean createTable,
                                   MapSerializer serializer,
                                   boolean noCommit)
        Deprecated.
        use newBuilder(DataSource) to create a builder that can be used to configure all aspects of the repository in a fluent way
        Constructor of JDBCStateRepository.
        Parameters:
        dataSource - The JDBC DataSource to obtain connections from
        tableName - The name of the database table to use
        createTable - If set to true, the table will be automatically created if it is missing
        serializer - The MapSerializer for storing parameters
    • Method Detail

      • migrateSchema

        protected void migrateSchema()
        Method for creating/migrating the database schema
      • beforeSchemaMigration

        protected void beforeSchemaMigration​(Connection connection)
        Method called before the database schema migration is performed.
      • afterSchemaMigration

        protected void afterSchemaMigration​(Connection connection)
        Method called after the database schema migration has been performed.
      • getFeatureState

        public FeatureState getFeatureState​(Feature feature)
        Description copied from interface: StateRepository
        Get the persisted state of a feature from the repository. If the repository doesn't contain any information regarding this feature it must return null.
        Specified by:
        getFeatureState in interface StateRepository
        Parameters:
        feature - The feature to read the state for
        Returns:
        The persisted feature state or null