1

我正在使用带有 org.apache.commons.dbcp.BasicDataSource 数据源的 spring 项目。

如果我希望我的数据库支持故障转移,我应该在哪里配置它?

我在谷歌上找不到任何参考资料

4

1 回答 1

2

你的问题有点笼统。我描述了一些可能的解决方案。
如果你可以使用 c3p0 这里是一个 mysql 服务器的例子。在 tomcat server.xml 中定义:

    <Resource
    name="jdbc/trm"
    type="com.mchange.v2.c3p0.ComboPooledDataSource"
    driverClass="com.microsoft.sqlserver.jdbc.SQLServerDriver"
    password="password"
    user="username"
    auth="Container"
    description="DB Connection pool for TRM application"
    minPoolSize="2"
    maxPoolSize="4"
    acquireIncrement="1"
    factory="org.apache.naming.factory.BeanFactory"
    jdbcUrl=jdbc:mysql://localhost:3306,backupdb.something.com:3306/dbname
    preferredTestQuery="SELECT 'Connection' = 'true'"
    testConnectionOnCheckout="true"
    />

如果是 sql server 更换

    jdbcUrl=jdbc:mysql://localhost:3306,backupdb.something.com:3306/dbname

     jdbcUrl="jdbc:sqlserver://mainserver:1433;failoverPartner=backupserver;databaseName=nameofyourdatabase;applicationName=appname"

在 oracle 的情况下,建议使用另一种解决方案。我只是给你官方 spring 文档的链接:

http://static.springsource.org/spring-data/jdbc/docs/current/reference/html/orcl.failover.html

我只是在这里复制用于完整性的spring bean。

    <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:orcl="http://www.springframework.org/schema/data/orcl"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.0.xsd
           http://www.springframework.org/schema/data/orcl
           http://www.springframework.org/schema/data/orcl/spring-data-orcl-1.0.xsd">

        <orcl:pooling-datasource id="racDataSource"
            url="jdbc:oracle:thin:@(description=(address_list=
                (address=(host=rac1)(protocol=tcp)(port=1521))
                (address=(host=rac2)(protocol=tcp)(port=1521)))
                (connect_data=(service_name=racdb1)))"
            properties-location="classpath:orcl.properties"
            fast-connection-failover-enabled="true" 1
            ONS-configuration="rac1:6200,rac2:6200"/> 2

        <bean id="transactionManager" 
              class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="racDataSource"/>
        </bean>

    </beans>

其他方法是可能的。例如,使用 springframework 提供的 AbstractRoutingDataSource。

于 2013-09-01T08:28:47.697 回答