Atomikos spring例子

下载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工程
[img]http://dl.iteye.com/upload/attachment/423535/16f92891-6935-35b0-ab95-69507322e1e5.jpg[/img]
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();
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值