public void testFailoverReadOnly() throws Exception {
  Set<String> downedHosts=new HashSet<String>();
  downedHosts.add(HOST_1);
  Properties props=new Properties();
  props.setProperty("retriesAllDown","2");
  for (  boolean foReadOnly : new boolean[]{true,false}) {
    props.setProperty("failOverReadOnly",Boolean.toString(foReadOnly));
    Connection testConn=getUnreliableFailoverConnection(new String[]{HOST_1,HOST_2,HOST_3},props,downedHosts);
    Statement testStmt=null;
    try {
      assertEquals(HOST_2_OK,UnreliableSocketFactory.getHostFromLastConnection());
      assertEquals(foReadOnly,testConn.isReadOnly());
      testStmt=testConn.createStatement();
      assertSingleValueQuery(testStmt,"SELECT 1",1L);
      UnreliableSocketFactory.downHost(HOST_2);
      assertEquals(HOST_2_OK,UnreliableSocketFactory.getHostFromLastConnection());
      assertSQLException(testStmt,"SELECT 1",COMM_LINK_ERR_PATTERN);
      assertEquals(HOST_3_OK,UnreliableSocketFactory.getHostFromLastConnection());
      assertEquals(foReadOnly,testConn.isReadOnly());
      testStmt=testConn.createStatement();
      assertSingleValueQuery(testStmt,"SELECT 1",1L);
      UnreliableSocketFactory.dontDownHost(HOST_2);
      UnreliableSocketFactory.downHost(HOST_3);
      assertEquals(HOST_3_OK,UnreliableSocketFactory.getHostFromLastConnection());
      assertSQLException(testStmt,"SELECT 1",COMM_LINK_ERR_PATTERN);
      assertEquals(HOST_2_OK,UnreliableSocketFactory.getHostFromLastConnection());
      assertEquals(foReadOnly,testConn.isReadOnly());
      testStmt=testConn.createStatement();
      assertSingleValueQuery(testStmt,"SELECT 1",1L);
      UnreliableSocketFactory.dontDownHost(HOST_1);
      UnreliableSocketFactory.downHost(HOST_2);
      assertEquals(HOST_2_OK,UnreliableSocketFactory.getHostFromLastConnection());
      assertSQLException(testStmt,"SELECT 1",COMM_LINK_ERR_PATTERN);
      assertEquals(HOST_1_OK,UnreliableSocketFactory.getHostFromLastConnection());
      assertFalse(testConn.isReadOnly());
      testStmt=testConn.createStatement();
      assertSingleValueQuery(testStmt,"SELECT 1",1L);
      UnreliableSocketFactory.downHost(HOST_1);
      UnreliableSocketFactory.dontDownHost(HOST_3);
      assertEquals(HOST_1_OK,UnreliableSocketFactory.getHostFromLastConnection());
      assertSQLException(testStmt,"SELECT 1",COMM_LINK_ERR_PATTERN);
      assertEquals(HOST_3_OK,UnreliableSocketFactory.getHostFromLastConnection());
      assertEquals(foReadOnly,testConn.isReadOnly());
      assertConnectionsHistory(HOST_2_OK,HOST_3_OK,HOST_2_OK,HOST_3_FAIL,HOST_2_FAIL,HOST_3_FAIL,HOST_1_OK,HOST_2_FAIL,HOST_3_OK);
    }
  finally {
      if (testStmt != null) {
        testStmt.close();
      }
      if (testConn != null) {
        testConn.close();
      }
    }
  }
}

public void testFailoverReadOnly ( ) throws Exception { Set < String > downedHosts = new HashSet < String > ( ) ; downedHosts . add ( HOST_1 ) ; Properties props = new Properties ( ) ; props . setProperty ( "retriesAllDown" , "2" ) ; for ( boolean foReadOnly : new boolean [ ] { true , false } ) { props . setProperty ( "failOverReadOnly" , Boolean . toString ( foReadOnly ) ) ; Connection testConn = getUnreliableFailoverConnection ( new String [ ] { HOST_1 , HOST_2 , HOST_3 } , props , downedHosts ) ; Statement testStmt = null ; try { assertEquals ( HOST_2_OK , UnreliableSocketFactory . getHostFromLastConnection ( ) ) ; assertEquals ( foReadOnly , testConn . isReadOnly ( ) ) ; testStmt = testConn . createStatement ( ) ; assertSingleValueQuery ( testStmt , "SELECT 1" , 1L ) ; UnreliableSocketFactory . downHost ( HOST_2 ) ; assertEquals ( HOST_2_OK , UnreliableSocketFactory . getHostFromLastConnection ( ) ) ; assertSQLException ( testStmt , "SELECT 1" , COMM_LINK_ERR_PATTERN ) ; assertEquals ( HOST_3_OK , UnreliableSocketFactory . getHostFromLastConnection ( ) ) ; assertEquals ( foReadOnly , testConn . isReadOnly ( ) ) ; testStmt = testConn . createStatement ( ) ; assertSingleValueQuery ( testStmt , "SELECT 1" , 1L ) ; UnreliableSocketFactory . dontDownHost ( HOST_2 ) ; UnreliableSocketFactory . downHost ( HOST_3 ) ; assertEquals ( HOST_3_OK , UnreliableSocketFactory . getHostFromLastConnection ( ) ) ; assertSQLException ( testStmt , "SELECT 1" , COMM_LINK_ERR_PATTERN ) ; assertEquals ( HOST_2_OK , UnreliableSocketFactory . getHostFromLastConnection ( ) ) ; assertEquals ( foReadOnly , testConn . isReadOnly ( ) ) ; testStmt = testConn . createStatement ( ) ; assertSingleValueQuery ( testStmt , "SELECT 1" , 1L ) ; UnreliableSocketFactory . dontDownHost ( HOST_1 ) ; UnreliableSocketFactory . downHost ( HOST_2 ) ; assertEquals ( HOST_2_OK , UnreliableSocketFactory . getHostFromLastConnection ( ) ) ; assertSQLException ( testStmt , "SELECT 1" , COMM_LINK_ERR_PATTERN ) ; assertEquals ( HOST_1_OK , UnreliableSocketFactory . getHostFromLastConnection ( ) ) ; assertFalse ( testConn . isReadOnly ( ) ) ; testStmt = testConn . createStatement ( ) ; assertSingleValueQuery ( testStmt , "SELECT 1" , 1L ) ; UnreliableSocketFactory . downHost ( HOST_1 ) ; UnreliableSocketFactory . dontDownHost ( HOST_3 ) ; assertEquals ( HOST_1_OK , UnreliableSocketFactory . getHostFromLastConnection ( ) ) ; assertSQLException ( testStmt , "SELECT 1" , COMM_LINK_ERR_PATTERN ) ; assertEquals ( HOST_3_OK , UnreliableSocketFactory . getHostFromLastConnection ( ) ) ; assertEquals ( foReadOnly , testConn . isReadOnly ( ) ) ; assertConnectionsHistory ( HOST_2_OK , HOST_3_OK , HOST_2_OK , HOST_3_FAIL , HOST_2_FAIL , HOST_3_FAIL , HOST_1_OK , HOST_2_FAIL , HOST_3_OK ) ; } finally { if ( testStmt != null ) { testStmt . close ( ) ; } if ( testConn != null ) { testConn . close ( ) ; } } } }
tests the property ' failoverreadonly ' in a failover connection using three hosts and the following sequence of events : - [ \ host_1 : / host_2 : / host_3 ] - - > host_2 - [ \ host_1 : \ host_2 : / host_3 ] - - > host_3 - [ \ host_1 : / host_2 : \ host_3 ] - - > host_2 - [ / host_1 : \ host_2 : \ host_3 ] - - > host_1 - [ \ host_1 : \ host_2 : / host_3 ] - - > host_3 [ legend : " / host_n " - - > host_n up ; " \ host_n " - - > host_n down ]
