Yes I do have experience with this. I've implemented iBatis allowing Spring to be utilized as the framework and transaction management system utilizing an external Transaction Manager. It took a while for me to find all of the pieces so I hope this will help you.
I'm using Tomcat 5.5 as my container so you will need to create a context.xml file with the similar entries and place it in your META-INF directory of your application. I'm also using JOTM as my transaction manager. You can download it freely at www.objectweb.com.
Tomcat context.xml configuration file:
<Context >
<!--
Your Datasources that are to be used.
Replace the driver class, URL, user name and password with your specific entries
-->
<Resource name="jdbc/datasource1" auth="Container" type="javax.sql.XADataSource"
="org.objectweb.jndi.DataSourceFactory"
="com.sybase.jdbc3.jdbc.SybXADataSource"
="jdbc:sybase:Tds:SERVER1:dbPort/dbName"
="dbUser" password="dbPassword" maxActive="30"
="4" timeout="360" defaultAutoCommit="false"
="true"/>
<Resource name="jdbc/datasource2" auth="Container" type="javax.sql.XADataSource"
="org.objectweb.jndi.DataSourceFactory"
="com.sybase.jdbc3.jdbc.SybXADataSource"
="jdbc:sybase:Tds:SERVER2:dbPort/dbName"
="dbUser" password="dbPassword" maxActive="30"
="4" timeout="360" defaultAutoCommit="false"
="true"/>
<!--
JOTM as your Transaction manager
-->
<Transaction factory="org.objectweb.jotm.UserTransactionFactory"
jotm.timeout="360"/>
</Context>
Spring configuration follows and should be defined within the application.xml file:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jndi="http://www.springframework.org/schema/jndi" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/jndi http://www.springframework.org/schema/jndi/spring-jndi.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- Main JNDI DataSource for J2EE environments -->
<bean id="dataSource1" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/datasource1"/>
</bean>
<bean id="dataSource2" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/datasource2"/>
</bean>
<!-- The reference to the Transaction Manager-->
<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
<property name="userTransactionName" value="java:comp/UserTransaction"/>
</bean>
<!--TRANSACTION INTERCEPTOR-->
<bean id="matchAllTxInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager">
<ref bean="transactionManager"/>
</property>
<property name="transactionAttributes">
<props>
<prop key="insert*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="delete*">PROPAGATION_REQUIRED</prop>
<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
<bean id="autoProxyCreator" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="interceptorNames">
<list>
<idref local="matchAllTxInterceptor"/>
</list>
</property>
<property name="beanNames">
<list>
<value>*Dao</value>
</list>
</property>
</bean>
<!-- SqlMap setup for iBATIS Database Layer -->
<bean id="sqlMapClient1" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation" value="WEB-INF/conf/sql-map-config1.xml"/>
<property name="dataSource" ref="dataSource1"/>
</bean>
<bean id="sqlMapClient2" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation" value="WEB-INF/conf/sql-map-config2.xml"/>
<property name="dataSource" ref="dataSource2"/>
</bean>
<!-- ========================= DAO DEFINITIONS: IBATIS IMPLEMENTATIONS ========================= -->
<bean id="currencyDao" class="com.yourCompany.dao.impl.CurrencyDaoImpl">
<property name="sqlMapClient" ref="sqlMapClient1"/>
</bean>
<bean id="countryDao" class="com.yourCompany.dao.impl.CountryDaoImpl">
<property name="sqlMapClient" ref="sqlMapClient2"/>
</bean></beans>
Now all that you need to do is implement your Sql Maps and your Doa's, making certain that the entries in your configuration files point to the correct classes. All the real work is handled by Spring including the transaction demarcation. When you start working with multiple data sources they must participate in a global transaciton. The above configuration demostrates this.
I hope this helps....
Chris Mathrusse
[EMAIL PROTECTED]
Sybase, Inc
One Sybase Drive
Dublin, CA 94568
(925) 236-5553
| Reuben Firmin <[EMAIL PROTECTED]>
03/09/2006 11:31 AM
|
|
Hello,
Has anybody applied transaction rules across multiple datasources using
Resin/Spring/Ibatis? I'm looking at the JtaTransactionManager that
spring provides, which seems like it'll work, and will be experimenting
with that today. If you have practical examples of setting this up,
though, I'd appreciate it.
Thanks
Reuben
