2013-09-01 43 views

回答

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服务器的情况下,与

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

取代

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

在oracle中另一溶液recomended的情况下。我只是给你链接到官方Spring文档:

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

,我只是在这里复制的Spring bean用于completness。

<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。

+0

对于MySQL,您使用的是什么driverClass? com.mysql.jdbc.ReplicationDriver? – emilebaizel