Heim  >  Artikel  >  Datenbank  >  Teilen Sie einen MySQL ReplicationDriver-Klassencode

Teilen Sie einen MySQL ReplicationDriver-Klassencode

零下一度
零下一度Original
2017-04-25 17:45:551140Durchsuche

Um in einer MySQL-Replikationsumgebung eine Verbindung zum MySQL-Cluster über JDBC herzustellen, müssen Sie die ReplicationDriver-Klasse verwenden, um die zu ersetzen Original Es gibt com.mysql.jdbc.Driver. Dieser Treiber ist jedoch in der Verbindungspoolumgebung nicht gültig, um über den Verbindungspool eine Verbindung zum MySQL-Cluster herzustellen.

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

Das obige ist der detaillierte Inhalt vonTeilen Sie einen MySQL ReplicationDriver-Klassencode. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn