Skip to end of metadata
Go to start of metadata

Integrating BTM with iBatis

iBatis can be integrated straight with any JTA transaction manager. These instructions have been verified against BTM 1.2.


JTA datasources

iBatis cannot directly create a BTM PoolingDataSource. You will have to create them yourself (either via the API or the Resource Loader) and make sure they are bound to a JNDI server.

Setting up a JNDI server

You have to bind the datasources to some JNDI server. You can use any one you wish, including the one embedded in your application server.

In the case you want to run iBatis in a standalone application, you can use Tomcat's JNDI server as it is very easy to use it in a standalone J2SE application. Just create a file at the root of your classpath. It should only contain this line:

Then add these two jar files to your classpath:

They are available in any Tomcat 5.5 distribution, just look into the TOMCAT_HOME/common/lib folder.

This will configure Tomcat's JNDI server as the default implementation. It is a simple intra-VM server that implements the most important JNDI features. You can now just create a InitialContext with the no-args constructor to have access to it.

API way: Creating the datasources

As you can expect, you will need to create one PoolingDataSource per database. Say that you want to use two Embedded Derby databases, and configure them via the BTM API. Here is what your code would look like:

Datasource's unique name and JNDI location correspondence


It is usually a good idea to set a datasource's unique name to the same value as its JNDI location although this is not mandatory.

Finally, here is the code to bind the datasources:

Resource Loader way: Creating the datasources

You can use BTM's Resource Loader instead of the BTM API. It is usually a good idea when you want to create a fully standalone application as you can get rid of the datasources creation, JNDI binding and shutdown code.

Create a file in the current directory containing these properties:

Resource Loader JNDI binding


When you configure the Resource Loader to bind datasources to JNDI (by setting the property to true) it will bind them using the unique name as the JNDI location.

In your application code, you will have to configure BTM to use the resource loader:

This has the exact same behavior as creating the PoolingDataSource objects and binding them to JNDI yourself. It is just more convenient.

iBatis SqlMapClients

You need to configure exactly one SqlMapClient per datasource.

Datasource JNDI location

You have to tell iBatis where to get the BTM datasource via JNDI. Add a DataSource property to the dataSource tag and set its value to the JNDI location of your datasource:

Transaction Manager JNDI location

You have to set transactionManager tag's type attribute to JTA and wrap the dataSource tag in it.

SqlMapConfig XML configuration files

Here is what the SqlMapConfig1.xml file will look like for the first datasource.

And here is the SqlMapConfig2.xml for the second datasource:

End result

Now that iBatis and BTM are properly configured, you can simply use the JTA and iBatis APIs in your application.

Application code

Here is what your code will look like when you want to update the content of both databases atomically:

Say that persistUser() creates a new user, in no way will a user be created in one database and not in the other.


You can download a sample runnable application putting these explanations in practice. It contains all the code that has been skipped for clarity in this page. Both the API and Resource Loader ways are implemented so you can try both and see which one you prefer.

You can download this demo here:

There is an ant build.xml file included as well as a the necessary batch and shell scripts required to run the application from Windows or Unix.

Before you run the application, you have to create the Derby database. Just run the included or derby-create.bat script to do so, it will create two directories called users1 and users2. Then you can start the demo by either running or run_api.bat for the API version, or run_rl.bat for the Resource Loader version.

Here is the list of JAR files with version required to run this demo. They're all included in the downloadable ZIP file.

JAR name



BTM 1.2


BTM 1.2


SLF4J 1.4.3


SLF4J 1.4.3






Apache Commons Logging 1.0.4


iBatis 2.3.3


Tomcat 5.5.23


Tomcat 5.5.23

  • No labels