Using Master/Slave Replication with ReplicationConnection
来源:互联网 发布:软件开发毕业设计 编辑:程序博客网 时间:2024/06/10 03:45
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).
- Using Master/Slave Replication with ReplicationConnection
- 17.3.2 Using Replication with Different Master and Slave Storage Engines
- MySQL Master-Slave Replication
- MySQL Master-Slave Replication
- Mysql 复制 配置详解, Replication with Master/Slave
- 使用 ReplicationConnection 进行 Master/Slave 间数据同步
- 使用 ReplicationConnection 连接 Master/Slave 实现负载均衡
- 使用 ReplicationConnection 连接 Master/Slave 实现负载均衡
- 搭建mysql master-slave replication
- MySQL Master Slave Replication配置
- Replication-Redis(redis master-slave)
- mysql同步复制报Slave can not handle replication events with the checksum that master 错误
- mysql的master和slave同步(replication)
- mysql-proxy实现replication(master&slave)
- mysql5.5 master-slave(Replication)配置
- mysql_upgrade 引起的master/slave replication 中断
- mysql实现master-slave的replication方案
- mysql replication copy master to slave
- 李阳疯狂英语突破对话(62)-看病
- 匿名方法与泛型编程(写给自己看的)
- J2EE petstorcore的安装
- DELL电脑安装linux后看不见鼠标指针的解决方法
- S3c2410/2440的Eboot流程介绍
- Using Master/Slave Replication with ReplicationConnection
- .Net环境下的缓存技术介绍
- Google 科学家吴军写的《数学之美》系列文章
- request.getParameter() 和request.getAttribute() 区别
- 巧遇红楼梦中人
- 用MySQL-Proxy实现读写分离
- 女人的功能
- 争辩
- 遥感图像增强方法应用