title: Connect to an Oracle cluster in Java
body: <p>We have a pair of Oracle servers which are set up as nodes in a cluster (apologies if my terminology is way off).  In my <code>tnsnames.ora</code> file, we have an entry that looks like</p>
  
  <pre><code>EXAMPLE.GOV =
    (DESCRIPTION =
      (ADDRESS = (PROTOCOL = TCP)(HOST = 1.2.3.4)(PORT = 1521))
      (ADDRESS = (PROTOCOL = TCP)(HOST = 1.2.3.5)(PORT = 1521))
      (LOAD_BALANCE = yes)
      (CONNECT_DATA =
        (SERVER = DEDICATED)
        (SERVICE_NAME = example.gov)
      )
    )
  </code></pre>
  
  <p>and this works when I connect with programs which use the <code>tnsnames.ora</code> file.  However, I also have a Java program which uses the <a href="http://www.princeton.edu/~storacle/jdbc8_doc/oracle.jdbc.pool.OracleDataSource.html" rel="nofollow noreferrer">oracle.jdbc.pool.OracleDataSource</a> class to establish a connection</p>
  
  <pre><code>public static Connection connect() throws Exception {
      OracleDataSource ods = new OracleDataSource();
      ods.setDriverType("thin");
      ods.setServerName("1.2.3.4");
      ods.setDatabaseName("example");
      ods.setPortNumber(1521);
      ods.setUser("scott");
      ods.setPassword("tiger");
      return ods.getConnection();
  }
  </code></pre>
  
  <p>which just connects to one of the nodes directly.  I'd like to instead use the load-balancing <code>tnsnames.ora</code> approach, where it uses load balancing or whatever to connect to either one of the nodes, so that if one of them is down then it'll automatically connect to the other.</p>
  
  <p>Since I only have two nodes, I could easily just try opening a connection to the first node, then if that doesn't work open a connection to the second one.  However, I'm wondering if there's a more correct way to do this.</p>
  
  <p>I see that there's a <code>setTNSEntryName</code> parameter, but since my <code>tnsnames.ora</code> is in a nonstandard place, I'd need to set the <code>TNS_ADMIN</code> environment variable, which I'm not sure that I can even do from within Java.  Nor am I certain that this would work in any case.</p>
  
  <p>Does anyone know how to connect to a cluster of Oracle nodes from a Java program?</p>