打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Spring(AbstractRoutingDataSource)实现动态数据源切换
<?xml version="1.0" encoding="UTF-8"?>
<!-- Spring 数据库相关配置 放在这里 -->
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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.xsd
       http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
    <bean id = "dataSource1" class = "com.mysql.jdbc.jdbc2.optional.MysqlDataSource">   
        <property name="url" value="${db1.url}"/>
        <property name = "user" value = "${db1.user}"/>
        <property name = "password" value = "${db1.pwd}"/>
        <property name="autoReconnect" value="true"/>
        <property name="useUnicode"  value="true"/>
        <property name="characterEncoding" value="UTF-8"/>
    </bean>
    <bean id = "dataSource2" class = "com.mysql.jdbc.jdbc2.optional.MysqlDataSource">
        <property name="url" value="${db2.url}"/>
        <property name = "user" value = "${db2.user}"/>
        <property name = "password" value = "${db2.pwd}"/>
        <property name="autoReconnect" value="true"/>
        <property name="useUnicode"  value="true"/>
        <property name="characterEncoding" value="UTF-8"/>
    </bean>
    <bean id = "dataSource3" class = "com.mysql.jdbc.jdbc2.optional.MysqlDataSource">
        <property name="url" value="${db3.url}"/>
        <property name = "user" value = "${db3.user}"/>
        <property name = "password" value = "${db3.pwd}"/>
        <property name="autoReconnect" value="true"/>
        <property name="useUnicode"  value="true"/>
        <property name="characterEncoding" value="UTF-8"/>
    </bean>
    <!-- 配置多数据源映射关系 -->
    <bean id="dataSource" class="com.datasource.test.util.database.DynamicDataSource">
        <property name="targetDataSources">
            <map key-type="java.lang.String">
        <entry key="dataSource1" value-ref="dataSource1"></entry>
                <entry key="dataSource2" value-ref="dataSource2"></entry>
                <entry key="dataSource3" value-ref="dataSource3"></entry>
            </map>
        </property>
    <!-- 默认目标数据源为你主库数据源 -->
        <property name="defaultTargetDataSource" ref="dataSource1"/>
    </bean>
    <bean id="sessionFactoryHibernate" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">com.datasource.test.util.database.ExtendedMySQLDialect</prop>
                <prop key="hibernate.show_sql">${SHOWSQL}</prop>
                <prop key="hibernate.format_sql">${SHOWSQL}</prop>
                <prop key="query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</prop>
                <prop key="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</prop>
                <prop key="hibernate.c3p0.max_size">30</prop>
                <prop key="hibernate.c3p0.min_size">5</prop>
                <prop key="hibernate.c3p0.timeout">120</prop>
                <prop key="hibernate.c3p0.idle_test_period">120</prop>
                <prop key="hibernate.c3p0.acquire_increment">2</prop>
                <prop key="hibernate.c3p0.validate">true</prop>
                <prop key="hibernate.c3p0.max_statements">100</prop>
            </props>
        </property>
    </bean>
    <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
        <property name="sessionFactory" ref="sessionFactoryHibernate"/>
    </bean>
    <bean id="dataSourceExchange" class="com.datasource.test.util.database.DataSourceExchange"/>
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactoryHibernate"/>
    </bean>
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="insert*" propagation="NESTED" rollback-for="Exception"/>
            <tx:method name="add*" propagation="NESTED" rollback-for="Exception"/>
            <tx:method name="update*" propagation="NESTED" rollback-for="Exception"/>
            <tx:method name="modify*" propagation="NESTED" rollback-for="Exception"/>
            <tx:method name="edit*" propagation="NESTED" rollback-for="Exception"/>
            <tx:method name="del*" propagation="NESTED" rollback-for="Exception"/>
            <tx:method name="save*" propagation="NESTED" rollback-for="Exception"/>
            <tx:method name="send*" propagation="NESTED" rollback-for="Exception"/>
            <tx:method name="get*" read-only="true"/>
            <tx:method name="find*" read-only="true"/>
            <tx:method name="query*" read-only="true"/>
            <tx:method name="search*" read-only="true"/>
            <tx:method name="select*" read-only="true"/>
            <tx:method name="count*" read-only="true"/>
        </tx:attributes>
    </tx:advice>
    <aop:config>
        <aop:pointcut id="service" expression="execution(* com.datasource..*.service.*.*(..))"/>
        <!-- 关键配置,切换数据源一定要比持久层代码更先执行(事务也算持久层代码) -->
        <aop:advisor advice-ref="txAdvice" pointcut-ref="service" order="2"/>
        <aop:advisor advice-ref="dataSourceExchange" pointcut-ref="service" order="1"/>
    </aop:config>
</beans>
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Spring 多数据源事务配置问题
ssh 中使用c3p0 的连接池配置
Spring + Atomikos 分布式事务实现方式
三种流行的连接池配置
各种连接池的比较
Mule+Spring+jbpm
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服