RSS Feed for This PostCurrent Article

Configure Different Database Settings for Different JVMs in iBATIS

I have a common class which is used to create the SqlMapClient  object to access the database. The class is used by multiple Java processes.

The problem is that I want each Java process to have specific database settings, and I do not want to maintain redundant information in different SQL mapping configuration files, e.g. the references to the SQL mapping files. 

The requirements

  1. If no process specific configuration, then all Java processes use default values
  2. If I want to configure process specific information, I need to specify the properties in a file, and specify it during process startup.

Below are the iBATIS configurations I want to be specific for each Java process if needed.

<!-- OPTIONAL PROPERTIES BELOW -->
<property name="initialSize" value="5"/>
<property name="maxActive" value="100"/>
<property name="maxIdle" value="20"/>
<property name="maxWait" value="60000"/>
<property name="poolPreparedStatements" value="true"/>
<property name="validationQuery" value="select 0 from dual"/>
<property name="testOnBorrow" value="true"/>

To achieve what I want, first I need to make sure the values are retrieve from properties

<!-- OPTIONAL PROPERTIES BELOW -->
<property name="initialSize" value="${db.connection.initialSize}"/>
<property name="maxActive" value="${db.connection.maxActive}"/>
<property name="maxIdle" value="${db.connection.maxIdle}"/>
<property name="maxWait" value="${db.connection.maxWait}"/>
<property name="poolPreparedStatements" value="${db.connection.poolPreparedStatements}"/>
<property name="validationQuery" value="${db.connection.validationQuery}"/>
<property name="testOnBorrow" value="${db.connection.testOnBorrow}"/>

In the global property files database.properties, I specify the default values

# General database values
db.connection.initialSize=10
db.connection.maxActive=800
db.connection.maxIdle=100
db.connection.maxWait=6000
db.connection.poolPreparedStatements=true
db.connection.validationQuery=select 0 from dual
db.connection.testOnBorrow=true
 

In the Java code, I use the SqlMapClientBuilder.buildSqlMapClient(Reader, Properties) method to achieve this

static {
    try {
        
        SqlMapClient sqlMap = buildSqlMapClient("sqlmap-config.xml", System.getProperty("db-config"));
        .....
    } ...
}
 
/**
 * Build SQL map client and optional load additional properties if specified in the start up command line
 *
 * @param resource               SQL map file
 * @param optionalPropertiesFile Optional properties file name
 * @return A SQL map client
 * @throws Exception Any exception
 */
private static SqlMapClient buildSqlMapClient(String resource, String optionalPropertiesFile) throws Exception {
    Properties properties = new Properties();
    properties.load(Resources.getResourceAsReader("database.properties"));
    Reader reader = Resources.getResourceAsReader(resource);
    if (StringUtils.isNotBlank(optionalPropertiesFile)) {
        properties.load(Resources.getResourceAsReader(optionalPropertiesFile));
    }
    return SqlMapClientBuilder.buildSqlMapClient(reader, properties);
} 

As you can see, I can pass the customised properties file into the code using the “db-config” property which can specified during run time.

Let’s say I create the following properties file called “customised.properties”

# customized database values
db.connection.initialSize=100
db.connection.maxActive=1000
 
 

I can tell the Java process to use this file by defining the start up properties.

java -Ddb-config=customised.properties com.myapp


Trackback URL


RSS Feed for This PostPost a Comment

CAPTCHA Image
Refresh Image
*