Warning: A non-numeric value encountered in /home/ricston2/public_html/blogarchive/wp-content/themes/Divi/functions.php on line 5766

When dealing with databases from a Java application, one of the issues that many people are faced with, is performance. When many connections are required, connection pooling is almost mandatory. There are quite a few options out there, just to mention a few:

One thing we learned the hard way is that pooling of XA connections is not that easy. Most connection pools do not support XA connections. Some implementations in the documentation say they provide support, but either do not work, or there is some hidden fancy stuff which we never managed to get our head around. In some cases, you can do without pooling, but what if its really necessary? XA transactions are already heavy. Opening and closing connections each time makes them even worse.

So we need a connection pool that supports XA connections. Luckily there are a few, but I think the most interesting of all is XAPool: http://xapool.ow2.org. Don’t get too excited however, even though it sounds easy to get it to work, it isn’t.

All the tests mentioned in this blog were done using Mule 3.3.2 using JBoss TS as the transaction manager for XA. As a database we used H2 and Postgresql. For the purpose of keeping this blog simple, we only show configurations for the H2 database. We created a very simple Mule flow which starts a transaction on a Mule’s VM queue and continues over JDBC.

First let’s start by having a look at Tomcat JDBC Connection Pool. In the documentation, we can find that this library does provide support for XA transactions. However, we never managed to get it to work. We tried a lot of different configurations, but the end result was always the same, it did not really work. If anyone can suggest a solution, you’re very welcome.

Try number one, set the internal datasource using H2’s jdbcx datasource and configure Tomcat Pool XADataSource. Note that in the pool we are referring to the datasource, this is required to be able to create XA Connections:

The problem with this is that every time we tried to get an XA transaction going from Mule, we were faced with an “object already closed” exception:

To try to fix this, we tried to wrap the H2 datasource in an XAPool StandardXADatasource in case the connection was not being created properly for XA. This is the configuration:

This did not help in any way, same issue encountered, just slightly different exception:

Its important to note that both configurations work if we subtract the Tomcat JDBC Pool. So clearly, this pool does not really work for XA transactions, at least with Mule.

The final option is to try with XAPool directly. XAPool provides a class called StandardXAPoolDataSource, so this looked quite promising. On their website they even have an example: here. Wait sorry. Even though the example is called StandardXAPoolDataSource, it actually uses StandardXADataSource with no pooling! Nice!! (I’m being utterly sarcastic just in case you have not noticed).

Ok so let’s dive in and try to configure StandardXAPoolDataSource:

This looks promising. However there is a huge problem here. StandardXAPoolDataSource does not implement javax.sql.XADataSource. It implements only javax.sql.DataSource. This means when Mule tries to get an XA transaction, it will not manage, and throw the following exception:

When taking a close look at the source, it looks like everything is in place. Only missing the implementation of javax.sql.XADataSource, and some of its methods. But these appear nowhere in the XAPool library.

Having established this, we implemented a class that extends StandardXAPoolDataSource and implements javax.sql.XADataSource. The implementation of the getXaConnection() method is just calling the super class getConnection() method which does all the magic pooling stuff.

Now we only need to configure it in Spring and wire it up with our Mule’s JDBC connector, and we’re done.

In a future post we intend to take a closer look at how to configure the pool size and other interesting attributes.