Skip to content

DataSource Configuration (commons dbcp 1)

Holger Thurow edited this page Jul 9, 2017 · 1 revision

There are five mandatory parameters: url, driver, user, password, type. The following shows an example of a DataSource that will be available under the lookup key application/ds/TestDS.

application/ds/TestDS.properties
    type=javax.sql.DataSource
    driver=org.gjt.mm.mysql.Driver
    url=jdbc:mysql://localhost/testdb
    user=testuser
    password=testing

The code to obtain it would be:

      InitialContext ctxt = new InitialContext();
      DataSource ds = (DataSource) ctxt.lookup("application/ds/TestDS");

This example uses a delimiter of '/', which must be set with the org.osjava.sj.delimiter property.

Often when using a DataSource you will want to pool the Connections the DataSource is handing out. Simple-JNDI delegates to the Jakarta Commons DBCP project for this feature so you will need commons-dbcp, commons-pool and commons-collections jars in your classpath.

The feature is turned on by adding a sub-parameter of 'pool=<pool-name>' in your datasource properties file. The above shown application1/ds/TestDS.properties file then looks like:

    type=javax.sql.DataSource
    driver=org.gjt.mm.mysql.Driver
    url=jdbc:mysql://localhost/testdb
    user=testuser
    password=testing
    pool=apachePool

Note: The pool variable used to be a boolean 'true' variable, but now a pool name is provided. This is fully backwards compatible.

To fine tune your DataSource there are the following parameters available:

Param Type Default
dbcpValidationQuery String not set
dbcpDefaultReadOnly true/false false
dbcpDefaultAutoCommit true/false true
dbcpMaxActive See DBCP's GenericObjectPool GenericObjectPool.DEFAULT_MAX_ACTIVE
dbcpWhenExhaustedAction See DBCP's GenericObjectPool GenericObjectPool.DEFAULT_WHEN_EXHAUSTED_ACTION
dbcpMaxWait See DBCP's GenericObjectPool GenericObjectPool.DEFAULT_MAX_WAIT
dbcpMaxIdle See DBCP's GenericObjectPool GenericObjectPool.DEFAULT_MAX_IDLE
dbcpMinIdle See DBCP's GenericObjectPool GenericObjectPool.DEFAULT_MIN_IDLE
dbcpTestOnBorrow See DBCP's GenericObjectPool GenericObjectPool.DEFAULT_TEST_ON_BORROW
dbcpTestOnReturn See DBCP's GenericObjectPool GenericObjectPool.DEFAULT_TEST_ON_RETURN
dbcpTimeBetweenEvictionRunsMillis See DBCP's GenericObjectPool GenericObjectPool.DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS
dbcpNumTestsPerEvictionRun See DBCP's GenericObjectPool GenericObjectPool.DEFAULT_NUM_TESTS_PER_EVICTION_RUN
dbcpMinEvictableIdleTimeMillis See DBCP's GenericObjectPool GenericObjectPool.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS
dbcpTestWhileIdle See DBCP's GenericObjectPool GenericObjectPool.DEFAULT_TEST_WHILE_IDLE
dbcpSoftMinEvictableIdleTimeMillis See DBCP's GenericObjectPool GenericObjectPool.DEFAULT_SOFT_MIN_EVICTABLE_IDLE_TIME_MILLIS