jump to navigation

Spring Transactions across multiple datasources September 16, 2008

Posted by Phill in Other Stuff, Spring.
Tags: , , , ,
trackback

Update: Since writing this post I have discovered problems with the Enhydra connection pool, and as such I do not recommend using it. I recommend finding another way to accomplish what you want to do.

We have an application here which connects to two different data sources. I was having problems getting the application to be transactional using Springs @Transactional annotation. The problem is, the Transactional annotation does not let you specify a transaction manager – and one transaction manager can only manage one DataSource. Or so I thought.

It turns out that it is possible, using JTA – and JOTM.

The configuration you need is pretty minimal. In your application context XML file, you need to put the following:

<bean id="jotm"
class="org.springframework.transaction.jta.JotmFactoryBean" />

<bean id="innerDataSource"
class="org.enhydra.jdbc.standard.StandardXADataSource"
destroy-method="shutdown">
<property name="transactionManager" ref="jotm" />
<property name="driverName" value="${dataSource.driver}" />
<property name="url" value="${dataSource.url}" />
<property name="user" value="${dataSource.username}" />
<property name="password" value="${dataSource.password}" />
</bean>

<bean id="dataSource"
class="org.enhydra.jdbc.pool.StandardXAPoolDataSource"
destroy-method="shutdown">
<property name="dataSource" ref="innerDataSource" />
<property name="user" value="${dataSource.username}" />
<property name="password" value="${dataSource.password}" />
</bean>

<bean id="transactionManager"
class="org.springframework.transaction.jta.JtaTransactionManager">
<property name="userTransaction" ref="jotm" />
</bean>

(You also need to set up your libraries to inculde JOTM and the XA Data Sources – which can be got here)

This is for the first DataSource. Your second DataSource should be set up as per the above, except that you don’t need to repeat the jotm or transactionManager beans.

Provided that you then set up your transactions as annotation-driven – and actually use a few annotations – you should find your methods are transactional across multiple dataSources!

You may also find it helps to read a couple of articles on the subject, two which I found helpful are here and here.

Advertisements

Comments

1. Mike S. - December 15, 2009

Hi,

What kind of problems arise when using Enhydra?

Thanks!

2. Phill - December 15, 2009

Mike, I think the problems were mainly issues with retrieving a connection object – sometimes it would randomly fail without a good reason.

It was over a year ago so I can’t remember exactly. But I’m guessing the issue still remains as the project hasn’t had a new release in three years.

3. justdoit - March 20, 2010

Hi Phill,

Do you have any sample code for this on how to implement .
I don’t understand why the innerDatasource is set as a property into dataSource bean.

What is the alternative to enhydra??.

I need to implement spring with multiple databases update in Tomcat with MYsql as db.

Please guide.


Sorry comments are closed for this entry

%d bloggers like this: