国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數據庫 > MySQL > 正文

MySQL 讀寫分離實例詳解

2024-07-24 13:11:16
字體:
來源:轉載
供稿:網友

MySQL 讀寫分離

MySQL讀寫分離又一好辦法 使用 com.mysql.jdbc.ReplicationDriver

在用過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");    ....... }}

感謝閱讀,希望能幫助到大家,謝謝大對本站的支持!


注:相關教程知識閱讀請移步到MYSQL教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 乐陵市| 开阳县| 枣阳市| 昌平区| 连江县| 江阴市| 广安市| 南昌市| 集贤县| 堆龙德庆县| 沿河| 尼玛县| 安国市| 澄迈县| 芜湖市| 皮山县| 洪江市| 柳林县| 丰原市| 延寿县| 佛山市| 抚远县| 台北市| 武山县| 南京市| 万州区| 富民县| 苍山县| 松阳县| 策勒县| 二连浩特市| 务川| 和龙市| 日照市| 县级市| 白河县| 定边县| 东阳市| 综艺| 息烽县| 平湖市|