The most popular object to get from JNDI is a object of type javax.sql.DataSource, allowing the developer to obtain JDBC connections to databases. Simple-JNDI supports this out of the box.
There are four mandatory parameters for a DataSource in Simple-JNDI, and four optional parameters (see next section). The mandatory parameters are url, driver, user, password. The following shows an example of a DataSource that will be available under the lookup key application1/ds/TestDS.
application1/ds.properties
TestDS.type=javax.sql.DataSource
TestDS.driver=org.gjt.mm.mysql.Driver
TestDS.url=jdbc:mysql://localhost/testdb
TestDS.user=testuser
TestDS.password=testing
The code to obtain it would be:
InitialContext ctxt = new InitialContext();
DataSource ds = (DataSource) ctxt.lookup("application1/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. For example, ApacheDS.pool=apachePool, will turn DBCP Connection pooling on for the ApacheDS datasource under a name of 'apachePool'.
Note: The pool variable used to be a boolean 'true' variable, but now a pool name is provided. This is fully backwards compatible as you'll just get a pool name of 'true'.
The following parameters are used to configure DBCP:
| 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 |