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.
Modifier and Type | Class and Description |
---|---|
static class |
JDBCStateRepository.Builder
Builder for a
JDBCStateRepository . |
Modifier and Type | Field and Description |
---|---|
protected DataSource |
dataSource |
protected Log |
log |
protected boolean |
noCommit |
protected MapSerializer |
serializer |
protected String |
tableName |
Constructor and Description |
---|
JDBCStateRepository(DataSource dataSource)
Constructor of
JDBCStateRepository . |
JDBCStateRepository(DataSource dataSource,
String tableName)
Constructor of
JDBCStateRepository . |
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 |
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 |
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 |
Modifier and Type | Method and Description |
---|---|
protected void |
afterSchemaMigration(Connection connection)
Method called after the database schema migration has been performed.
|
protected void |
beforeSchemaMigration(Connection connection)
Method called before the database schema migration is performed.
|
FeatureState |
getFeatureState(Feature feature)
Get the persisted state of a feature from the repository.
|
protected void |
migrateSchema()
Method for creating/migrating the database schema
|
static JDBCStateRepository.Builder |
newBuilder(DataSource dataSource)
Creates a new builder for creating a
JDBCStateRepository . |
void |
setFeatureState(FeatureState featureState)
Persist the supplied feature state.
|
protected final Log log
protected final DataSource dataSource
protected final String tableName
protected final MapSerializer serializer
protected final boolean noCommit
public JDBCStateRepository(DataSource dataSource)
JDBCStateRepository
. A database table called TOGGLZ
will be created automatically for
you.dataSource
- The JDBC DataSource
to obtain connections fromJDBCStateRepository(DataSource, String)
public JDBCStateRepository(DataSource dataSource, String tableName)
JDBCStateRepository
. The database table will be created automatically for you.dataSource
- The JDBC DataSource
to obtain connections fromtableName
- The name of the database table to use@Deprecated public JDBCStateRepository(DataSource dataSource, String tableName, boolean createTable)
newBuilder(DataSource)
to create a builder that can be used to configure all
aspects of the repository in a fluent wayJDBCStateRepository
.dataSource
- The JDBC DataSource
to obtain connections fromtableName
- The name of the database table to usecreateTable
- If set to true
, the table will be automatically created if it is missing@Deprecated public JDBCStateRepository(DataSource dataSource, String tableName, boolean createTable, MapSerializer serializer)
newBuilder(DataSource)
to create a builder that can be used to configure all
aspects of the repository in a fluent wayJDBCStateRepository
.dataSource
- The JDBC DataSource
to obtain connections fromtableName
- The name of the database table to usecreateTable
- If set to true
, the table will be automatically created if it is missingserializer
- The MapSerializer
for storing parameterspublic JDBCStateRepository(DataSource dataSource, String tableName, boolean createTable, MapSerializer serializer, boolean noCommit)
newBuilder(DataSource)
to create a builder that can be used to configure all
aspects of the repository in a fluent wayJDBCStateRepository
.dataSource
- The JDBC DataSource
to obtain connections fromtableName
- The name of the database table to usecreateTable
- If set to true
, the table will be automatically created if it is missingserializer
- The MapSerializer
for storing parametersprotected void migrateSchema()
protected void beforeSchemaMigration(Connection connection)
protected void afterSchemaMigration(Connection connection)
public FeatureState getFeatureState(Feature feature)
StateRepository
null
.getFeatureState
in interface StateRepository
feature
- The feature to read the state fornull
public void setFeatureState(FeatureState featureState)
StateRepository
StateRepository.getFeatureState(Feature)
return the same state as persisted using this method.setFeatureState
in interface StateRepository
featureState
- The feature state to persistpublic static JDBCStateRepository.Builder newBuilder(DataSource dataSource)
JDBCStateRepository
.dataSource
- the DataSource
Togglz should use to obtain JDBC connectionsCopyright © 2017. All Rights Reserved.