本文整理汇总了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());
}
}
示例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()));
}
示例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;
}
});
}
示例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);
}
}
示例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()));
}
示例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);
}
}