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

配置2个数据源,2个对应的SessionFactory,2个事务等。Spring的配置如下:

  
<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">  
      
    <context:property-placeholder location="classpath:datasource.properties" />  
      
    <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>  
      
    <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>  
      
    <bean id="mysqlSessionFactory"  
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">  
        <property name="dataSource" ref="mysqlDataSource" />  
        <property name="hibernateProperties">  
            <props>  
                <prop key="hibernate.show_sql">trueprop>  
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialectprop>  
                <prop key="current_session_context_class">threadprop>  
            props>  
        property>  
        <property name="mappingResources">  
            <list>  
                <value>edu/njupt/zhb/model/mysql/Student.hbm.xmlvalue>  
            list>  
        property>  
    bean>  

      
    <bean id="oracleSessionFactory"  
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">  
        <property name="dataSource" ref="oracleDataSource" />  
        <property name="hibernateProperties">  
            <props>  
                <prop key="hibernate.show_sql">trueprop>  
                <prop key="hibernate.dialect">org.hibernate.dialect.OracleDialectprop>  
                <prop key="current_session_context_class">threadprop>  
            props>  
        property>  
        <property name="mappingResources">  
            <list>  
                <value>edu/njupt/zhb/model/oracle/Student.hbm.xmlvalue>  
            list>  
        property>  
    bean>  

      
    <bean id="mysqlTransactionManager"  
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">  
        <property name="sessionFactory">  
            <ref local="mysqlSessionFactory" />  
        property>  
    bean>  
       
    <bean id="mysqlTransactionInterceptor"  
        class="org.springframework.transaction.interceptor.TransactionInterceptor">  
        <property name="transactionManager">  
            <ref local="mysqlTransactionManager" />  
        property>  
        <property name="transactionAttributes">  
            <props>  
                <prop key="register">PROPAGATION_REQUIREDprop>  
                <prop key="get*">PROPAGATION_REQUIRED,readOnlyprop>  
                <prop key="find*">PROPAGATION_REQUIRED,readOnlyprop>  
                <prop key="select*">PROPAGATION_REQUIRED,readOnlyprop>  
                <prop key="query*">PROPAGATION_REQUIRED,readOnlyprop>  
                <prop key="sync*">PROPAGATION_REQUIREDprop>  
                <prop key="finish*">PROPAGATION_REQUIREDprop>  
                <prop key="add*">PROPAGATION_REQUIREDprop>  
                <prop key="insert*">PROPAGATION_REQUIREDprop>  
                <prop key="edit*">PROPAGATION_REQUIREDprop>  
                <prop key="update*">PROPAGATION_REQUIREDprop>  
                <prop key="save*">PROPAGATION_REQUIREDprop>  
                <prop key="remove*">PROPAGATION_REQUIREDprop>  
                <prop key="delete*">PROPAGATION_REQUIREDprop>  
                <prop key="modify*">PROPAGATION_REQUIREDprop>  
                <prop key="*">PROPAGATION_REQUIRED,-java.lang.Exceptionprop>  
            props>  
        property>  
    bean>  

          
    <bean id="oracleTransactionManager"  
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">  
        <property name="sessionFactory">  
            <ref local="oracleSessionFactory" />  
        property>  
    bean>  
       
    <bean id="oracleTransactionInterceptor"  
        class="org.springframework.transaction.interceptor.TransactionInterceptor">  
        <property name="transactionManager">  
            <ref local="oracleTransactionManager" />  
        property>  
        <property name="transactionAttributes">  
            <props>  
                <prop key="register">PROPAGATION_REQUIREDprop>  
                <prop key="get*">PROPAGATION_REQUIRED,readOnlyprop>  
                <prop key="find*">PROPAGATION_REQUIRED,readOnlyprop>  
                <prop key="select*">PROPAGATION_REQUIRED,readOnlyprop>  
                <prop key="query*">PROPAGATION_REQUIRED,readOnlyprop>  
                <prop key="sync*">PROPAGATION_REQUIREDprop>  
                <prop key="finish*">PROPAGATION_REQUIREDprop>  
                <prop key="add*">PROPAGATION_REQUIREDprop>  
                <prop key="insert*">PROPAGATION_REQUIREDprop>  
                <prop key="edit*">PROPAGATION_REQUIREDprop>  
                <prop key="update*">PROPAGATION_REQUIREDprop>  
                <prop key="save*">PROPAGATION_REQUIREDprop>  
                <prop key="remove*">PROPAGATION_REQUIREDprop>  
                <prop key="delete*">PROPAGATION_REQUIREDprop>  
                <prop key="modify*">PROPAGATION_REQUIREDprop>  
                <prop key="*">PROPAGATION_REQUIRED,-java.lang.Exceptionprop>  
            props>  
        property>  
    bean>  

      
      <bean id="ProxyCreator" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">  
         <property name="beanNames">  
             <list>  
                <value>*ServiceImplvalue>  
             list>  
         property>  
         <property name="interceptorNames">  
             <list>  
                <value>mysqlTransactionInterceptorvalue>  
                <value>oracleTransactionInterceptorvalue>  
             list>  
         property>  
      bean>  
      
    <bean id="mysqlBaseDao" class="edu.njupt.zhb.dao.MysqlBaseDao">  
        <property name="sessionFactory" ref="mysqlSessionFactory">property>  
    bean>  
      
    <bean id="oracleBaseDao" class="edu.njupt.zhb.dao.OracleBaseDao">  
        <property name="sessionFactory" ref="oracleSessionFactory">property>  
    bean>  
      
      
beans>

你可能感兴趣的:(Spring,Hibernate)