SSH框架系列:Spring配置多个数据源

本文介绍如何在Spring中配置两个不同的数据源(MySQL和Oracle),并分别为其设置SessionFactory及事务管理器。通过这种方式,可以实现应用程序对不同数据库的支持。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

配置2个数据源,2个对应的SessionFactory,2个事务等。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:context="http://www.springframework.org/schema/context"  
    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-3.0.xsd  
    http://www.springframework.org/schema/tx   
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd  
    http://www.springframework.org/schema/aop  
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd  
    http://www.springframework.org/schema/context  
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">  
    <!-- 引入datasource配置文件 -->  
    <context:property-placeholder location="classpath:datasource.properties" />  
    <!--创建mysql jdbc数据源 -->  
    <bean id="mysqlDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"  
        destroy-method="close">  
        <property name="driverClass" value="${jdbc.mysql.driver}" />  
        <property name="jdbcUrl" value="${jdbc.mysql.url}" />  
        <property name="user" value="${jdbc.mysql.username}" />  
        <property name="password" value="${jdbc.mysql.password}" />  
        <!-- 指定连接数据库连接池的最小连接数 -->  
        <property name="minPoolSize" value="10" />  
        <!-- 指定连接数据库连接池的最大连接数 -->  
        <property name="maxPoolSize" value="30" />  
        <!-- 指定连接数据库连接池的连接的最大空闲时间 -->  
        <property name="maxIdleTime" value="1800" />  
        <property name="acquireIncrement" value="2" />  
        <property name="maxStatements" value="0" />  
        <!-- 指定连接数据库连接池的初始化连接数 -->  
        <property name="initialPoolSize" value="2" />  
        <property name="idleConnectionTestPeriod" value="1800" />  
        <!-- 当连接失败时,尝试重新连接的次数 -->  
        <property name="acquireRetryAttempts" value="30" />  
        <property name="breakAfterAcquireFailure" value="true" />  
        <property name="testConnectionOnCheckout" value="false" />  
    </bean>  
    <!--创建oracle jdbc数据源 -->  
    <bean id="oracleDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"  
        destroy-method="close">  
        <property name="driverClass" value="${jdbc.oracle.driver}" />  
        <property name="jdbcUrl" value="${jdbc.oracle.url}" />  
        <property name="user" value="${jdbc.oracle.username}" />  
        <property name="password" value="${jdbc.oracle.password}" />  
        <!-- 指定连接数据库连接池的最小连接数 -->  
        <property name="minPoolSize" value="10" />  
        <!-- 指定连接数据库连接池的最大连接数 -->  
        <property name="maxPoolSize" value="30" />  
        <!-- 指定连接数据库连接池的连接的最大空闲时间 -->  
        <property name="maxIdleTime" value="1800" />  
        <property name="acquireIncrement" value="2" />  
        <property name="maxStatements" value="0" />  
        <!-- 指定连接数据库连接池的初始化连接数 -->  
        <property name="initialPoolSize" value="2" />  
        <property name="idleConnectionTestPeriod" value="1800" />  
        <!-- 当连接失败时,尝试重新连接的次数 -->  
        <property name="acquireRetryAttempts" value="1" />  
        <property name="breakAfterAcquireFailure" value="true" />  
        <property name="testConnectionOnCheckout" value="false" />  
    </bean>  
    <!-- 创建MysqlSessionFactory-->  
    <bean id="mysqlSessionFactory"  
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">  
        <property name="dataSource" ref="mysqlDataSource" />  
        <property name="hibernateProperties">  
            <props>  
                <prop key="hibernate.show_sql">true</prop>  
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>  
                <prop key="current_session_context_class">thread</prop>  
            </props>  
        </property>  
        <property name="mappingResources">  
            <list>  
                <value>edu/njupt/zhb/model/mysql/Student.hbm.xml</value>  
            </list>  
        </property>  
    </bean>  

    <!-- 创建OracleSessionFactory-->  
    <bean id="oracleSessionFactory"  
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">  
        <property name="dataSource" ref="oracleDataSource" />  
        <property name="hibernateProperties">  
            <props>  
                <prop key="hibernate.show_sql">true</prop>  
                <prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>  
                <prop key="current_session_context_class">thread</prop>  
            </props>  
        </property>  
        <property name="mappingResources">  
            <list>  
                <value>edu/njupt/zhb/model/oracle/Student.hbm.xml</value>  
            </list>  
        </property>  
    </bean>  

    <!-- 配置Mysql事务 -->  
    <bean id="mysqlTransactionManager"  
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">  
        <property name="sessionFactory">  
            <ref local="mysqlSessionFactory" />  
        </property>  
    </bean>  
     <!--Mysql hibernate4必须配置为开启事务 否则 getCurrentSession()获取不到-->  
    <bean id="mysqlTransactionInterceptor"  
        class="org.springframework.transaction.interceptor.TransactionInterceptor">  
        <property name="transactionManager">  
            <ref local="mysqlTransactionManager" />  
        </property>  
        <property name="transactionAttributes">  
            <props>  
                <prop key="register">PROPAGATION_REQUIRED</prop>  
                <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>  
                <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>  
                <prop key="select*">PROPAGATION_REQUIRED,readOnly</prop>  
                <prop key="query*">PROPAGATION_REQUIRED,readOnly</prop>  
                <prop key="sync*">PROPAGATION_REQUIRED</prop>  
                <prop key="finish*">PROPAGATION_REQUIRED</prop>  
                <prop key="add*">PROPAGATION_REQUIRED</prop>  
                <prop key="insert*">PROPAGATION_REQUIRED</prop>  
                <prop key="edit*">PROPAGATION_REQUIRED</prop>  
                <prop key="update*">PROPAGATION_REQUIRED</prop>  
                <prop key="save*">PROPAGATION_REQUIRED</prop>  
                <prop key="remove*">PROPAGATION_REQUIRED</prop>  
                <prop key="delete*">PROPAGATION_REQUIRED</prop>  
                <prop key="modify*">PROPAGATION_REQUIRED</prop>  
                <prop key="*">PROPAGATION_REQUIRED,-java.lang.Exception</prop>  
            </props>  
        </property>  
    </bean>  

        <!-- 配置oracle事务 -->  
    <bean id="oracleTransactionManager"  
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">  
        <property name="sessionFactory">  
            <ref local="oracleSessionFactory" />  
        </property>  
    </bean>  
     <!--Mysql hibernate4必须配置为开启事务 否则 getCurrentSession()获取不到-->  
    <bean id="oracleTransactionInterceptor"  
        class="org.springframework.transaction.interceptor.TransactionInterceptor">  
        <property name="transactionManager">  
            <ref local="oracleTransactionManager" />  
        </property>  
        <property name="transactionAttributes">  
            <props>  
                <prop key="register">PROPAGATION_REQUIRED</prop>  
                <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>  
                <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>  
                <prop key="select*">PROPAGATION_REQUIRED,readOnly</prop>  
                <prop key="query*">PROPAGATION_REQUIRED,readOnly</prop>  
                <prop key="sync*">PROPAGATION_REQUIRED</prop>  
                <prop key="finish*">PROPAGATION_REQUIRED</prop>  
                <prop key="add*">PROPAGATION_REQUIRED</prop>  
                <prop key="insert*">PROPAGATION_REQUIRED</prop>  
                <prop key="edit*">PROPAGATION_REQUIRED</prop>  
                <prop key="update*">PROPAGATION_REQUIRED</prop>  
                <prop key="save*">PROPAGATION_REQUIRED</prop>  
                <prop key="remove*">PROPAGATION_REQUIRED</prop>  
                <prop key="delete*">PROPAGATION_REQUIRED</prop>  
                <prop key="modify*">PROPAGATION_REQUIRED</prop>  
                <prop key="*">PROPAGATION_REQUIRED,-java.lang.Exception</prop>  
            </props>  
        </property>  
    </bean>  

    <!-- autoproxy 自动创建代理-->  
      <bean id="ProxyCreator" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">  
         <property name="beanNames">  
             <list>  
                <value>*ServiceImpl</value>  
             </list>  
         </property>  
         <property name="interceptorNames">  
             <list>  
                <value>mysqlTransactionInterceptor</value>  
                <value>oracleTransactionInterceptor</value>  
             </list>  
         </property>  
      </bean>  
    <!--********************************注入Mysql Dao*********************************************-->  
    <bean id="mysqlBaseDao" class="edu.njupt.zhb.dao.MysqlBaseDao">  
        <property name="sessionFactory" ref="mysqlSessionFactory"></property>  
    </bean>  
    <!--********************************注入Oracle Dao*********************************************-->  
    <bean id="oracleBaseDao" class="edu.njupt.zhb.dao.OracleBaseDao">  
        <property name="sessionFactory" ref="oracleSessionFactory"></property>  
    </bean>  
    <!--********************************注入Services********************************-->  
    <!--********************************注入Action********************************-->  
</beans>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值