多数据源问题--Spring+Ibatis 访问多个数据源(非分布式事务)

有的时候,我在一个工程中需要访问两个以上的数据源,尤其是在系统集成的时候,以下是我在系统集成的时候遇到的情况,我的工程的架构是:spring2.0+ibatis2.0+struts1.2. 数据库是oracle数据库,
而需要集成的系统是sql server数据库,对方没有提供接口,要求直接操作sql server库。以下是spring配置文件中配置了两个数据源,包括service事务。实际上要把applicationContext.xml中的dao部分
独立到daoContext.xml文件中,为了节省篇幅,就放到了一起。

jdbc.properties文件内容如下:

jdbc2.driverClassName=net.sourceforge.jtds.jdbc.Driver
jdbc2.url=jdbc:jtds:sqlserver://10.101.122.9:1433/testdb;charset=gb2312
jdbc2.username=user1
jdbc2.password=password

jdbc.driverClassName=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@10.101.126.29:1521:DBSERVER
jdbc.username=user1
jdbc.password=password

applicationContext.xml配置文件内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
    <list>
      <value>jdbc.properties</value>
    </list>
</property>
</bean>
<!-- =========================transactionManager========================= -->
<bean id="autoProxyCreator" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames">
    <value>*Service</value>
</property>
<property name="interceptorNames">
    <list>
      <value>transactionInterceptor</value>
       <value>transactionInterceptor2</value>
    </list>
</property>
</bean>

<!-- =========================oracle dataSource========================= -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
   <property name="driverClassName" value="${jdbc.driverClassName}"/>
   <property name="url" value="${jdbc.url}"/>
   <property name="username" value="${jdbc.username}"/>
   <property name="password" value="${jdbc.password}"/>

</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
   <property name="dataSource" ref="dataSource"/>
</bean>
<bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
   <property name="transactionManager" ref="transactionManager"/>
                <property name="transactionAttributes">
                <props>
                <prop key="insert*">PROPAGATION_REQUIRED</prop>
                <prop key="delete*">PROPAGATION_REQUIRED</prop>
                <prop key="update*">PROPAGATION_REQUIRED</prop>
                <prop key="do*">PROPAGATION_REQUIRED</prop>
                 </props>
                </property>
</bean>
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
   <property name="configLocation" value="sql-map-config.xml"/>
   <property name="dataSource" ref="dataSource"/>
</bean>
<!-- =========================sqlserver dataSource========================= -->
   <bean id="dataSource2" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
   <property name="driverClassName" value="${jdbc2.driverClassName}"/>
   <property name="url" value="${jdbc2.url}"/>
   <property name="username" value="${jdbc2.username}"/>
   <property name="password" value="${jdbc2.password}"/>
</bean>

<bean id="transactionManager2" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
   <property name="dataSource" ref="dataSource2"/>
</bean>

<bean id="transactionInterceptor2" class="org.springframework.transaction.interceptor.TransactionInterceptor">
   <property name="transactionManager" ref="transactionManager2"/>
                <property name="transactionAttributes">
                <props>
                <prop key="insert*">PROPAGATION_REQUIRED</prop>
                <prop key="delete*">PROPAGATION_REQUIRED</prop>
                <prop key="update*">PROPAGATION_REQUIRED</prop>
                <prop key="do*">PROPAGATION_REQUIRED</prop>
                 </props>
                </property>
</bean>
   <bean id="sqlMapClient2" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
   <property name="configLocation" value="sql-map-config.xml"/>
   <property name="dataSource" ref="dataSource2"/>
</bean>


   <!-- =========================for example ========================= -->
<bean id="FirstDAO" class="com.demo.impl.FirstDAOImpl">
   <property name="sqlMapClient" ref="sqlMapClient"/>
</bean>
<bean id="SecondDAO" class="com.demo.impl.SecondDAOImpl">
   <property name="sqlMapClient" ref="sqlMapClient2"/>
</bean>

<bean id="FirstService" class="com.demo.impl.FirstServiceImpl">
        <property name="firstDAO" ref="FirstDAO"/>
</bean>
<bean id="SecondService" class="com.demo.impl.SecondServiceImpl">
      <property name="secondDAO" ref="SecondDAO"/>
</bean>
</beans>
原文地址:https://www.cnblogs.com/duanxz/p/3746992.html