Switching Datasource connection pooling in TomEE/OpenEJB


DataSource Pooling

Pooling its datasource connection in a real application is very important to respect its SLA.

OpenEJB (and by the way TomEE) uses commons-dbcp since many years. It works well if well configured, but suffers from an old design. What’s more, some other libraries are today interesting and are good alternatives.

Two famous of them are tomcat-jdbc (a fork from commons-dbcp) and BoneCP. Both promises great performances (particulary in high load).

Many refactoring has been done in OpenEJB to allow switching pool library keeping the JTA management (supported by commons-dbcp but no others). The result is mainly a “magic” abstract class called org.apache.openejb.resource.jdbc.pool.PoolDataSourceCreator. Its API obliges you to implement a way to pool a provided datasource, to create a pooled datasource and to destroy what you created.

But in a user life, you don’t need it, and you can simply use what is already done through configuration :).

Configuration/Usage

First, you declare your datasources as usual, either in tomee.xml/openejb.xml in <Resource /> tag or through system properties mecanisms (new://Resource?type=DataSource…).

By default in embedded mode, you are with commons-dbcp. In TomEE, current default is tomcat-jdbc but it can be reworked before the release to stick to dbcp for compatibility reasons.

Then, there is a global system property to configure – globally – the pool library to use : openejb.jdbc.datasource-creator

You can set this property to the qualified name of your pool creator or to some aliases: dbcp, tomcat, bonecp.

Then if you want to change the pooling for a single datasource, you can use a resource property: DataSourceCreator (letters case is important). Values should respect the previous rules too.

For all the properties, use the properties of your configured pool.  Some properties can be automatically converted but IMO is better to stick to what you use. Normally if you use a property not supported you should get a warning about it.

Note about libraries: in embedded mode only, commons-dbcp is provided. In TomEE, tomcat-jdbc is here by default too. If you want to use bonecp, you’ll need to add openejb-bonecp module and its dependencies (bonecp itself and guava).

Note about global VS specific configuration: global configuration will need to get the libraries in the container (lib/) where specific configuration supports through META-INF/resources.xml to provide additional libraries in the application.

Sample

Here is a little datasource configuration to give you a sample:

<!--?xml version="1.0" encoding="UTF-8"?-->
<resources>
  <Resource type="javax.sql.DataSource">
    # configure the pool
    DataSourceCreator = tomcat

    # it is a jta datasource
    JtaManaged = true

    # tomcat pool configuration
    driverClassName = com.mysql.Driver
    url = jdbc:mysql://127.0.0.1:3306/pooling
    username = tomee
    password = tomeepwd
    validationQuery = SELECT 1
    jmxEnabled = true # specific to tomcat pooling
  </Resource>
</resources>
Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s