w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
Datasource mysql failover - howto configure?

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.





© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.