Skip to content

DataSource Configuration DBCP 2 and Commons Pool 2 (New in 0.15.0)

Holger Thurow edited this page Feb 22, 2021 · 9 revisions

As of 0.15.0 you can choose to use the superior DBCP 2 component for creation of a connection pool. It "provides increased performance, JMX support as well as numerous other new features compared to DBCP 1.x" (see http://commons.apache.org/proper/commons-dbcp and http://commons.apache.org/proper/commons-dbcp/changes-report.html). To make Simple-JNDI using this component instead of its own DataSource implementation and Commons DBCP 1 you have to set a new configuration parameter in jndi.properties:

java.naming.factory.object = org.apache.commons.dbcp2.BasicDataSourceFactory

Note that as of 0.21.0 you can register the ObjectFactory in the file itself. See Usage of 3rd party SPI ObjectFactory implementations (New in 0.21.0).

The following shows an example of a DataSource that will be available under the lookup key "application/ds/TestDS":

In root/application/ds/TestDS.properties

type=javax.sql.DataSource
driverClassName=org.gjt.mm.mysql.Driver
url=jdbc:mysql://localhost/testdb
username=user_name
password=password

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.

All possible parameters can be looked up at

BasicDataSource Configuration Parameters
BasicDataSourceFactory source code
BasicDataSource source code

Note that you can not mix up commons-dbcp with commons-dbcp2 DataSources.

Clone this wiki locally