81,091
社区成员
发帖
与我相关
我的任务
分享
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
default-autowire="byName" default-lazy-init="false">
<context:component-scan base-package="com.ssi.*" />
<!-- 属性文件读入 -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath*:jdbc.properties</value>
</list>
</property>
</bean>
<!-- JTA 数据源配置 -->
<bean id="center" class="com.atomikos.jdbc.AtomikosDataSourceBean" init-method="init" destroy-method="close">
<property name="uniqueResourceName">
<value>mysql/center</value>
</property>
<property name="xaDataSourceClassName">
<value>${jta.driver.className}</value>
</property>
<property name="xaProperties">
<props>
<prop key="url">${center.jdbc.driver.url}</prop>
<prop key="user">${center.sql.user.name}</prop>
<prop key="password">${center.sql.user.password}</prop>
</props>
</property>
<property name="testQuery" value="select 1" />
<property name="poolSize">
<value>${poolsize}</value>
</property>
<property name="maxPoolSize">
<value>${maxPoolSize}</value>
</property>
<property name="borrowConnectionTimeout"><value>${borrowConnectionTimeout}</value></property>
</bean>
<bean id="db1" class="com.atomikos.jdbc.AtomikosDataSourceBean" init-method="init" destroy-method="close">
<property name="uniqueResourceName">
<value>mysql/db1</value>
</property>
<property name="xaDataSourceClassName">
<value>${jta.driver.className}</value>
</property>
<property name="xaProperties">
<props>
<prop key="url">${db1.jdbc.driver.url}</prop>
<prop key="user">${company.sql.user.name}</prop>
<prop key="password">${company.sql.user.password}</prop>
</props>
</property>
<property name="testQuery" value="select 1" />
<property name="poolSize">
<value>${poolsize}</value>
</property>
<property name="maxPoolSize">
<value>${maxPoolSize}</value>
</property>
<property name="borrowConnectionTimeout"><value>${borrowConnectionTimeout}</value></property>
</bean>
<bean id="db2" class="com.atomikos.jdbc.AtomikosDataSourceBean" init-method="init" destroy-method="close">
<property name="uniqueResourceName">
<value>mysql/db2</value>
</property>
<property name="xaDataSourceClassName">
<value>${jta.driver.className}</value>
</property>
<property name="xaProperties">
<props>
<prop key="url">${db2.jdbc.driver.url}</prop>
<prop key="user">${company.sql.user.name}</prop>
<prop key="password">${company.sql.user.password}</prop>
</props>
</property>
<property name="testQuery" value="select 1" />
<property name="poolSize">
<value>${poolsize}</value>
</property>
<property name="maxPoolSize">
<value>${maxPoolSize}</value>
</property>
<property name="borrowConnectionTimeout"><value>${borrowConnectionTimeout}</value></property>
</bean>
<bean id="dataSource" class="com.ssi.datasource.DynamicDataSource">
<property name="targetDataSources">
<map key-type="java.lang.String">
<entry key="db1" value-ref="db1" />
<entry key="db2" value-ref="db2" />
<entry key="center" value-ref="center" />
</map>
</property>
<property name="defaultTargetDataSource" ref="center" />
</bean>
<!-- 配置sqlMapclient -->
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation" value="classpath:ibatis-sqlmap-config.xml" />
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 支持 @AspectJ 标记-->
<aop:aspectj-autoproxy proxy-target-class="true"/>
<!-- 配置JTA的事务管理器 -->
<bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager" init-method="init" destroy-method="close">
<property name="forceShutdown" value="true" />
</bean>
<bean id="atomikosUserTransaction" class="com.atomikos.icatch.jta.UserTransactionImp">
<property name="transactionTimeout" value="300" />
</bean>
<bean id="springTransactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
<property name="transactionManager" ref="atomikosTransactionManager" />
<property name="userTransaction" ref="atomikosUserTransaction" />
</bean>
<!-- 配置通知 -->
<tx:advice id="txAdvice" transaction-manager="springTransactionManager">
<tx:attributes>
<tx:method name="*" rollback-for="Exception,RuntimeException,com.ssi.exception.SystemException" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<!-- 以AspectJ方式 定义 AOP -->
<aop:config>
<aop:advisor pointcut="execution(* com.ssi.service..*Service*.*(..))" advice-ref="txAdvice" />
</aop:config>
</beans>
public class UserServiceImpl implements IUserService {
@Resource
private IUserDao userDao;
public void addUser(User user) throws Exception{
DbContextHolder.setDbType("db1");
userDao.addUser(user);
DbContextHolder.setDbType("db2");
userDao.addUser(user);
DbContextHolder.setDbType("center");
userDao.addUser(user);
}
public void addUser2(User user,int status) throws Exception{
userDao.addUser(user);
if(status==1){
System.out.println(1/0);
}
}
}
@Repository("userDao")
public class UserDaoImpl extends SqlMapClientDaoSupport implements IUserDao {
@Override
public Integer addUser(User user) throws Exception{
return (Integer) this.getSqlMapClient().insert("User.insert", user);
}
}
public class DynamicDataSource extends AbstractRoutingDataSource {
static Logger log = Logger.getLogger(DynamicDataSource.class);
protected Object determineCurrentLookupKey() {
return DbContextHolder.getDbType();
}
}
public class DbContextHolder {
private static final ThreadLocal contextHolder = new ThreadLocal();
public static void setDbType(String dbType) {
contextHolder.set(dbType);
}
public static String getDbType() {
return (String) contextHolder.get();
}
public static void clearDbType() {
contextHolder.remove();
}
}
DROP TABLE IF EXISTS `tb_user`;
CREATE TABLE `tb_user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`userName` varchar(20) DEFAULT NULL,
`password` varchar(60) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
public class JunitTest{
public ApplicationContext cxt;
@Test
public void init() throws Exception{
cxt = new ClassPathXmlApplicationContext(new String[] {"applicationContext.xml"});
testInsertUser1();
testInsertUser2();
}
private void testInsertUser1() throws Exception{
IUserService userService = (IUserService)cxt.getBean("userService");
User user = new User();
user.setUserName("user1");
user.setPassword("0");
userService.addUser(user);
}
private void testInsertUser2() throws Exception{
IUserService userService = (IUserService)cxt.getBean("userService");
User user = new User();
user.setUserName("user2");
user.setPassword("0");
DbContextHolder.setDbType("db1");
userService.addUser2(user,0);
DbContextHolder.setDbType("db2");
userService.addUser2(user,0);
DbContextHolder.setDbType("center");
userService.addUser2(user,1);
}
}