Spring配置多个连接池

项目中需要同时连接多个数据库的时候,如何才能在需要用到哪个数据库就连接哪个数据库呢?
Spring中有关于dataSource的配置:
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
        destroy-method="close">
        <property name="driverClass" value="${jdbc.driverClass}" />
        <property name="jdbcUrl" value="${jdbc.jdbcUrl}" />
        <property name="user" value="${jdbc.user}" />
        <property name="password" value="${jdbc.password}" />
        <property name="minPoolSize" value="${c3p0.minPoolSize}" />
        <property name="maxPoolSize" value="${c3p0.maxPoolSize}" />
        <property name="maxIdleTime" value="${c3p0.maxIdleTime}" />
        <property name="acquireIncrement" value="${c3p0.acquireIncrement}" />
        <property name="initialPoolSize" value="${c3p0.initialPoolSize}" />
        <property name="idleConnectionTestPeriod" value="${c3p0.idleConnectionTestPeriod}" />
        <property name="acquireRetryAttempts" value="${c3p0.acquireRetryAttempts}" />
    </bean>
一个dataSource对应一个连接池,也就是对应一个数据库,新增一个数据库就需要新增一个dataSource:
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
        destroy-method="close">
        <property name="driverClass" value="${jdbc.driverClass}" />
        <property name="jdbcUrl" value="${jdbc.jdbcUrl_ddns}" />
        <property name="user" value="${jdbc.user}" />
        <property name="password" value="${jdbc.password}" />
        <property name="minPoolSize" value="${c3p0.minPoolSize}" />
        <property name="maxPoolSize" value="${c3p0.maxPoolSize}" />
        <property name="maxIdleTime" value="${c3p0.maxIdleTime}" />
        <property name="acquireIncrement" value="${c3p0.acquireIncrement}" />
        <property name="initialPoolSize" value="${c3p0.initialPoolSize}" />
        <property name="idleConnectionTestPeriod" value="${c3p0.idleConnectionTestPeriod}" />
        <property name="acquireRetryAttempts" value="${c3p0.acquireRetryAttempts}" />
    </bean>
当然,对应的sessionFactory也要新增一个配置:
    <bean id="sessionFactory_ddns" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource">
            <ref bean="dataSource_ddns" />
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
                <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
                <prop key="hibernate.autoReconnection">
                    ${hibernate.autoReconnection}
                </prop>
                <prop key="hibernate.jdbc.batch_size">
                    ${hibernate.jdbc.batch_size}
                </prop>
            </props>
        </property>
        <property name="mappingDirectoryLocations">
            <list>
                <value>classpath:com/tplink/cloud/WebManager/ddns
                </value>
            </list>
        </property>
    </bean>
其中指定com/tplink/cloud/WebManager/ddns,只有这个包需要连到新增的数据库,引用到sessionFactory的类都要新建一个,如commonDao, result等,当然,这里只需要改个名字,并不需要真正的重新写一个类,这样就完成了多连接池的配置。[align=left][/align]

你可能感兴趣的:(spring)