`
Jatula
  • 浏览: 276622 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

Using Master/Slave Replication with ReplicationCon

阅读更多

refers to:http://dev.mysql.com/doc/refman/5.1/en/connector-j-reference-replication-connection.html

 

Starting with Connector/J 3.1.7, we've made available a variant of the driver that will automatically send queries to a read/write master, or a failover or round-robin loadbalanced set of slaves based on the state of Connection.getReadOnly() .

An application signals that it wants a transaction to be read-only by calling Connection.setReadOnly(true), this replication-aware connection will use one of the slave connections, which are load-balanced per-vm using a round-robin scheme (a given connection is sticky to a slave unless that slave is removed from service). If you have a write transaction, or if you have a read that is time-sensitive (remember, replication in MySQL is asynchronous), set the connection to be not read-only, by calling Connection.setReadOnly(false) and the driver will ensure that further calls are sent to the master MySQL server. The driver takes care of propagating the current state of autocommit, isolation level, and catalog between all of the connections that it uses to accomplish this load balancing functionality.

To enable this functionality, use the " com.mysql.jdbc.ReplicationDriver " class when configuring your application server's connection pool or when creating an instance of a JDBC driver for your standalone application. Because it accepts the same URL format as the standard MySQL JDBC driver, ReplicationDriver does not currently work with java.sql.DriverManager -based connection creation unless it is the only MySQL JDBC driver registered with the DriverManager .

Here is a short, simple example of how ReplicationDriver might be used in a standalone application.

import java.sql.Connection;
import java.sql.ResultSet;
import java.util.Properties;

import com.mysql.jdbc.ReplicationDriver;

public class ReplicationDriverDemo {

  public static void main(String[] args) throws Exception {
    ReplicationDriver driver = new ReplicationDriver();

    Properties props = new Properties();

    // We want this for failover on the slaves
    props.put("autoReconnect", "true");

    // We want to load balance between the slaves
    props.put("roundRobinLoadBalance", "true");

    props.put("user", "foo");
    props.put("password", "bar");

    //
    // Looks like a normal MySQL JDBC url, with a
    // comma-separated list of hosts, the first 
    // being the 'master', the rest being any number
    // of slaves that the driver will load balance against
    //

    Connection conn =
        driver.connect("jdbc:mysql://master,slave1,slave2,slave3/test",
            props);

    //
    // Perform read/write work on the master
    // by setting the read-only flag to "false"
    //

    conn.setReadOnly(false);
    conn.setAutoCommit(false);
    conn.createStatement().executeUpdate("UPDATE some_table ....");
    conn.commit();

    //
    // Now, do a query from a slave, the driver automatically picks one
    // from the list
    //

    conn.setReadOnly(true);

    ResultSet rs = 
      conn.createStatement().executeQuery("SELECT a,b FROM alt_table");

     .......
  }
}

You may also want to investigate the Load Balancing JDBC Pool (lbpol) tool, which provides a wrapper around the standard JDBC driver and allows you to use DB connection pools that includes checks for system failures and uneven load distribution. For more information, see Load Balancing JDBC Pool (lbpool).

分享到:
评论

相关推荐

    ActiveMQ Master/Slave 主从配置

    ActiveMQ支持多种高可用的主从配置模式,包括Shared File System Master/Slave、JDBC Master/Slave和Replicated LevelDB Store。文章主要介绍了使用JDBC Master/Slave模式来实现ActiveMQ的高可用配置。 在JDBC ...

    Modbus-Master-Slave-for-Arduino-master.zip_Master/Slave_arduino

    标题中的"Modbus-Master-Slave-for-Arduino-master.zip_Master/Slave_arduino"指出这是一个关于Arduino平台的Modbus主从通信库。Modbus是一种广泛使用的工业通信协议,允许不同设备之间交换数据,尤其在自动化系统中...

    wb_master.zip_Master/Slave_wbmaster_wishbone

    描述中提到"this is the wishbone master interface to connect with wishbone slave and a clock module",这意味着设计中不仅包含了Wishbone主接口,还整合了一个时钟模块。时钟模块在数字电路中起到同步所有操作...

    Master_Slave_步进电机_Master/Slave_stm32步进电机_stm32步进电机_

    标题中的“Master_Slave_步进电机_Master/Slave_stm32步进电机_stm32步进电机_”表明我们将讨论一个基于STM32微控制器的步进电机控制系统,其中采用主从(Master/Slave)架构。在这个系统中,主控制器(Master)负责...

    mater_slave_inter.gz_Master/Slave

    在电子和计算机通信领域,主从结构(Master/Slave)是一种常见的通信模式,尤其是在I2C(Inter-Integrated Circuit)总线协议中。标题“mater_slave_inter.gz_Master/Slave”暗示了我们即将讨论的内容与I2C主从通信...

    64143308_LOGO_Set9_Master_Slave_CODE_v21_Master/Slave_siemens_

    8配合主/从(Master/Slave)通信模式构建智能楼宇控制系统,这主要基于"64143308_LOGO_Set9_Master_Slave_CODE_v21_Master/Slave_siemens_"项目。 LOGO! 8系统以其紧凑的尺寸、易于编程和强大的功能而受到赞誉。在...

    Master_Slave_Interrupt.rar_Master/Slave

    在本资源"Master_Slave_Interrupt.rar_Master/Slave"中,我们聚焦于ARM Keil开发环境下的I2C外设使用,特别是主-从模式的中断处理。 ARM Keil是ARM公司提供的一种集成开发环境(IDE),支持多种基于ARM架构的微控制...

    Master_slave_multi_machine.rar_Master-slave_Master/Slave_multi m

    标题“Master_slave_multi_machine.rar_Master-slave_Master/Slave_multi m”暗示了我们正在讨论的焦点是关于主从结构在多机器环境中的应用。 描述“主从多机通信系统问题研究建模及其实性研究”表明我们将深入探讨...

    MASTERv1-20200914T103502Z-001_Master/Slave_arduino_

    【标题】"MASTERv1-20200914T103502Z-001_Master/Slave_arduino_" 提供的是一个关于 Arduino 的主从(Master/Slave)项目,时间戳表明这个版本创建于2020年9月14日。在 Arduino 的上下文中,主从架构常用于多设备...

    PWM.rar_ATMEGA 8 PWM_Master/Slave

    在ATMEGA 8 PWM_Master/Slave配置中,我们探讨的是如何设置该微控制器作为PWM主从系统的中心单元。 PWM的工作原理是通过调整脉冲宽度来改变信号的平均功率,从而实现对负载的模拟控制。在ATMEGA 8中,PWM功能由硬件...

    Mysql-master-slave.rar_Master/Slave_slave mysql

    5. **启动复制**:在从服务器上,使用`START SLAVE`命令启动复制进程。此时,从服务器将开始读取主服务器的binlog并执行相应的SQL语句。 二、主从复制工作原理 1. **写操作**:在主服务器上,任何数据修改都会被...

    GPIF_EXAMPLE1.zip_Master/Slave_cypress GPIF_gpif

    在 Cypress GPIF_EXAMPLE1.zip 文件中,我们可以找到一个关于 Master/Slave 模式的具体设计实例,这对于初学者了解和掌握 GPIF 的工作原理及应用具有很高的参考价值。 GPIF 是 Cypress 的 PSoC(Programmable ...

    tvip-axi-master_Master/Slave_axivip_tvip_vip_AMBA_

    在本项目“tvip-axi-master_Master/Slave_axivip_tvip_vip_AMBA”中,我们主要探讨的是与AXI协议相关的VIP(Verification Intellectual Property)组件,包括Master和Slave端。 VIP是验证IP的核心组件,用于在系统...

    mysql master/slave介绍及配置

    Master-Slave的数据库机构解决了很多问题,特别是read/write比较高的web2.0应用: 1、写操作全部在Master结点执行,并由Slave数据库结点定时(默认60s)读取Master的bin-log 2、将众多的用户读请求分散到更多的...

    Auto-Mode.rar_Master/Slave_gpif master

    "Auto-Mode.rar_Master/Slave_gpif master" 文件压缩包提供的是 Cypress GPIF 的配置代码,专门针对自动模式以及主从模式进行了优化。下面我们将详细探讨 GPIF、自动模式以及主从模式的相关知识点。 1. GPIF 概述:...

    i2s_master_slave_vhdl.rar_I2S vhdl_Master/Slave_i2s slave_verilo

    在这个“i2s_master_slave_vhdl.rar”压缩包中,包含的是用VHDL语言实现的I2S主从模式的代码,同时也有一个Verilog版本的I2S slave模块,这对于理解和设计嵌入式音频系统非常有帮助。 首先,我们要理解I2S总线的...

    master_slave_tx_rx_code-master.zip_Master/Slave

    在这个名为"master_slave_tx_rx_code-master.zip_Master/Slave"的压缩包中,我们很显然会找到与CC2500相关的主(Master)从(Slave)模式的传输和接收代码。这种通信模式是许多无线网络协议的基础,如I2C、SPI和UART...

    I2C_control.zip_I2C slave_I2C_control_Master/Slave_i2c slave_vh

    标题"I2C_control.zip_I2C slave_I2C_control_Master/Slave_i2c slave_vh"揭示了这个压缩包内容是关于I2C协议的,其中包含了主设备和从设备的实现,特别提到了`i2c slave_vh`,这可能是一个用VHDL编写的I2C从设备的...

    Modbus_Master_slave_STM32modrtu主从站_modbusRTU_modbus_Master/Slave

    《Modbus_Master_slave_STM32modrtu主从站_modbusRTU_modbus_Master/Slave》 在工业自动化领域,Modbus协议是广泛应用的一种通信协议,尤其在嵌入式系统如STM32微控制器中,它扮演着重要的角色。本资源主要涵盖STM...

Global site tag (gtag.js) - Google Analytics