Package com.mysql.jdbc

Examples of com.mysql.jdbc.ReplicationConnection


    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", 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());
    }
View Full Code Here


  public void testReplicationConnectionHostManagement() throws Exception {
    Properties props = new Properties();
    props.setProperty("retriesAllDown", "3");
   
    ReplicationConnection conn2 = this.getUnreliableReplicationConnection(
        new String[] { "first", "second", "third" }, props);
    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.
    assertTrue(conn2.isHostMaster(firstHost));
    assertTrue(conn2.isHostSlave(secondHost));
    assertTrue(conn2.isHostSlave(thirdHost));
    assertFalse(conn2.isHostSlave(firstHost));
    assertFalse(conn2.isHostMaster(secondHost));
    assertFalse(conn2.isHostMaster(thirdHost));

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

    // add "third" back into slave pool:
    conn2.addSlaveHost(thirdHost);
    assertTrue(conn2.isHostSlave(thirdHost));
    assertFalse(conn2.isHostMaster(thirdHost));
    conn2.setReadOnly(false);

    // failover to "second" as master, "first"
    // can still be used:
    conn2.promoteSlaveToMaster(secondHost);
    assertTrue(conn2.isHostMaster(firstHost));
    assertFalse(conn2.isHostSlave(firstHost));
    assertFalse(conn2.isHostSlave(secondHost));
    assertTrue(conn2.isHostMaster(secondHost));
    assertTrue(conn2.isHostSlave(thirdHost));
    assertFalse(conn2.isHostMaster(thirdHost));
   
    conn2.removeMasterHost(firstHost);

    // "first" should no longer be used:
    conn2.promoteSlaveToMaster(secondHost);
    assertTrue(conn2.isHostMaster(firstHost));
    assertFalse(conn2.isHostSlave(firstHost));
    assertFalse(conn2.isHostSlave(secondHost));
    assertTrue(conn2.isHostMaster(secondHost));
    assertTrue(conn2.isHostSlave(thirdHost));
    assertFalse(conn2.isHostMaster(thirdHost));

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

    // check that we're waiting until transaction
    // boundary to fail over.
//    assertTrue(conn2.hasPendingNewMaster());
    assertFalse(conn2.isClosed());
    conn2.commit();
    assertFalse(conn2.isClosed());
    assertTrue(conn2.isHostMaster(secondHost));
    assertFalse(conn2.isClosed());
    assertTrue(conn2.isMasterConnection());
    assertFalse(conn2.isClosed());

    // 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());
    }

    UnreliableSocketFactory.dontDownHost("second");
    try {
      // won't work now even though master is back up
      // connection has already been implicitly closed
      // when a new master host cannot be found:
      conn2.createStatement().execute("SELECT 1");
      fail("Will fail because inability to find new master host implicitly closes connection.");
    } catch (SQLException e) {
      assertEquals("08003", e.getSQLState());
    }
View Full Code Here

    props.setProperty("allowMasterDownConnections", "true");
   
    Set<String> downedHosts = new HashSet<String>();
    downedHosts.add("first");
   
    ReplicationConnection conn2 = this
        .getUnreliableReplicationConnection(new String[] { "first",
            "second", "third" }, props, downedHosts);
    assertTrue(conn2.isReadOnly());
    assertFalse(conn2.isMasterConnection());
    try {
      conn2.createStatement().execute("SELECT 1");
    } catch (SQLException e) {
      fail("Should not fail to execute SELECT statements!");
    }
    UnreliableSocketFactory.flushAllHostLists();
    conn2.setReadOnly(false);
    assertFalse(conn2.isReadOnly());
    assertTrue(conn2.isMasterConnection());
    try {
      conn2.createStatement().execute("DROP TABLE IF EXISTS testRepTable");
      conn2.createStatement().execute("CREATE TABLE testRepTable (a INT)");
      conn2.createStatement().execute("INSERT INTO testRepTable VALUES (1)");
      conn2.createStatement().execute("DROP TABLE IF EXISTS testRepTable");
     
    } catch (SQLException e) {
      fail("Should not fail to execute CREATE/INSERT/DROP statements.");
    }   
  }
View Full Code Here

   
    configs.add(first);
    configs.add(second);
    configs.add(third);
   
    ReplicationConnection conn2 = this
        .getUnreliableReplicationConnection(configs, props);
    assertFalse(conn2.isReadOnly());
    assertTrue(conn2.isMasterConnection());
    assertTrue(conn2.isHostSlave(first.getAddress()));
    assertTrue(conn2.isHostMaster(second.getAddress()));
    assertTrue(conn2.isHostMaster(third.getAddress()));
 
  }
View Full Code Here

   * @throws Exception
   *             if the test fails.
   */
  public void testBug68763() throws Exception {
   
      ReplicationConnection replConn = null;

      replConn = (ReplicationConnection) getMasterSlaveReplicationConnection();
      replConn.setReadOnly(true);
      assertFalse("isMasterConnection() should be false for slave connection", replConn.isMasterConnection());
      replConn.setReadOnly(false);
      assertTrue("isMasterConnection() should be true for master connection", replConn.isMasterConnection());

  }
View Full Code Here

    ForcedLoadBalanceStrategy.forceFutureServer("slave1:" + portNumber, -1);
    // throw Exception if slave2 gets ping
    UnreliableSocketFactory.downHost("slave2");
   
    ReplicationConnection conn2 = this.getUnreliableReplicationConnection(
        new String[] { "master", "slave1", "slave2" }, props);
    assertTrue("Is not actually on master!", conn2.isMasterConnection());

   
    conn2.setAutoCommit(false);

    conn2.commit();
    // go to slaves:
    conn2.setReadOnly(true);
   
    // should succeed, as slave2 has not yet been activated:
    conn2.createStatement().execute("/* ping */ SELECT 1");
    // allow connections to slave2:
    UnreliableSocketFactory.dontDownHost("slave2");
    // force next re-balance to slave2:
    ForcedLoadBalanceStrategy.forceFutureServer("slave2:" + portNumber, -1);
    // re-balance:
    conn2.commit();
    // down slave1 (active but not selected slave connection):
    UnreliableSocketFactory.downHost("slave1");
    // should succeed, as slave2 is currently selected:
    conn2.createStatement().execute("/* ping */ SELECT 1");
   
   
 
    // make all hosts available
    UnreliableSocketFactory.flushAllHostLists();
   
    // peg connection to slave2:
    ForcedLoadBalanceStrategy.forceFutureServer("slave2:" + portNumber, -1);
    conn2.commit();

    rs = conn2.createStatement().executeQuery("SELECT CONNECTION_ID()");
    rs.next();
    int slave2id = rs.getInt(1);

    // peg connection to slave1 now:
    ForcedLoadBalanceStrategy.forceFutureServer("slave1:" + portNumber, -1);
    conn2.commit();
   
   
    // this is a really hacky way to confirm ping was processed
    // by an inactive load-balanced connection, but we lack COM_PING
    // counters on the server side, and need to create infrastructure
    // to capture what's being sent by the driver separately.
   
    Thread.sleep(2000);
    conn2.createStatement().execute("/* ping */ SELECT 1");
    rs = conn2.createStatement().executeQuery("SELECT time FROM information_schema.processlist WHERE id = " + slave2id);
    rs.next();
    assertTrue("Processlist should be less than 2 seconds due to ping", rs.getInt(1) < 2);
   
    // peg connection to slave2:
    ForcedLoadBalanceStrategy.forceFutureServer("slave2:" + portNumber, -1);
    conn2.commit();
    // leaving connection tied to slave2, bring slave2 down and slave1 up:
    UnreliableSocketFactory.downHost("slave2");
   
    try {
      conn2.createStatement().execute("/* ping */ SELECT 1");
      fail("Expected failure because current slave connection is down.");
    } catch (SQLException e) { }
   
    conn2.close();
   
    ForcedLoadBalanceStrategy.forceFutureServer("slave1:" + portNumber, -1);
    UnreliableSocketFactory.flushAllHostLists();
    conn2 = this.getUnreliableReplicationConnection(
        new String[] { "master", "slave1", "slave2" }, props);
    conn2.setAutoCommit(false);
    // go to slaves:
    conn2.setReadOnly(true);

    // on slave1 now:
    conn2.commit();
   
    ForcedLoadBalanceStrategy.forceFutureServer("slave2:" + portNumber, -1);
    // on slave2 now:
    conn2.commit();
   
    // disable master:
    UnreliableSocketFactory.downHost("master");
   
    // ping should succeed, because we're still attached to slaves:
    conn2.createStatement().execute("/* ping */ SELECT 1");
   
    // bring master back up:
    UnreliableSocketFactory.dontDownHost("master");

    // get back to master, confirm it's recovered:
    conn2.commit();
    conn2.createStatement().execute("/* ping */ SELECT 1");
    try{
      conn2.setReadOnly(false);
    } catch (SQLException e) {}
   
    conn2.commit();
   
    // take down both slaves:
    UnreliableSocketFactory.downHost("slave1");
    UnreliableSocketFactory.downHost("slave2");
   
    assertTrue(conn2.isMasterConnection());
    // should succeed, as we're still on master:
    conn2.createStatement().execute("/* ping */ SELECT 1");   
   
    UnreliableSocketFactory.dontDownHost("slave1");
    UnreliableSocketFactory.dontDownHost("slave2");
    UnreliableSocketFactory.downHost("master");
   
    try {
      conn2.createStatement().execute("/* ping */ SELECT 1")
      fail("should have failed because master is offline");
    } catch (SQLException e) {
     
    }
   
    UnreliableSocketFactory.dontDownHost("master");
    conn2.createStatement().execute("SELECT 1")
    // continue on slave2:
    conn2.setReadOnly(true);
   
    // should succeed, as slave2 is up:
    conn2.createStatement().execute("/* ping */ SELECT 1")
   
    UnreliableSocketFactory.downHost("slave2");
   
    try {
      conn2.createStatement().execute("/* ping */ SELECT 1")
      fail("should have failed because slave2 is offline and the active chosen connection.");
    } catch (SQLException e) {}
     
   
    conn2.close();
  }
View Full Code Here

   * @throws Exception
   *             if the test fails.
   */
  public void testBug68763() throws Exception {
   
      ReplicationConnection replConn = null;

      replConn = (ReplicationConnection) getMasterSlaveReplicationConnection();
      replConn.setReadOnly(true);
      assertFalse("isMasterConnection() should be false for slave connection", replConn.isMasterConnection());
      replConn.setReadOnly(false);
      assertTrue("isMasterConnection() should be true for master connection", replConn.isMasterConnection());

  }
View Full Code Here

   * @throws Exception
   *             if the test fails.
   */
  public void testBug68763() throws Exception {
   
      ReplicationConnection replConn = null;

      replConn = (ReplicationConnection) getMasterSlaveReplicationConnection();
      replConn.setReadOnly(true);
      assertFalse("isMasterConnection() should be false for slave connection", replConn.isMasterConnection());
      replConn.setReadOnly(false);
      assertTrue("isMasterConnection() should be true for master connection", replConn.isMasterConnection());

  }
View Full Code Here

TOP

Related Classes of com.mysql.jdbc.ReplicationConnection

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.