OpenEJB and TomEE provide default factories for datasources allowing to define easily by configuration JTA or not datasource pooled by a configurable pool implementation (dbcp, tomcat-jdbc, bonecp…).
But sometimes it is not enough. You want to take the control of the datasource (if you want to use native driver pooling, read the config from a custom source etc…).
Defining a DataSource in TomEE is quite easy, the most common way to do so is to define a Resource block in tomee.xml:
<Resource id="jdbc/ds" type="DataSource">
JdbcUrl = jdbc:mysql://datasourcehost:3306/tomee
When you have multiple hosts you often use datasourcehost (in the previous sample) as a service host (or IP) to delegate then to real hosts. But sometimes it is not possible or too complicated. For such needs TomEE now provides a basic failover feature.
The idea is to reuse dynamic datasource feature of tomee to handle it.
In JavaEE a datasource generally means a pool. So if you have two persistence units using the same physical datasource it can be very interesting to be able to use the same pool (easier to configure the pool size for instance).
TomEE now supports to switch the connection pooling implementation (commons-dbcp, tomcat-jdbc, bonecp…). However it tries to convert dbcp properties to the other implementation if these properties are found (mainly for compatibility reasons).
In some cases it can be hard.
Here some small tips to make it easier :).
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 :).