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).
分享到:
相关推荐
to chain replication and switching a slave from chain replication to master-slave is something that will interest the readers. Chapter 10, Table Maintenance, covers cloning tables. Managing big tables...
"A self-organising topology for master-slave replication in p2p TCD-CS-2006-10.pdf"论文可能介绍了一种动态调整主从复制关系的策略,以适应P2P网络中的变化。这种拓扑结构有助于提高系统的可靠性和容错性。 5...
* Set up master-slave and automatic failover replication in MongoDB * Use sharding to scale MongoDB horizontally, and learn how it impacts applications * Get example applications written in Java, ...
GRANT REPLICATION SLAVE ON *.* TO test@192.168.0.2 IDENTIFIED BY '********'; ``` - 这里指定了用户名为`test`,密码为`********`,并且只能从IP地址为`192.168.0.2`的服务器进行复制。 3. **重启MySQL服务*...
# Replication Slave (comment out master section to use this) ``` - **[client]** 部分设置了客户端连接所需的端口和socket路径。 - **[mysqld]** 部分是MySQL服务器的核心配置: - `basedir` 和 `datadir`...
第 1 章Redis 介绍. . . . . . . . . ....第 2 章数据类型初探....字符串 (Strings) ....列表 (Lists)....集合 (Sets) ....哈希 / 散列 (Hashes) ....有序集合 (Sorted sets) ....位图 (Bitmaps) 和超重对数 (HyperLogLogs) ....
第 1 章Redis 介绍. . . . . . . . . ....第 2 章数据类型初探....字符串 (Strings) .... ....列表 (Lists)....集合 (Sets) ....哈希 / 散列 (Hashes) ....有序集合 (Sorted sets) ....位图 (Bitmaps) 和超重对数 (HyperLogLogs) ....