Spring Transactions across multiple datasources September 16, 2008
Posted by Phill in Other Stuff, Spring.Tags: jotm, jta, multiple datasources, Spring, transactions
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.
hi, thanks for your good post.
but i inconvenience of using jtom..
then did you know and using other solution.
thanks.
chanwook.