我正在使用带有org.apache.commons.dbcp.BasicDataSource数据源的spring项目。
如果我希望我的数据库支持故障转移,我在哪里配置它?
我无法找到谷歌的任何参考资料
答案 0 :(得分:3)
你在这个问题上有点泛泛。我描述了一些可能的解决方案 如果你可以使用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 replace
jdbcUrl=jdbc:mysql://localhost:3306,backupdb.something.com:3306/dbname
与
jdbcUrl="jdbc:sqlserver://mainserver:1433;failoverPartner=backupserver;databaseName=nameofyourdatabase;applicationName=appname"
在oracle的情况下,建议另一个解决方案。我只是给你官方弹簧文档的链接:
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。