日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

MySQL 读写分离 使用驱动com.mysql.jdbc.ReplicationDriver

發布時間:2023/11/29 数据库 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 MySQL 读写分离 使用驱动com.mysql.jdbc.ReplicationDriver 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

說明文檔:http://dev.mysql.com/doc/refman/5.1/en/connector-j-reference-replication-connection.html

?

代碼例子:

1 import java.sql.Connection; 2 import java.sql.ResultSet; 3 import java.util.Properties; 4 5 import com.mysql.jdbc.ReplicationDriver; 6 7 public class ReplicationDriverDemo { 8 9 public static void main(String[] args) throws Exception { 10 ReplicationDriver driver = new ReplicationDriver(); 11 12 Properties props = new Properties(); 13 14 // We want this for failover on the slaves 15 props.put("autoReconnect", "true"); 16 17 // We want to load balance between the slaves 18 props.put("roundRobinLoadBalance", "true"); 19 20 props.put("user", "foo"); 21 props.put("password", "bar"); 22 23 // 24 // Looks like a normal MySQL JDBC url, with a 25 // comma-separated list of hosts, the first 26 // being the 'master', the rest being any number 27 // of slaves that the driver will load balance against 28 // 29 30 Connection conn = 31 driver.connect("jdbc:mysql:replication://master,slave1,slave2,slave3/test", 32 props); 33 34 // 35 // Perform read/write work on the master 36 // by setting the read-only flag to "false" 37 // 38 39 conn.setReadOnly(false); 40 conn.setAutoCommit(false); 41 conn.createStatement().executeUpdate("UPDATE some_table ...."); 42 conn.commit(); 43 44 // 45 // Now, do a query from a slave, the driver automatically picks one 46 // from the list 47 // 48 49 conn.setReadOnly(true); 50 51 ResultSet rs = 52 conn.createStatement().executeQuery("SELECT a,b FROM alt_table"); 53 54 ....... 55 } 56 }

?

來自:http://www.cnblogs.com/taven/archive/2013/04/24/3040489.html

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的MySQL 读写分离 使用驱动com.mysql.jdbc.ReplicationDriver的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。