最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

MySQL 讀寫(xiě)分離實(shí)例詳解

 更新時(shí)間:2016年11月23日 09:41:47   作者:Taven  
這篇文章主要介紹了MySQL 讀寫(xiě)分離實(shí)例詳解的相關(guān)資料,這里對(duì)讀寫(xiě)MySQL分離進(jìn)行了簡(jiǎn)單介紹,并附實(shí)例代碼,需要的朋友可以參考下

MySQL 讀寫(xiě)分離

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

在用過(guò)Amoeba 和 Cobar,還有dbware 等讀寫(xiě)分離組件后,今天我的一個(gè)好朋友跟我講,MySQL自身的也是可以讀寫(xiě)分離的,因?yàn)樗麄兲峁┝艘粋€(gè)新的驅(qū)動(dòng),叫 com.mysql.jdbc.ReplicationDriver

說(shuō)明文檔: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");
 
   .......
 }
}

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

相關(guān)文章

最新評(píng)論

城固县| 孟村| 余江县| 富蕴县| 桑日县| 保山市| 通海县| 三亚市| 丘北县| 桂东县| 富阳市| 江城| 儋州市| 安远县| 衡水市| 九龙县| 龙州县| 固阳县| 皋兰县| 吉安县| 伊金霍洛旗| 平乐县| 苏尼特右旗| 本溪市| 桑植县| 汉沽区| 济南市| 会理县| 扬中市| 疏附县| 大庆市| 龙州县| 紫云| 兴国县| 宣汉县| 滦南县| 上蔡县| 宜良县| 新绛县| 贵溪市| 遂平县|