Class JDBCStateRepository

java.lang.Object
org.togglz.core.repository.jdbc.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 Details

    • dataSource

      protected final DataSource dataSource
    • tableName

      protected final String tableName
    • serializer

      protected final MapSerializer serializer
    • noCommit

      protected final boolean noCommit
    • usePostgresTextColumns

      protected final boolean usePostgresTextColumns
  • Constructor Details

    • JDBCStateRepository

      public JDBCStateRepository(DataSource dataSource)
      Constructor of JDBCStateRepository. A database table called TOGGLZ will be created automatically for you.
      Parameters:
      dataSource - The JDBC DataSource to obtain connections from
      See Also:
    • 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

      public JDBCStateRepository(JDBCStateRepository.Builder builder)
      Private constructor initializing the class from a builder
  • Method Details

    • 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
    • setFeatureState

      public void setFeatureState(FeatureState featureState)
      Description copied from interface: StateRepository
      Persist the supplied feature state. The repository implementation must ensure that subsequent calls to StateRepository.getFeatureState(Feature) return the same state as persisted using this method.
      Specified by:
      setFeatureState in interface StateRepository
      Parameters:
      featureState - The feature state to persist
    • insertTableName

      protected String insertTableName(String s)
    • newBuilder

      public static JDBCStateRepository.Builder newBuilder(DataSource dataSource)
      Creates a new builder for creating a JDBCStateRepository.
      Parameters:
      dataSource - the DataSource Togglz should use to obtain JDBC connections