public void testFailoverQueriesBeforeRetryMaster() throws Exception {
  Properties props=new Properties();
  props.setProperty("retriesAllDown","2");
  for (  boolean setQueriesBeforeRetryMaster : new boolean[]{true,false}) {
    if (setQueriesBeforeRetryMaster) {
      props.setProperty("queriesBeforeRetryMaster","10");
    }
 else {
      props.remove("queriesBeforeRetryMaster");
    }
    Connection testConn=getUnreliableFailoverConnection(new String[]{HOST_1,HOST_2,HOST_3},props);
    Statement testStmt=null;
    try {
      testConn.setAutoCommit(false);
      assertEquals(HOST_1_OK,UnreliableSocketFactory.getHostFromLastConnection());
      testStmt=testConn.createStatement();
      assertSingleValueQuery(testStmt,"SELECT 1",1L);
      UnreliableSocketFactory.downHost(HOST_1);
      UnreliableSocketFactory.downHost(HOST_2);
      assertEquals(HOST_1_OK,UnreliableSocketFactory.getHostFromLastConnection());
      assertSQLException(testStmt,"SELECT 1",COMM_LINK_ERR_PATTERN);
      assertEquals(HOST_3_OK,UnreliableSocketFactory.getHostFromLastConnection());
      testStmt=testConn.createStatement();
      for (int i=0; i < 10; i++) {
        assertSingleValueQuery(testStmt,"SELECT 1",1L);
        assertEquals(HOST_3_OK,UnreliableSocketFactory.getHostFromLastConnection());
      }
      UnreliableSocketFactory.dontDownHost(HOST_1);
      UnreliableSocketFactory.dontDownHost(HOST_2);
      UnreliableSocketFactory.downHost(HOST_3);
      assertEquals(HOST_3_OK,UnreliableSocketFactory.getHostFromLastConnection());
      if (setQueriesBeforeRetryMaster) {
        assertSQLException(testStmt,"SELECT 1",COMM_LINK_ERR_PATTERN);
        assertEquals(HOST_1_OK,UnreliableSocketFactory.getHostFromLastConnection());
        assertConnectionsHistory(HOST_1_OK,HOST_2_FAIL,HOST_3_OK,HOST_1_OK);
      }
 else {
        assertSQLException(testStmt,"SELECT 1",COMM_LINK_ERR_PATTERN);
        assertEquals(HOST_2_OK,UnreliableSocketFactory.getHostFromLastConnection());
        assertConnectionsHistory(HOST_1_OK,HOST_2_FAIL,HOST_3_OK,HOST_2_OK);
      }
    }
  finally {
      if (testStmt != null) {
        testStmt.close();
      }
      if (testConn != null) {
        testConn.close();
      }
    }
  }
}

public void testFailoverQueriesBeforeRetryMaster ( ) throws Exception { Properties props = new Properties ( ) ; props . setProperty ( "retriesAllDown" , "2" ) ; for ( boolean setQueriesBeforeRetryMaster : new boolean [ ] { true , false } ) { if ( setQueriesBeforeRetryMaster ) { props . setProperty ( "queriesBeforeRetryMaster" , "10" ) ; } else { props . remove ( "queriesBeforeRetryMaster" ) ; } Connection testConn = getUnreliableFailoverConnection ( new String [ ] { HOST_1 , HOST_2 , HOST_3 } , props ) ; Statement testStmt = null ; try { testConn . setAutoCommit ( false ) ; assertEquals ( HOST_1_OK , UnreliableSocketFactory . getHostFromLastConnection ( ) ) ; testStmt = testConn . createStatement ( ) ; assertSingleValueQuery ( testStmt , "SELECT 1" , 1L ) ; UnreliableSocketFactory . downHost ( HOST_1 ) ; UnreliableSocketFactory . downHost ( HOST_2 ) ; assertEquals ( HOST_1_OK , UnreliableSocketFactory . getHostFromLastConnection ( ) ) ; assertSQLException ( testStmt , "SELECT 1" , COMM_LINK_ERR_PATTERN ) ; assertEquals ( HOST_3_OK , UnreliableSocketFactory . getHostFromLastConnection ( ) ) ; testStmt = testConn . createStatement ( ) ; for ( int i = 0 ; i < 10 ; i ++ ) { assertSingleValueQuery ( testStmt , "SELECT 1" , 1L ) ; assertEquals ( HOST_3_OK , UnreliableSocketFactory . getHostFromLastConnection ( ) ) ; } UnreliableSocketFactory . dontDownHost ( HOST_1 ) ; UnreliableSocketFactory . dontDownHost ( HOST_2 ) ; UnreliableSocketFactory . downHost ( HOST_3 ) ; assertEquals ( HOST_3_OK , UnreliableSocketFactory . getHostFromLastConnection ( ) ) ; if ( setQueriesBeforeRetryMaster ) { assertSQLException ( testStmt , "SELECT 1" , COMM_LINK_ERR_PATTERN ) ; assertEquals ( HOST_1_OK , UnreliableSocketFactory . getHostFromLastConnection ( ) ) ; assertConnectionsHistory ( HOST_1_OK , HOST_2_FAIL , HOST_3_OK , HOST_1_OK ) ; } else { assertSQLException ( testStmt , "SELECT 1" , COMM_LINK_ERR_PATTERN ) ; assertEquals ( HOST_2_OK , UnreliableSocketFactory . getHostFromLastConnection ( ) ) ; assertConnectionsHistory ( HOST_1_OK , HOST_2_FAIL , HOST_3_OK , HOST_2_OK ) ; } } finally { if ( testStmt != null ) { testStmt . close ( ) ; } if ( testConn != null ) { testConn . close ( ) ; } } } }
tests the property ' queriesbeforeretrymaster ' in a failover connection using three hosts and the following sequence of events : - [ / host_1 : / host_2 : / host_3 ] - - > host_1 - [ \ host_1 : \ host_2 : / host_3 ] - - > host_3 - [ / host_1 : / host_2 : \ host_3 ] - - > host_1 vs host_2 [ legend : " / host_n " - - > host_n up ; " \ host_n " - - > host_n down ]
