javaEE项目建立多个数据源并配置事务


现在有以下3个数据源:

jdbc1.driver=oracle.jdbc.driver.OracleDriver
jdbc1.url=jdbc:oracle:thin:@127.0.0.1:1521:jdbc1
jdbc1.user=jdbc1
jdbc1.pwd=jdbc1
jdbc1.validationQuery=select 1 from dual
jdbc1.pagesize=12

jdbc2.jdbc.driver = oracle.jdbc.driver.OracleDriver
jdbc2.jdbc.url = jdbc:oracle:thin:@127.0.0.1:152:jdbc2
jdbc2.jdbc.user=jdbc2
jdbc2.jdbc.pwd=jdbc2
jdbc2.jdbc.validationQuery=select 1 from dual
jdbc2.jdbc.pagesize=12

jdbc3.driver=oracle.jdbc.driver.OracleDriver
jdbc3.url=jdbc:oracle:thin:@127.0.0.1:1521:jdbc3
jdbc3.user=jdbc3
jdbc3.pwd=jdbc3
jdbc3.validationQuery=select 1 from dual
jdbc3.pagesize=12


spring 配置文件配置数据源:

<?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:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="
 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.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-2.0.xsd">
    <!-- dataSource1-->
    <bean id="dataSource1" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="${jdbc1.driver}" />
        <property name="url" value="${jdbc1.url}" />
        <property name="username" value="${jdbc1.user}" />
        <property name="password" value="${jdbc1.pwd}" />
        <property name="maxActive" value="30" />
        <property name="maxIdle" value="30" />
        <property name="maxWait" value="10000" />
        <property name="logAbandoned" value="true" />
        <property name="removeAbandoned" value="true" />
        <property name="removeAbandonedTimeout" value="150" />
        <property name="testWhileIdle" value="true" />
        <property name="timeBetweenEvictionRunsMillis" value="300000" />
        <property name="minEvictableIdleTimeMillis" value="320000" />
    </bean>
    <!--dataSource2 -->
    <bean id="dataSource2" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="${jdbc2.jdbc.driver}" />
        <property name="url" value="${jdbc2.jdbc.url}" />
        <property name="username" value="${jdbc2.jdbc.user}" />
        <property name="password" value="${jdbc2.jdbc.pwd}" />
        <property name="maxActive" value="30" />
        <property name="maxIdle" value="30" />
        <property name="maxWait" value="10000" />
        <property name="logAbandoned" value="true" />
        <property name="removeAbandoned" value="true" />
        <property name="removeAbandonedTimeout" value="150" />
        <property name="testWhileIdle" value="true" />
        <property name="timeBetweenEvictionRunsMillis" value="300000" />
        <property name="minEvictableIdleTimeMillis" value="320000" />
    </bean>
    
    <bean id="dataSource3" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="${jdbc3.driver}" />
        <property name="url" value="${jdbc3.url}" />
        <property name="username" value="${jdbc3.user}" />
        <property name="password" value="${jdbc3.pwd}" />
        <property name="maxActive" value="30" />
        <property name="maxIdle" value="30" />
        <property name="maxWait" value="10000" />
        <property name="logAbandoned" value="true" />
        <property name="removeAbandoned" value="true" />
        <property name="removeAbandonedTimeout" value="150" />
        <property name="testWhileIdle" value="true" />
        <property name="timeBetweenEvictionRunsMillis" value="300000" />
        <property name="minEvictableIdleTimeMillis" value="320000" />
    </bean>
    
    <bean id="nativeJdbcExtractor"     class="org.springframework.jdbc.support.nativejdbc.SimpleNativeJdbcExtractor" />

    <bean id="oracleLobHandle" class="org.springframework.jdbc.support.lob.OracleLobHandler" lazy-init="true">
        <property name="nativeJdbcExtractor" ref="nativeJdbcExtractor" />
    </bean>
    
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="lobHandler" ref="oracleLobHandle" />
        <property name="mappingDirectoryLocations">
            <list>
                <value>classpath*:resource/hibernate</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
                <prop key="hibernate.cache.use_query_cache">${hibernate.cache.use_query_cache}
                </prop>
                <prop key="hibernate.cache.provider_class">${hibernate.cache.provider_class}
                </prop>
                <prop key="hibernate.query.factory_class">${hibernate.query.factory_class}</prop>
            </props>
        </property>
    </bean>

    <!--Hibernate TransactionManager-->
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource">
            <ref bean="dataSource" />       <!--dataSource bean 的id-->
        </property>
        <property name="globalRollbackOnParticipationFailure" value="false" />
    </bean>
    
    <aop:config proxy-target-class="true">  <!--事务控制在service层和dao层-->
        <aop:pointcut id="serviceMethods"
            expression="execution(* *..*ServiceImpl.*(..))" />
            <aop:pointcut id="managerMethods"
            expression="execution(* *..*ManagerImpl.*(..))" />
        <aop:pointcut id="serviceMethods2"
            expression="execution(* *..*Dao.*(..))" />
        <aop:advisor pointcut-ref="serviceMethods" advice-ref="txAdvice" />
        <aop:aspect ref="logMonitor">   <!--自定义LogProfiler打印数据库日志-->
            <aop:around pointcut-ref="serviceMethods" method="profileMethod" />
        </aop:aspect>
    </aop:config>
    
    <bean id="logMonitor" class="LogProfiler" />
    
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="save*"/>
            <tx:method name="insert*" />
            <tx:method name="remove*" />
            <tx:method name="delete*" />
            <tx:method name="update*" />
            <tx:method name="add*" />
            <tx:method name="*" read-only="true" />
        </tx:attributes>
    </tx:advice>
</beans>


  1. 写好一个类继承JdbcGenericDao继承JdbcTemplate,JdbcTemplate需要传人dataSource;

    public class JdbcGenericDao extends JdbcTemplate {
        protected Log log = LogFactory.getLog(getClass());
        protected LobHandler lobHandler;
        
        public void setLobHandler(LobHandler lobHandler) {
            this.lobHandler = lobHandler;
        }
    }


2.给项目不同模块dao类(如:FundInfoDao,CommonLog,FundDirectDao)继承JdbcGenericDao,并传人指定的dataSource;


    <bean id="fundInfoDao" class="FundInfoDao">
        <property name="dataSource" ref="dataSource1"/>
    </bean>
 
    <bean id="commonLogDao" class="CommonLog">
        <property name="dataSource" ref="dataSource2"/>
    </bean>
 
    <bean id="fundDirectDao" class="FundDirectDao">
        <property name="dataSource" ref="dataSource3"/>
    </bean>


3.总结

    接口的多态性,spring整合jdbc的JdbcTemplate,需要dataSource 的bean,这个bean具体怎么实现不管,只要符合规定就可以;


你可能感兴趣的:(javaEE项目建立多个数据源并配置事务)