Datasource mysql failover - howto configure?

3k Views Asked by At

I'm using a spring project with org.apache.commons.dbcp.BasicDataSource datasource.

If i want my Database to support a failover where do i configure it?

I cant find any references with google

1

There are 1 best solutions below

1
On

You were a bit generic in the question. I describe some possible solutions.
If you can use c3p0 here is an example with mysql server. In the tomcat server.xml define this:

    <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"
    />

In case of sql server replace

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

with

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

In case of oracle another solution is recomended. I just give you the link to the offical spring documentation:

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

and I just copy here the spring bean to be used for 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>

Other approaches are possible. E.g. using the AbstractRoutingDataSource offered from springframework.