How to use the database as backup / failover in sleep mode?

My application is based on sleep mode to receive data from mysql server. This mysql server is replicated to another instance of mysql server. Today I got downtime as the main database server was without any notifications. To avoid any future random problem, I plan to add functionality that will allow the system to connect to the secondary database if it detects a primary snapshot.

Is there a way by which I can use the hibernate library to enable this function?

+5
source share
4 answers

, . , .

package com.vsd.server.hibernate;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

import org.apache.commons.lang.StringUtils;
import org.hibernate.HibernateException;
import org.hibernate.connection.C3P0ConnectionProvider;

public class FailoverConnectionProvider extends C3P0ConnectionProvider {

    String password;
    String username;
    String connectionString;
    String failover_connstring;

    @Override
    public Connection getConnection() throws SQLException {

        Connection conn = null;

        try {
            conn = DriverManager.getConnection(connectionString, username, password);
        } catch (Exception ex) {
            conn = DriverManager.getConnection(failover_connstring, username, password);
        }

        if(conn == null){
            throw new IllegalStateException("Database connection was not initialized");
        }

        return conn;

    }

    @Override
    public void configure(Properties properties) throws HibernateException {
        failover_connstring = properties.getProperty("hibernate.connection.failover.url");

        if (StringUtils.isBlank(connectionString)
                && StringUtils.isBlank(failover_connstring)
                && StringUtils.isBlank(username)
                && StringUtils.isBlank(password)) {
            throw new IllegalStateException("Unable to initialize connection provider for hibernate");
        }
    }
}
+2

, - .

, , ( ).

(...) , hibernate ?

, Hibernate . MySQL JDBC. , MySQL, :

, ( ):

" JDBC" (lbpol), JDBC , . . JDBC Pool (lbpool).

+6

You can try to add fault tolerance to the data layer (any type of database). There are several libraries for this. For instance:

  • "Sequoia"
  • "FODS - Fail-over data-source"
0
source
0
source

All Articles