DataSources

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.

Connection pooling

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:

ParamTypeDefault
dbcpValidationQueryStringnot set
dbcpDefaultReadOnlytrue/falsefalse
dbcpDefaultAutoCommittrue/falsetrue
dbcpMaxActiveSee DBCP's GenericObjectPoolGenericObjectPool.DEFAULT_MAX_ACTIVE
dbcpWhenExhaustedActionSee DBCP's GenericObjectPoolGenericObjectPool.DEFAULT_WHEN_EXHAUSTED_ACTION
dbcpMaxWaitSee DBCP's GenericObjectPoolGenericObjectPool.DEFAULT_MAX_WAIT
dbcpMaxIdleSee DBCP's GenericObjectPoolGenericObjectPool.DEFAULT_MAX_IDLE
dbcpMinIdleSee DBCP's GenericObjectPoolGenericObjectPool.DEFAULT_MIN_IDLE
dbcpTestOnBorrowSee DBCP's GenericObjectPoolGenericObjectPool.DEFAULT_TEST_ON_BORROW
dbcpTestOnReturnSee DBCP's GenericObjectPoolGenericObjectPool.DEFAULT_TEST_ON_RETURN
dbcpTimeBetweenEvictionRunsMillisSee DBCP's GenericObjectPoolGenericObjectPool.DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS
dbcpNumTestsPerEvictionRunSee DBCP's GenericObjectPoolGenericObjectPool.DEFAULT_NUM_TESTS_PER_EVICTION_RUN
dbcpMinEvictableIdleTimeMillisSee DBCP's GenericObjectPoolGenericObjectPool.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS
dbcpTestWhileIdleSee DBCP's GenericObjectPoolGenericObjectPool.DEFAULT_TEST_WHILE_IDLE
dbcpSoftMinEvictableIdleTimeMillisSee DBCP's GenericObjectPoolGenericObjectPool.DEFAULT_SOFT_MIN_EVICTABLE_IDLE_TIME_MILLIS