360教程:是一个免费提供流行杀毒软件教程、在线学习分享的学习平台!

分享一个MySQL ReplicationDriver类代码

时间:2022-10-18作者:未知来源:360教程人气:

SQL是Structured Query Language(结构化查询语言)的缩写。SQL是专为数据库而建立的操作命令集,是一种功能齐全的数据库语言。在使用它时,只需要发出“做什么”的命令,“怎么做”是不用使用者考虑的。SQL功能强大、简单易学、使用方便,已经成为了数据库操作的基础,并且现在几乎所有的数据库均支持SQL。

在 MySQL 复制的环境中,要通过 JDBC 连接这个 MySQL 集群,就必须使用 ReplicationDriver 这个类来替换原有的 com.mysql.jdbc.Driver 。不过该 Driver 在连接池环境下无效,要通过连接池连接 MySQL 集群可使用 lbpool。

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://master,slave1,slave2,slave3/test",
            props);
    //
    // Perform read/write work on the master
    // by setting the read-only flag to "false"
    //
  //这个节点应该是通过spring的事务管理来设置,同时这个conn对象应该不是一个真正的connection,
	    //而是一个代理类,通过设置readonly,代理类会去使用不同的connection,
	    //那么问题是它该代理类使用的connection是哪里取的,抑或说难道它每次都会新开一个connection?,需要看源代码
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 ReplicationDriver类代码的详细内容,更多请关注php中文网其它相关文章!


学习教程快速掌握从入门到精通的SQL知识。



关键词:分享一个MySQL ReplicationDriver类代码




Copyright © 2012-2018 360教程(http://www.360sd.cn) .All Rights Reserved 网站地图 友情链接

免责声明:本站资源均来自互联网收集 如有侵犯到您利益的地方请及时联系管理删除,敬请见谅!

QQ:1006262270   邮箱:kfyvi376850063@126.com   手机版