JDBC pools configuration
BTM XA datasources can be created via some java code or via a BTM-specific tool called the Resource Loader. You are free to choose the method you prefer, there is absolutely no difference between them.
BTM comes bundled with a JDBC XA connection pool. It is very easy to configure it. You basically have to create an instance of
bitronix.tm.resource.jdbc.PoolingDataSource set some properties and you're done.
Here is an example of datasource creation that connects to an Oracle database:
1. The Bitronix
PoolingDataSource is a javabean that implements
2. You have to specify the driver's
XADataSource implementation here. If your database vendor does not provide a
XADataSource implementation, you should have a look at the Last Resource Commit optimization.
3. Each datasource must be assigned a unique name. This is required for distributed crash recovery.
4. This datasource will pool 5 connections.
5. You have to set
allowLocalTransactions to true if you want to be able to run SQL statements outside of XA transactions scope.
6,7,8. The driverProperties is a
java.util.Properties object. You have to add into it a set of property name / property value of the
OracleXADataSource class. You have to refer to the driver's documentation to know what can / has to be set. The OracleXADataSource javadoc contains this list for the Oracle case. BTM will perform conversion from
boolean or to
int when necessary.
9,10. You can now use the
PoolingDataSource like any other
11. Remember to close the
PoolingDataSource after you're done with it to release the connections.
The connection pool will be initialized during the first call to
getConnection(). It might be desirable to initialize the pool eagerly, like during application startup rather than having to wait for the first requests. This can be done by calling
Now line 9 will initialize the pool and create the 5 connections to the database instead of line 10.
Using the Resource Loader
A datasource configuration utility is also bundled with BTM. It is convenient to use it rather than create your datasources in code. Refer to the Resource Loader page for more details.
Here is the equivalent Resource Loader configuration of the previous code example:
You just have to write those properties in a simple text file and tell BTM where to load it by setting the
resourceConfigurationFilename property of the Configuration object.
Now you also have to know how to get the datasource created by the Resource Loader. There are multiple ways:
bitronix.tm.resource.bind=trueto your resource loader properties file. The datasources will then be bound to the default JNDI server using their
uniqueNameas their JNDI name.
- Another way (in case the JNDI context is read only, like in Tomcat) is to bind a
bitronix.tm.resource.ResourceFactoryobject, passing it a
javax.naming.StringRefAddrcontaining the datasource's
addrTypesomewhere in your JNDI tree. The
bitronix.tm.resource.ResourceFactoryclass will just return the datasource with the specified
uniqueName. This is explained more in-depth in the Jetty and Tomcat documentation pages.
- The last way is to call
bitronix.tm.resource.ResourceRegistrar.get(String uniqueName). This is the least preferred method as this ties your code to BTM which you probably want to avoid.