Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to manage configuration data

I am working on a product suite which has 4 products. Right now, all of the configuration data is either in the XML or properties files.This approach is not maintainable as we have to manage different configuration file for different environment(for e.g. Production, Development etc).

So, what is the best way to handle configuration data?

Also, can we modularize this into a separate module? So that all products can use this module. We don't want to use the property files. I am looking for a solution in which we can move all of the configuration specific code as a new configuration module and persist all the configuration data in database.

like image 960
Shekhar Avatar asked Feb 24 '10 06:02

Shekhar


People also ask

What is good configuration management?

Configuration management is a systems engineering process for establishing consistency of a product's attributes throughout its life. In the technology world, configuration management is an IT management process that tracks individual configuration items of an IT system.

What is configuration data management?

Configuration management (CM) is a systems engineering process for establishing and maintaining consistency of a product's performance, functional, and physical attributes with its requirements, design, and operational information throughout its life.

Is IT better to keep configurations in config file or database?

We find it a better way do do config than a file because it means you can easily programmatically change config values through an admin interface when needed, which can enforce logic around what can go into each setting. You can't do that so easily with a file (though, of course, it is possible).


3 Answers

Using commons-configuration you have a unified API for accessing the properties, no matter how they are represented - .properties, xml, JNDI, etc. For example:

config.properties:

jdbcHost=192.168.12.35
jdbcUsername=dbuser
jdbcPassword=pass

config.xml:

<config>
   <jdbcHost>192.168.12.35</jdbcHost>
   <jdbcUsername>dbuser</jdbcUsername>
   <jdbcPassword>pass</jdbcPassword>
</config>

in both cases they will be accessible with something like:

String host = config.getString("jdbcHost");
like image 169
Bozho Avatar answered Oct 06 '22 20:10

Bozho


You're almost there... I would keep your same approach and pull in the correct configuration file for the instance of the application that is running by a method similar to either of the following:

  1. Name all of your configuration files differently and have your application pull them in by some unique criteria (username, hostname, etc.):

    • production.properties
    • developer1.properties
    • developer2.properties
  2. Keep them outside the codebase in a location based on an environment variable that the application assumes exists:

    • YOURAPP_CONFIG_DIR/server_config.xml
    • YOURAPP_CONFIG_DIR/database_config.properties

I've even used a combination of these approaches on the same project (#1 for build process configurations and #2 for runtime configurations).

like image 22
Nicole Avatar answered Oct 06 '22 21:10

Nicole


If your applications work with a database, you can create a "configuration" table as follows:

create table configuration (mode char(3), key varchar(255), value varchar(1023));

You would initialize it using an init script, say init.sql with contents along the lines of:

insert into configuration values ('pro', 'param1', 'value1'); -- production
insert into configuration values ('dev', 'param1', 'value1'); -- development
insert into configuration values ('tst', 'param1', 'value1'); -- testing
...

The benefits of this approach are as follows:

  • you version the script together with your code
  • you can easily extend it to include per-user or per-group settings by adding a user/group id
  • you can change the settings at runtime if you need to do so
  • you get to use the same stack (JPA + DAO, Cayenne...) you normally use to handle core application data to handle configuration data
like image 25
Tomislav Nakic-Alfirevic Avatar answered Oct 06 '22 19:10

Tomislav Nakic-Alfirevic