下载AtomikosTransactionsEssentials-3.7.0M5-bin.zip
ojdbc14.jar,mysql-connector-java-5.0.8-bin.jar,spring.jar(2.0),log4j-1.2.14.jar,commons-logging.jar
工程类似,其实不是web工程
import.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" " http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="mysqlDS"
class="com.atomikos.jdbc.AtomikosDataSourceBean" init-method="init"
destroy-method="close">
<description>mysql xa datasource</description>
<property name="uniqueResourceName">
<value>mysql_ds</value>
</property>
<property name="xaDataSourceClassName">
<value>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</value>
</property>
<property name="xaProperties">
<props>
<prop key="user">root</prop>
<prop key="password">haoning</prop>
<prop key="URL">jdbc:mysql://localhost:3306/test</prop>
</props>
</property>
</bean>
<bean id="oracleDS" class="com.atomikos.jdbc.AtomikosDataSourceBean"
init-method="init" destroy-method="close">
<description>oracle xa datasource</description>
<property name="uniqueResourceName">
<value>oracle_ds</value>
</property>
<property name="xaDataSourceClassName">
<value>oracle.jdbc.xa.client.OracleXADataSource</value>
</property>
<property name="xaProperties">
<props>
<prop key="user">UNIFLOW</prop>
<prop key="password">UNIFLOW</prop>
<prop key="URL">jdbc:oracle:thin:@10.4.120.126:1521:E4CS</prop>
</props>
</property>
</bean>
<bean id="atomikosUserTransaction"
class="com.atomikos.icatch.jta.UserTransactionImp">
<description>UserTransactionImp</description>
<property name="transactionTimeout" value="300"/>
</bean>
<bean id="atomikosTransactionManager"
class="com.atomikos.icatch.jta.UserTransactionManager"
init-method="init" destroy-method="close">
<description>UserTransactionManager</description>
<property name="forceShutdown">
<value>true</value>
</property>
</bean>
<bean id="springTransactionManager"
class="org.springframework.transaction.jta.JtaTransactionManager">
<description>JtaTransactionManager</description>
<property name="transactionManager">
<ref bean="atomikosTransactionManager" />
</property>
<property name="userTransaction">
<ref bean="atomikosUserTransaction" />
</property>
</bean>
<bean id="transactionProxyFactoryBean"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
abstract="true">
<property name="transactionManager">
<ref bean="springTransactionManager" />
</property>
<property name="transactionAttributes">
<props>
<prop key="insert*">PROPAGATION_REQUIRED,-Exception</prop>
</props>
</property>
</bean>
<bean id="helloDao" class="com.jta.HelloDaoImpl">
<property name="dataSource" ref="mysqlDS" />
</bean>
<bean id="worldDao" class="com.jta.WorldDaoImpl">
<property name="dataSource" ref="oracleDS" />
</bean>
<bean id="helloBiz" class="com.jta.HelloBizImpl">
<property name="helloDao" ref="helloDao" />
<property name="worldDao" ref="worldDao" />
</bean>
<bean id="helloBizProxy" parent="transactionProxyFactoryBean">
<property name="target">
<ref bean="helloBiz" />
</property>
</bean>
</beans>
jta.properties
com.atomikos.icatch.service=com.atomikos.icatch.standalone.UserTransactionServiceFactory
com.atomikos.icatch.console_file_name = tm.out
com.atomikos.icatch.log_base_name = tmlog
com.atomikos.icatch.tm_unique_name = com.atomikos.spring.jdbc.tm
com.atomikos.icatch.console_log_level = INFO
package com.jta;
public interface HelloBiz {
public void insertTwoDatabase() throws Exception;
}
package com.jta;
public class HelloBizImpl implements HelloBiz {
private HelloDao helloDao;
private WorldDao worldDao;
public void insertTwoDatabase() throws Exception{
this.getHelloDao().addHello();//mysql
this.getWorldDao().addWorld();//oracle
}
public HelloDao getHelloDao() {
return helloDao;
}
public void setHelloDao(HelloDao helloDao) {
this.helloDao = helloDao;
}
public WorldDao getWorldDao() {
return worldDao;
}
public void setWorldDao(WorldDao worldDao) {
this.worldDao = worldDao;
}
}
package com.jta;
public interface HelloDao {
public void addHello() throws Exception;
}
package com.jta;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
public class HelloDaoImpl extends JdbcDaoSupport implements HelloDao{
public void addHello() throws Exception{//mysql
String sql = "insert into user(name,age) values(?,?)";
Object[] params = new Object[]{"test","12"};
this.getJdbcTemplate().update(sql, params);
}
}
package com.jta;
public interface WorldDao {
public void addWorld() throws Exception;
}
package com.jta;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
public class WorldDaoImpl extends JdbcDaoSupport implements WorldDao {
public void addWorld() throws Exception {// oracle
String sql = "insert into test1(id,myname) values(?,?)";
Object[] params = new Object[] { 3, "ningge" };
this.getJdbcTemplate().update(sql, params);
}
}
package com.jta;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args)throws Exception{
ApplicationContext context=new ClassPathXmlApplicationContext("import.xml");
HelloBiz helloBiz=(HelloBiz)context.getBean("helloBizProxy");
helloBiz.insertTwoDatabase();
}
}
posted on 2011-03-01 22:57
soken 阅读(1238)
评论(0) 编辑 收藏