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,中间件层实现 应用层实现 应用层实 ...
随机推荐
- IOS快速入门
http://www.cnblogs.com/wellsoho/p/4313312.html
- 用资源管理器右键编译 Visual Studio 解决方案文件
每次改动 VC 工程之后都要重新编译,每次 VS 又会生成调试数据库文件,很费时间,于是研究了一下如何在资源管理器中直接编译,还真发现了解决办法. 以下是适用 Visual Studio 2008 的 ...
- jQuery中的观察者模式(Observer Pattern)
在jQuery中,on方法可以为元素绑定事件,trigger方法可以手动触发事件,围绕这2个方法,我们来体验jQuery中的观察者模式(Observer Pattern). ■ on方法绑定内置事件, ...
- [runtime] MAObjCRuntime
MAObjCRuntime 源码地址:(引入头文件MARTNSObject.h即可,非arc环境下) http://pan.baidu.com/s/1eQ6776U https://github.co ...
- iOS获取网络类型的四种方法
Reachability类只能区分WIFI和WWAN类型,却无法区分2G网和3G网. 网上也有些方法,却都存在Bug. 经过网上查找资料和测试,基本上总结了以下几种方法: 1.使用导航栏的方式:(私有 ...
- easyui input设置为disabled提交后获取不到属性值
在做网站管理后台的用户修改功能时,由于当前用户修改个人信息时规定用户名不能修改,故使用了input标签的disabled属性,但是在提交数据后却发现用户名显示为空了.后 来一查才知道input设置为d ...
- easyui 排序实现
1.对easyui datagrid 返回的数据,进行排序处理,便于搜索到我们的有用的信息. 例如: 2.datagrid 需要设置 sortable : true { field : 'crtTi ...
- 解决 Ubuntu Software (Software Center) Crash 问题
问题描述: no application data found 解决方式: sudo apt purge gnome-software ubuntu-software sudo apt autorem ...
- ORA-12560: TNS: 协议适配器错误的解决方法
ORA-12560: TNS: 协议适配器错误的解决方法 造成ORA-12560: TNS: 协议适配器错误的问题的原因有三个: 1.监听服务没有起起来.windows平台个一如下操作:开始---程序 ...
- 使用ADB管理Andorid系统软件
# Usageadb shell pm list packages -fadb shell pm list packages -eadb shell pm list packages -dadb sh ...