Monday, January 11, 2010

Share a datasource among several applications in Tomcat using Atomikos

In a previous post I discussed about how to use Atomikos JTA implementation in Tomcat. We had a requirement of sharing a globally defined datasource among several applications deployed in Tomcat container. The datasource lookup from the one application (whichever does the lookup first) succeeds but it always fails from the second application with an error message like-

Another resource already exists with name DATA_SOURCE_NAME - pick a different name

Why? We were using the sample bean factory available at Atomikos' site, which tries to recreate the datasource on each lookup. Within an application one can cache the datasource after initial JNDI lookup call. But what about doing a lookup of same resource from multiple applications?

After poking around for a while in Atomikos source code I found that after creation, datasource instance is cached by the Atomikos. With a minor modification in bean factory as shown below, the cached instance can be retrieved instead of recreating it.
// see if this DataSource is already initialized then return cached object
// if available, else Atomikos will throw an exception
try {
    bean = IntraVmObjectRegistry.getResource(((AbstractDataSourceBean) bean).getUniqueResourceName());
    if (log.isInfoEnabled())
        log.info("Returning cached value of AbstractDataSourceBean (Atomikos): " + bean);

    return bean;
} catch (NameNotFoundException nfe) {
    // OK, it is not available go ahead and create one
}
With this change a globally defined datasource can be used by multiple applications inside a Tomcat container to use Atomikos JTA implementation.

6 Comments: