Package org.togglz.core.repository.jdbc
Class JDBCStateRepository
java.lang.Object
org.togglz.core.repository.jdbc.JDBCStateRepository
- All Implemented Interfaces:
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
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final DataSourceprotected final booleanprotected final MapSerializerprotected final Stringprotected final boolean -
Constructor Summary
ConstructorsConstructorDescriptionJDBCStateRepository(DataSource dataSource) Constructor ofJDBCStateRepository.JDBCStateRepository(DataSource dataSource, String tableName) Constructor ofJDBCStateRepository.Private constructor initializing the class from a builder -
Method Summary
Modifier and TypeMethodDescriptionprotected voidafterSchemaMigration(Connection connection) Method called after the database schema migration has been performed.protected voidbeforeSchemaMigration(Connection connection) Method called before the database schema migration is performed.getFeatureState(Feature feature) Get the persisted state of a feature from the repository.protected Stringprotected voidMethod for creating/migrating the database schemastatic JDBCStateRepository.BuildernewBuilder(DataSource dataSource) Creates a new builder for creating aJDBCStateRepository.voidsetFeatureState(FeatureState featureState) Persist the supplied feature state.
-
Field Details
-
dataSource
-
tableName
-
serializer
-
noCommit
protected final boolean noCommit -
usePostgresTextColumns
protected final boolean usePostgresTextColumns
-
-
Constructor Details
-
JDBCStateRepository
Constructor ofJDBCStateRepository. A database table calledTOGGLZwill be created automatically for you.- Parameters:
dataSource- The JDBCDataSourceto obtain connections from- See Also:
-
JDBCStateRepository
Constructor ofJDBCStateRepository. The database table will be created automatically for you.- Parameters:
dataSource- The JDBCDataSourceto obtain connections fromtableName- The name of the database table to use
-
JDBCStateRepository
Private constructor initializing the class from a builder
-
-
Method Details
-
migrateSchema
protected void migrateSchema()Method for creating/migrating the database schema -
beforeSchemaMigration
Method called before the database schema migration is performed. -
afterSchemaMigration
Method called after the database schema migration has been performed. -
getFeatureState
Description copied from interface:StateRepositoryGet the persisted state of a feature from the repository. If the repository doesn't contain any information regarding this feature it must returnnull.- Specified by:
getFeatureStatein interfaceStateRepository- Parameters:
feature- The feature to read the state for- Returns:
- The persisted feature state or
null
-
setFeatureState
Description copied from interface:StateRepositoryPersist the supplied feature state. The repository implementation must ensure that subsequent calls toStateRepository.getFeatureState(Feature)return the same state as persisted using this method.- Specified by:
setFeatureStatein interfaceStateRepository- Parameters:
featureState- The feature state to persist
-
insertTableName
-
newBuilder
Creates a new builder for creating aJDBCStateRepository.- Parameters:
dataSource- theDataSourceTogglz should use to obtain JDBC connections
-