public void testFailoverCombinations() throws Exception {
  Properties props=new Properties();
  props.setProperty("retriesAllDown","2");
  for (int run=1; run <= 3; run++) {
    Connection testConn=getUnreliableFailoverConnection(new String[]{HOST_1,HOST_2,HOST_3},props);
    Statement testStmt1=null, testStmt2=null;
    testConn.setAutoCommit(false);
    try {
      assertEquals(HOST_1_OK,UnreliableSocketFactory.getHostFromLastConnection());
      testStmt1=testConn.createStatement();
      testStmt2=testConn.createStatement();
      assertSingleValueQuery(testStmt1,"SELECT 1",1L);
      assertSingleValueQuery(testStmt2,"SELECT 2",2L);
      UnreliableSocketFactory.downHost(HOST_1);
      assertEquals(HOST_1_OK,UnreliableSocketFactory.getHostFromLastConnection());
      if (run == 1) {
        assertSQLException(testStmt1,"SELECT 1",COMM_LINK_ERR_PATTERN);
      }
 else       if (run == 2) {
        assertSQLException(testConn,"commit",COMM_LINK_ERR_PATTERN);
      }
 else {
        assertSQLException(testConn,"rollback",COMM_LINK_ERR_PATTERN);
      }
      assertEquals(HOST_2_OK,UnreliableSocketFactory.getHostFromLastConnection());
      assertSQLException(testStmt1,"SELECT 1",STMT_CLOSED_ERR_PATTERN);
      assertSQLException(testStmt2,"SELECT 2",STMT_CLOSED_ERR_PATTERN);
      testStmt1=testConn.createStatement();
      testStmt2=testConn.createStatement();
      assertSingleValueQuery(testStmt1,"SELECT 1",1L);
      assertSingleValueQuery(testStmt2,"SELECT 2",2L);
      UnreliableSocketFactory.downHost(HOST_2);
      assertEquals(HOST_2_OK,UnreliableSocketFactory.getHostFromLastConnection());
      if (run == 1) {
        assertSQLException(testConn,"commit",COMM_LINK_ERR_PATTERN);
      }
 else       if (run == 2) {
        assertSQLException(testStmt1,"SELECT 1",COMM_LINK_ERR_PATTERN);
      }
 else {
        assertSQLException(testConn,"rollback",COMM_LINK_ERR_PATTERN);
      }
      assertEquals(HOST_3_OK,UnreliableSocketFactory.getHostFromLastConnection());
      assertSQLException(testStmt1,"SELECT 1",STMT_CLOSED_ERR_PATTERN);
      assertSQLException(testStmt2,"SELECT 2",STMT_CLOSED_ERR_PATTERN);
      UnreliableSocketFactory.dontDownHost(HOST_1);
      UnreliableSocketFactory.dontDownHost(HOST_2);
      testStmt1=testConn.createStatement();
      testStmt2=testConn.createStatement();
      assertSingleValueQuery(testStmt1,"SELECT 1",1L);
      assertSingleValueQuery(testStmt2,"SELECT 2",2L);
      UnreliableSocketFactory.downHost(HOST_3);
      assertEquals(HOST_3_OK,UnreliableSocketFactory.getHostFromLastConnection());
      if (run == 1) {
        assertSQLException(testConn,"rollback",COMM_LINK_ERR_PATTERN);
      }
 else       if (run == 2) {
        assertSQLException(testConn,"commit",COMM_LINK_ERR_PATTERN);
      }
 else {
        assertSQLException(testStmt2,"SELECT 2",COMM_LINK_ERR_PATTERN);
      }
      assertEquals(HOST_2_OK,UnreliableSocketFactory.getHostFromLastConnection());
      assertSQLException(testStmt1,"SELECT 1",STMT_CLOSED_ERR_PATTERN);
      assertSQLException(testStmt2,"SELECT 2",STMT_CLOSED_ERR_PATTERN);
      testStmt1=testConn.createStatement();
      testStmt2=testConn.createStatement();
      assertSingleValueQuery(testStmt1,"SELECT 1",1L);
      assertSingleValueQuery(testStmt2,"SELECT 2",2L);
      UnreliableSocketFactory.downHost(HOST_2);
      assertEquals(HOST_2_OK,UnreliableSocketFactory.getHostFromLastConnection());
      if (run == 1) {
        assertSQLException(testStmt1,"SELECT 1",COMM_LINK_ERR_PATTERN);
      }
 else       if (run == 2) {
        assertSQLException(testConn,"rollback",COMM_LINK_ERR_PATTERN);
      }
 else {
        assertSQLException(testConn,"commit",COMM_LINK_ERR_PATTERN);
      }
      assertEquals(HOST_1_OK,UnreliableSocketFactory.getHostFromLastConnection());
      assertSQLException(testStmt1,"SELECT 1",STMT_CLOSED_ERR_PATTERN);
      assertSQLException(testStmt2,"SELECT 2",STMT_CLOSED_ERR_PATTERN);
      testStmt1=testConn.createStatement();
      testStmt2=testConn.createStatement();
      assertSingleValueQuery(testStmt1,"SELECT 1",1L);
      assertSingleValueQuery(testStmt2,"SELECT 2",2L);
      assertConnectionsHistory(HOST_1_OK,HOST_2_OK,HOST_3_OK,HOST_2_OK,HOST_3_FAIL,HOST_2_FAIL,HOST_3_FAIL,HOST_1_OK);
    }
  finally {
      if (testStmt1 != null) {
        testStmt1.close();
      }
      if (testStmt2 != null) {
        testStmt2.close();
      }
      testConn.close();
    }
  }
}

public void testFailoverCombinations ( ) throws Exception { Properties props = new Properties ( ) ; props . setProperty ( "retriesAllDown" , "2" ) ; for ( int run = 1 ; run <= 3 ; run ++ ) { Connection testConn = getUnreliableFailoverConnection ( new String [ ] { HOST_1 , HOST_2 , HOST_3 } , props ) ; Statement testStmt1 = null , testStmt2 = null ; testConn . setAutoCommit ( false ) ; try { assertEquals ( HOST_1_OK , UnreliableSocketFactory . getHostFromLastConnection ( ) ) ; testStmt1 = testConn . createStatement ( ) ; testStmt2 = testConn . createStatement ( ) ; assertSingleValueQuery ( testStmt1 , "SELECT 1" , 1L ) ; assertSingleValueQuery ( testStmt2 , "SELECT 2" , 2L ) ; UnreliableSocketFactory . downHost ( HOST_1 ) ; assertEquals ( HOST_1_OK , UnreliableSocketFactory . getHostFromLastConnection ( ) ) ; if ( run == 1 ) { assertSQLException ( testStmt1 , "SELECT 1" , COMM_LINK_ERR_PATTERN ) ; } else if ( run == 2 ) { assertSQLException ( testConn , "commit" , COMM_LINK_ERR_PATTERN ) ; } else { assertSQLException ( testConn , "rollback" , COMM_LINK_ERR_PATTERN ) ; } assertEquals ( HOST_2_OK , UnreliableSocketFactory . getHostFromLastConnection ( ) ) ; assertSQLException ( testStmt1 , "SELECT 1" , STMT_CLOSED_ERR_PATTERN ) ; assertSQLException ( testStmt2 , "SELECT 2" , STMT_CLOSED_ERR_PATTERN ) ; testStmt1 = testConn . createStatement ( ) ; testStmt2 = testConn . createStatement ( ) ; assertSingleValueQuery ( testStmt1 , "SELECT 1" , 1L ) ; assertSingleValueQuery ( testStmt2 , "SELECT 2" , 2L ) ; UnreliableSocketFactory . downHost ( HOST_2 ) ; assertEquals ( HOST_2_OK , UnreliableSocketFactory . getHostFromLastConnection ( ) ) ; if ( run == 1 ) { assertSQLException ( testConn , "commit" , COMM_LINK_ERR_PATTERN ) ; } else if ( run == 2 ) { assertSQLException ( testStmt1 , "SELECT 1" , COMM_LINK_ERR_PATTERN ) ; } else { assertSQLException ( testConn , "rollback" , COMM_LINK_ERR_PATTERN ) ; } assertEquals ( HOST_3_OK , UnreliableSocketFactory . getHostFromLastConnection ( ) ) ; assertSQLException ( testStmt1 , "SELECT 1" , STMT_CLOSED_ERR_PATTERN ) ; assertSQLException ( testStmt2 , "SELECT 2" , STMT_CLOSED_ERR_PATTERN ) ; UnreliableSocketFactory . dontDownHost ( HOST_1 ) ; UnreliableSocketFactory . dontDownHost ( HOST_2 ) ; testStmt1 = testConn . createStatement ( ) ; testStmt2 = testConn . createStatement ( ) ; assertSingleValueQuery ( testStmt1 , "SELECT 1" , 1L ) ; assertSingleValueQuery ( testStmt2 , "SELECT 2" , 2L ) ; UnreliableSocketFactory . downHost ( HOST_3 ) ; assertEquals ( HOST_3_OK , UnreliableSocketFactory . getHostFromLastConnection ( ) ) ; if ( run == 1 ) { assertSQLException ( testConn , "rollback" , COMM_LINK_ERR_PATTERN ) ; } else if ( run == 2 ) { assertSQLException ( testConn , "commit" , COMM_LINK_ERR_PATTERN ) ; } else { assertSQLException ( testStmt2 , "SELECT 2" , COMM_LINK_ERR_PATTERN ) ; } assertEquals ( HOST_2_OK , UnreliableSocketFactory . getHostFromLastConnection ( ) ) ; assertSQLException ( testStmt1 , "SELECT 1" , STMT_CLOSED_ERR_PATTERN ) ; assertSQLException ( testStmt2 , "SELECT 2" , STMT_CLOSED_ERR_PATTERN ) ; testStmt1 = testConn . createStatement ( ) ; testStmt2 = testConn . createStatement ( ) ; assertSingleValueQuery ( testStmt1 , "SELECT 1" , 1L ) ; assertSingleValueQuery ( testStmt2 , "SELECT 2" , 2L ) ; UnreliableSocketFactory . downHost ( HOST_2 ) ; assertEquals ( HOST_2_OK , UnreliableSocketFactory . getHostFromLastConnection ( ) ) ; if ( run == 1 ) { assertSQLException ( testStmt1 , "SELECT 1" , COMM_LINK_ERR_PATTERN ) ; } else if ( run == 2 ) { assertSQLException ( testConn , "rollback" , COMM_LINK_ERR_PATTERN ) ; } else { assertSQLException ( testConn , "commit" , COMM_LINK_ERR_PATTERN ) ; } assertEquals ( HOST_1_OK , UnreliableSocketFactory . getHostFromLastConnection ( ) ) ; assertSQLException ( testStmt1 , "SELECT 1" , STMT_CLOSED_ERR_PATTERN ) ; assertSQLException ( testStmt2 , "SELECT 2" , STMT_CLOSED_ERR_PATTERN ) ; testStmt1 = testConn . createStatement ( ) ; testStmt2 = testConn . createStatement ( ) ; assertSingleValueQuery ( testStmt1 , "SELECT 1" , 1L ) ; assertSingleValueQuery ( testStmt2 , "SELECT 2" , 2L ) ; assertConnectionsHistory ( HOST_1_OK , HOST_2_OK , HOST_3_OK , HOST_2_OK , HOST_3_FAIL , HOST_2_FAIL , HOST_3_FAIL , HOST_1_OK ) ; } finally { if ( testStmt1 != null ) { testStmt1 . close ( ) ; } if ( testStmt2 != null ) { testStmt2 . close ( ) ; } testConn . close ( ) ; } } }
repeatedly tests a failover connection using three hosts and the following sequence of events , combining distinct failover event triggering : - [ / host_1 : / host_2 : / host_3 ] - - > host_1 - [ \ host_1 : / host_2 : / host_3 ] - - > host_2 - [ \ host_1 : \ host_2 : / host_3 ] - - > host_3 - [ / host_1 : / host_2 : / host_3 ] - [ / host_1 : / host_2 : \ host_3 ] - - > host_2 - [ / host_1 : \ host_2 : \ host_3 ] - - > host_1 [ legend : " / host_n " - - > host_n up ; " \ host_n " - - > host_n down ]
