MySQL读写分离又一好办法 使用 com.mysql.jdbc.ReplicationDriver
2015-10-08 09:35
513 查看
在用过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");
.......
}
} 原文出自:http://www.cnblogs.com/taven/archive/2013/04/24/3040489.html 阅读更多
相关文章推荐
- MySQL读写分离又一好办法 使用 com.mysql.jdbc.ReplicationDriver
- MySQL 读写分离 使用驱动com.mysql.jdbc.ReplicationDriver
- 使用tomcat产生java.lang.ClassNotFoundException: com.mysql.jdbc.Driver问题
- 关于No suitable driver found for com.mysql.jdbc.Driver问题的解决办法
- 关于在windows环境下启动java工程提示类找不到,即com.mysql.jdbc.Driver的解决办法
- com.mysql.jdbc.PacketTooBigException: Packet for query is too large 异常解决办法
- 使用tomcat产生的java.lang.ClassNotFoundException: com.mysql.jdbc.Driver问题
- com.mysql.jdbc.SingleByteCharsetConverter错误解决办法
- com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure 解决办法
- Web--项目报com.mysql.jdbc.Driver错误解决办法
- 在Ubuntu/Linux环境下使用MySQL:解决com.mysql.jdbc.PacketTooBigException: Packet for query is too large的问题
- Intellij IDEA出现java.lang.ClassNotFoundException: com.mysql.jdbc.Driver处理办法
- 使用Eclipse运行Java代码调用JDBC读写MySQL中文变成问号的终极解决办法
- 使用powerdesigner连接数据库时出现Non SQL Error : Could not load class com.mysql.jdbc.Drive
- 关于java.lang.ClassNotFoundException: com.mysql.jdbc.Driver的解决办法
- 使用powerdesigner连接数据库时出现Non SQL Error : Could not load class com.mysql.jdbc.Drive
- java.lang.ClassNotFoundException: com.mysql.jdbc.Driver解决办法
- IDEA连接mysql教程,以及出现java.lang.ClassNotFoundException: com.mysql.jdbc.Driver的解决办法
- Java与mysql数据库编程中遇见“Before start of result set at com.mysql.jdbc.SQLError.createSQLException” 的解决办法
- 使用powerdesigner连接数据库时出现Non SQL Error : Could not load class com.mysql.jdbc.Drive