使用Mysql新版驱动的ReplicationDriver实现MySQL读写分离
来源:互联网 发布:世界最快的数学计算法 编辑:程序博客网 时间:2024/05/29 05:03
在用过Amoeba 和 Cobar,还有dbware 等读写分离组件后,今天我的一个好朋友跟我讲,MySQL自身的也是可以读写分离的,因为他们提供了一个新的驱动,叫 com.mysql.jdbc.ReplicationDriver
说明文档:http://dev.mysql.com/doc/refman/5.1/en/connector-j-reference-replication-connection.html
代码例子:
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:replication://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"); ....... }}
0 0
- 使用Mysql新版驱动的ReplicationDriver实现MySQL读写分离
- mysql ReplicationDriver做读写分离
- 使用Spring实现Mysql的读写分离
- mysql 驱动方式实现读写分离
- mysql读写分离的实现
- mysql读写分离的实现
- 使用mysql-proxy实现mysql的读写分离
- MYSQL实现读写分离
- mysql读写分离实现
- Mysql实现读写分离
- 如何使用"MySQL-Proxy"实现读写分离
- 如何使用"MySQL-Proxy"实现读写分离
- 使用Amoeba实现MySQL读写分离
- 使用Amoeba实现mysql读写分离
- 使用Amoeba 实现MySQL DB 读写分离
- mysql读写分离(使用Atlas实现)
- mysql读写分离(使用Atlas实现)
- 使用Atlas 实现 MySQL读写分离
- Linux内核开发者峰会照的全家福-070911
- 循环(1)
- csdn乱码
- 一次向svn中增加所有新增文件 svn add all new files
- VB
- 使用Mysql新版驱动的ReplicationDriver实现MySQL读写分离
- Ruby On Rails--Active Record Validations(校验)
- 多线程&网络总结
- 控件AutoCompleteTextView 自动提示de 学习
- 随机10个数和 三
- 正则表达式初步
- 9个循环2
- Matlab R2014a 由.m文件产生C代码,并在VS2010中测试
- 获取driver导出的所有symbol