Configuration stored in a database. The properties are retrieved from a table containing at least one column for the keys, and one column for the values. It's possible to store several configurations in the same table by adding a column containing the name of the configuration. The name of the table and the columns have to be specified using the corresponding properties.
The recommended way to create an instance of {@code DatabaseConfiguration}is to use a configuration builder. The builder is configured with a special parameters object defining the database structures used by the configuration. Such an object can be created using the {@code database()}method of the {@code Parameters} class. See the examples below for moredetails.
Example 1 - One configuration per table
CREATE TABLE myconfig ( `key` VARCHAR NOT NULL PRIMARY KEY, `value` VARCHAR ); INSERT INTO myconfig (key, value) VALUES ('foo', 'bar'); BasicConfigurationBuilder builder = new BasicConfigurationBuilder(DatabaseConfiguration.class); builder.configure( Parameters.database() .setDataSource(dataSource) .setTable("myconfig") .setKeyColumn("key") .setValueColumn("value") ); Configuration config = builder.getConfiguration(); String value = config.getString("foo");
Example 2 - Multiple configurations per table
CREATE TABLE myconfigs ( `name` VARCHAR NOT NULL, `key` VARCHAR NOT NULL, `value` VARCHAR, CONSTRAINT sys_pk_myconfigs PRIMARY KEY (`name`, `key`) ); INSERT INTO myconfigs (name, key, value) VALUES ('config1', 'key1', 'value1'); INSERT INTO myconfigs (name, key, value) VALUES ('config2', 'key2', 'value2'); BasicConfigurationBuilder builder = new BasicConfigurationBuilder(DatabaseConfiguration.class); builder.configure( Parameters.database() .setDataSource(dataSource) .setTable("myconfigs") .setKeyColumn("key") .setValueColumn("value") .setConfigurationNameColumn("name") .setConfigurationName("config1") ); Configuration config1 = new DatabaseConfiguration(dataSource, "myconfigs", "name", "key", "value", "config1"); String value1 = conf.getString("key1");
The configuration can be instructed to perform commits after database updates. This is achieved by setting the {@code commits} parameter of theconstructors to
true. If commits should not be performed (which is the default behavior), it should be ensured that the connections returned by the {@code DataSource} are in auto-commit mode.
Note: Like JDBC itself, protection against SQL injection is left to the user.
@since 1.0
@author
Emmanuel Bourg
@version $Id: DatabaseConfiguration.java 1624601 2014-09-12 18:04:36Z oheger $