当前位置: 首页>>代码示例>>Java>>正文


Java ReplicationConnection.setReadOnly方法代码示例

本文整理汇总了Java中com.mysql.jdbc.ReplicationConnection.setReadOnly方法的典型用法代码示例。如果您正苦于以下问题:Java ReplicationConnection.setReadOnly方法的具体用法?Java ReplicationConnection.setReadOnly怎么用?Java ReplicationConnection.setReadOnly使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在com.mysql.jdbc.ReplicationConnection的用法示例。


在下文中一共展示了ReplicationConnection.setReadOnly方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: testReplicationConnectionGroupHostManagement

import com.mysql.jdbc.ReplicationConnection; //导入方法依赖的package包/类
public void testReplicationConnectionGroupHostManagement() throws Exception {
    String replicationGroup1 = "rg1";

    Properties props = new Properties();
    props.setProperty("replicationConnectionGroup", replicationGroup1);
    props.setProperty("retriesAllDown", "3");
    ReplicationConnection conn2 = this.getUnreliableReplicationConnection(new String[] { "first", "second", "third" }, props);
    assertNotNull("Connection should not be null", this.conn);
    conn2.setAutoCommit(false);
    String port = getPort(props, new NonRegisteringDriver());
    String firstHost = "first:" + port;
    String secondHost = "second:" + port;
    String thirdHost = "third:" + port;

    // "first" should be master, "second" and "third" should be slaves.
    assertEquals(1, ReplicationConnectionGroupManager.getConnectionCountWithHostAsMaster(replicationGroup1, firstHost));
    assertEquals(0, ReplicationConnectionGroupManager.getConnectionCountWithHostAsSlave(replicationGroup1, firstHost));

    // remove "third" from slave pool:
    conn2.removeSlave(thirdHost);

    assertEquals(0, ReplicationConnectionGroupManager.getConnectionCountWithHostAsMaster(replicationGroup1, thirdHost));
    assertEquals(0, ReplicationConnectionGroupManager.getConnectionCountWithHostAsSlave(replicationGroup1, thirdHost));

    // add "third" back into slave pool:
    conn2.addSlaveHost(thirdHost);

    assertEquals(0, ReplicationConnectionGroupManager.getConnectionCountWithHostAsMaster(replicationGroup1, thirdHost));
    assertEquals(1, ReplicationConnectionGroupManager.getConnectionCountWithHostAsSlave(replicationGroup1, thirdHost));

    conn2.setReadOnly(false);

    assertEquals(0, ReplicationConnectionGroupManager.getNumberOfMasterPromotion(replicationGroup1));

    // failover to "second" as master
    ReplicationConnectionGroupManager.promoteSlaveToMaster(replicationGroup1, secondHost);
    assertEquals(1, ReplicationConnectionGroupManager.getNumberOfMasterPromotion(replicationGroup1));

    // "first" is still a master:
    assertEquals(1, ReplicationConnectionGroupManager.getConnectionCountWithHostAsMaster(replicationGroup1, firstHost));
    assertEquals(0, ReplicationConnectionGroupManager.getConnectionCountWithHostAsSlave(replicationGroup1, firstHost));
    assertEquals(1, ReplicationConnectionGroupManager.getConnectionCountWithHostAsMaster(replicationGroup1, secondHost));
    assertEquals(0, ReplicationConnectionGroupManager.getConnectionCountWithHostAsSlave(replicationGroup1, secondHost));

    ReplicationConnectionGroupManager.removeMasterHost(replicationGroup1, firstHost);

    conn2.createStatement().execute("SELECT 1");
    assertFalse(conn2.isClosed());

    conn2.commit();

    // validate that queries are successful:
    conn2.createStatement().execute("SELECT 1");
    assertTrue(conn2.isHostMaster(secondHost));

    // master is now offline
    UnreliableSocketFactory.downHost("second");
    try {
        Statement lstmt = conn2.createStatement();
        lstmt.execute("SELECT 1");
        fail("Should fail here due to closed connection");
    } catch (SQLException sqlEx) {
        assertEquals("08S01", sqlEx.getSQLState());
    }

}
 
开发者ID:bragex,项目名称:the-vigilantes,代码行数:67,代码来源:ConnectionRegressionTest.java

示例2: testReplicationConnectionMemory

import com.mysql.jdbc.ReplicationConnection; //导入方法依赖的package包/类
public void testReplicationConnectionMemory() throws Exception {
    Properties props = new Properties();
    props.setProperty("retriesAllDown", "3");
    String replicationGroup = "memoryGroup";
    props.setProperty("replicationConnectionGroup", replicationGroup);

    Set<MockConnectionConfiguration> configs = new HashSet<MockConnectionConfiguration>();
    MockConnectionConfiguration first = new MockConnectionConfiguration("first", "slave", null, false);
    MockConnectionConfiguration second = new MockConnectionConfiguration("second", "master", null, false);
    MockConnectionConfiguration third = new MockConnectionConfiguration("third", "slave", null, false);

    configs.add(first);
    configs.add(second);
    configs.add(third);

    ReplicationConnection conn2 = this.getUnreliableReplicationConnection(configs, props);

    ReplicationConnectionGroupManager.promoteSlaveToMaster(replicationGroup, first.getAddress());
    ReplicationConnectionGroupManager.removeMasterHost(replicationGroup, second.getAddress());
    ReplicationConnectionGroupManager.addSlaveHost(replicationGroup, second.getAddress());

    conn2.setReadOnly(false);

    assertFalse(conn2.isReadOnly());
    assertTrue(conn2.isMasterConnection());
    assertTrue(conn2.isHostMaster(first.getAddress()));
    assertTrue(conn2.isHostSlave(second.getAddress()));
    assertTrue(conn2.isHostSlave(third.getAddress()));

    // make sure state changes made are reflected in new connections:

    ReplicationConnection conn3 = this.getUnreliableReplicationConnection(configs, props);

    conn3.setReadOnly(false);

    assertFalse(conn3.isReadOnly());
    assertTrue(conn3.isMasterConnection());
    assertTrue(conn3.isHostMaster(first.getAddress()));
    assertTrue(conn3.isHostSlave(second.getAddress()));
    assertTrue(conn3.isHostSlave(third.getAddress()));

}
 
开发者ID:bragex,项目名称:the-vigilantes,代码行数:43,代码来源:ConnectionRegressionTest.java

示例3: testBug56100

import com.mysql.jdbc.ReplicationConnection; //导入方法依赖的package包/类
/**
 * Tests fix for Bug#56100 - Replication driver routes DML statements to read-only slaves.
 */
public void testBug56100() throws Exception {
    final String port = getPort(null, new NonRegisteringDriver());
    final String hostMaster = "master:" + port;
    final String hostSlave = "slave:" + port;

    final Properties props = new Properties();
    props.setProperty("statementInterceptors", Bug56100StatementInterceptor.class.getName());

    final ReplicationConnection testConn = getUnreliableReplicationConnection(new String[] { "master", "slave" }, props);

    assertTrue(testConn.isHostMaster(hostMaster));
    assertTrue(testConn.isHostSlave(hostSlave));

    // verify that current connection is 'master'
    assertTrue(testConn.isMasterConnection());

    final Statement testStmt1 = testConn.createStatement();
    testBug56100AssertHost(testStmt1, "master");

    // set connection to read-only state and verify that current connection is 'slave' now
    testConn.setReadOnly(true);
    assertFalse(testConn.isMasterConnection());

    final Statement testStmt2 = testConn.createStatement();
    testBug56100AssertHost(testStmt1, "slave");
    testBug56100AssertHost(testStmt2, "slave");

    // set connection to read/write state and verify that current connection is 'master' again
    testConn.setReadOnly(false);
    assertTrue(testConn.isMasterConnection());

    final Statement testStmt3 = testConn.createStatement();
    testBug56100AssertHost(testStmt1, "master");
    testBug56100AssertHost(testStmt2, "master");
    testBug56100AssertHost(testStmt3, "master");

    // let Connection.close() also close open statements
    testConn.close();

    assertThrows(SQLException.class, "No operations allowed after statement closed.", new Callable<Void>() {
        public Void call() throws Exception {
            testStmt1.execute("SELECT 'Bug56100'");
            return null;
        }
    });

    assertThrows(SQLException.class, "No operations allowed after statement closed.", new Callable<Void>() {
        public Void call() throws Exception {
            testStmt2.execute("SELECT 'Bug56100'");
            return null;
        }
    });

    assertThrows(SQLException.class, "No operations allowed after statement closed.", new Callable<Void>() {
        public Void call() throws Exception {
            testStmt3.execute("SELECT 'Bug56100'");
            return null;
        }
    });
}
 
开发者ID:bragex,项目名称:the-vigilantes,代码行数:64,代码来源:ConnectionRegressionTest.java

示例4: setReadOnly

import com.mysql.jdbc.ReplicationConnection; //导入方法依赖的package包/类
public void setReadOnly(boolean readOnly) throws SQLException {
    this.readOnly = readOnly;
    for (ReplicationConnection conn : this.serverConnections.values()) {
        conn.setReadOnly(readOnly);
    }
}
 
开发者ID:bragex,项目名称:the-vigilantes,代码行数:7,代码来源:FabricMySQLConnectionProxy.java

示例5: testReplicationConnectionMemory

import com.mysql.jdbc.ReplicationConnection; //导入方法依赖的package包/类
public void testReplicationConnectionMemory() throws Exception {
	Properties props = new Properties();
	props.setProperty("retriesAllDown", "3");
	String replicationGroup = "memoryGroup";
	props.setProperty("replicationConnectionGroup", replicationGroup);

	Set<MockConnectionConfiguration> configs = new HashSet<MockConnectionConfiguration>();
	MockConnectionConfiguration first = new MockConnectionConfiguration("first", "slave", null, false);
	MockConnectionConfiguration second = new MockConnectionConfiguration("second", "master", null, false);
	MockConnectionConfiguration third = new MockConnectionConfiguration("third", "slave", null, false);

	configs.add(first);
	configs.add(second);
	configs.add(third);

	ReplicationConnection conn2 = this
			.getUnreliableReplicationConnection(configs, props);


	ReplicationConnectionGroupManager.promoteSlaveToMaster(replicationGroup, first.getAddress());
	ReplicationConnectionGroupManager.removeMasterHost(replicationGroup, second.getAddress());
	ReplicationConnectionGroupManager.addSlaveHost(replicationGroup, second.getAddress());

	conn2.setReadOnly(false);

	assertFalse(conn2.isReadOnly());
	assertTrue(conn2.isMasterConnection());
	assertTrue(conn2.isHostMaster(first.getAddress()));
	assertTrue(conn2.isHostSlave(second.getAddress()));
	assertTrue(conn2.isHostSlave(third.getAddress()));


	// make sure state changes made are reflected in new connections:

	ReplicationConnection conn3 = this
			.getUnreliableReplicationConnection(configs, props);


	conn3.setReadOnly(false);

	assertFalse(conn3.isReadOnly());
	assertTrue(conn3.isMasterConnection());
	assertTrue(conn3.isHostMaster(first.getAddress()));
	assertTrue(conn3.isHostSlave(second.getAddress()));
	assertTrue(conn3.isHostSlave(third.getAddress()));


}
 
开发者ID:hinsenchan,项目名称:fil_project_mgmt_app_v2,代码行数:49,代码来源:ConnectionRegressionTest.java

示例6: setReadOnly

import com.mysql.jdbc.ReplicationConnection; //导入方法依赖的package包/类
public void setReadOnly(boolean readOnly) throws SQLException {
	this.readOnly = readOnly;
	for (ReplicationConnection conn : serverConnections.values()) {
		conn.setReadOnly(readOnly);
	}
}
 
开发者ID:hinsenchan,项目名称:fil_project_mgmt_app_v2,代码行数:7,代码来源:FabricMySQLConnectionProxy.java


注:本文中的com.mysql.jdbc.ReplicationConnection.setReadOnly方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。