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"); .......
}
}
来自:http://www.cnblogs.com/taven/archive/2013/04/24/3040489.html
MySQL 读写分离 使用驱动com.mysql.jdbc.ReplicationDriver的更多相关文章
- mysql运维入门6:MySQL读写分离
Amoeba 以MySQL为底层数据存储,并对应用提供MySQL协议接口的proxy 集中想用应用的请求,根据用户事先设置的规则,将SQL请求发送到特定的数据库上执行 基于此可以实现负载均衡.读写分离 ...
- mysql读写分离[高可用]
顾名思义, 在mysql负载均衡中有多种方式, 本人愚钝,只了解驱动中间件和mysql_proxy两种方式, 对于驱动,利用的是ReplicationDriver,具体请看远哥的这篇文章: MySQL ...
- SSM/SSH框架的MySQL 读写分离实现的一种简单方法
简介 MySQL已经是使用最为广泛的一种数据库,往往实际使用过程中,为实现高可用及高性能,项目会采用主丛复制的方式实现读写分离.MySQL本身支持复制,通过简单的配置即可实现一主多从的配置,具体实现可 ...
- PHP代码实现MySQL读写分离
关于MySQL的读写分离有几种方法:中间件,Mysql驱动层,代码控制 关于中间件和Mysql驱动层实现Mysql读写分离的方法,今天暂不做研究, 这里主要写一点简单的代码来实现由PHP代码控制MyS ...
- php实现MySQL读写分离
MySQL读写分离有好几种方式 MySQL中间件 MySQL驱动层 代码控制 关于 中间件 和 驱动层的方式这里不做深究 暂且简单介绍下 如何通过PHP代码来控制MySQL读写分离 我们都知道 &q ...
- Mysql读写分离-Amoeba Proxy
参考:http://www.linuxidc.com/Linux/2015-10/124115.htm 一个完整的MySQL读写分离环境包括以下几个部分: 应用程序client database pr ...
- 构建高性能web之路------mysql读写分离实战(转)
一个完整的mysql读写分离环境包括以下几个部分: 应用程序client database proxy database集群 在本次实战中,应用程序client基于c3p0连接后端的database ...
- mysql读写分离实战
一个完整的MySQL读写分离环境包括以下几个部分: 应用程序client database proxy database集群 在本次实战中,应用程序client基于c3p0连接后端的database ...
- 使用Amoeba实现mysql读写分离机制
Amoeba的实用指南 http://docs.hexnova.com/amoeba/ 如何实现mysql读写分离 : 通常来说有两种方式: 1,应用程序层实现 2,中间件层实现 应用层实现 应用层实 ...
随机推荐
- SyncThingWin -- Run syncthing as a windows service
SyncThingWin Auto restart and minor bug fixes bloones released this on 23 Dec 2014 There is now an a ...
- java跨域解决
import java.util.ArrayList; import java.util.List; import org.springframework.context.annotation.Bea ...
- 模拟出ios中流行的黑色背景底
[activityIndicatorView setBackgroundColor:[UIColor colorWithRed: green: blue: alpha:0.4]]; 用上面的语法,可以 ...
- Java String和Date的转换 Date类型操作
String—>Date String dateString = "2012-12-06 "; try { SimpleDateFormat sdf = new Simple ...
- 演示Microsoft Advertising SDK for Windows Phone 8.1
演示Microsoft Advertising SDK for Windows Phone 8.1,Only for Windows Phone 8.1 1.在References上点右键,添加引用, ...
- python笔记25-mock-server之moco
前言 mock除了用在单元测试过程中,还有一个用途,当前端开发在开发页面的时候,需要服务端提供API接口 此时服务端没开发完成,或者说没搭建测试环境,这个时候前端开发会自己mock一个api服务端,自 ...
- struts2 iterator 迭代标签只显示前五条记录
<s:iterator value="#session.produceLists" var="produce" begin="0" e ...
- SQL_求集合中每天最大时间记录的总和
--问题求 集合中每天最大时间的总和 表中的数据 列: 用户 分数 时间 A 2 2014-01-01 01:00:00 A 2 2014-01-01 02:00:00 A 2 2014-01-01 ...
- Composer使用体验
什么是Composer 简单来说,Composer是PHP的包依赖管理器.但是Composer并不是类似于Yum.Apt的包管理器.Composer可以用于包或者第三方库的安装,但是可以选择在Proj ...
- 【屌丝程序的口才逆袭演讲稿50篇】第十三篇:爱迪生欺骗了我们!【张振华.Jack】
演讲稿主题:<爱迪生欺骗了我们> --作者:张振华Jack.摘抄<马云为雅虎员工的演讲稿:爱迪生欺骗了我们> 非常多人都记得爱迪生说的 ...