Example of enabling Db2 on Linux, UNIX, and Windows systems automatic client reroute support in Java applications

Java™ client setup for Db2 on Linux, UNIX, and Windows systems automatic client reroute support includes setting several IBM® Data Server Driver for JDBC and SQLJ properties.

The following example demonstrates setting up Java client applications for Db2 on Linux, UNIX, and Windows systems automatic client reroute support.

Suppose that your installation has a primary server and an alternate server with the following server names and port numbers:
Server name Port number
srv1.sj.ibm.com 50000
srv3.sj.ibm.com 50002
The following code sets up DataSource properties in an application so that the application connects to srv1.sj.ibm.com as the primary server, and srv3.sj.ibm.com as the alternative server. That is, if srv1.sj.ibm.com is down during the initial connection, the driver should connect to srv3.sj.ibm.com.
 ds.setDriverType(4);
 ds.setServerName("srv1.sj.ibm.com");
 ds.setPortNumber("50000");
 ds.setClientRerouteAlternateServerName("srv3.sj.ibm.com");
 ds.setClientRerouteAlternatePortNumber("50002");

The following code configures JNDI for automatic client reroute. It creates an instance of DB2ClientRerouteServerList, binds that instance to the JNDI registry, and assigns the JNDI name of the DB2ClientRerouteServerList object to the clientRerouteServerListJNDIName property.

// Create a starting context for naming operations
InitialContext registry = new InitialContext();
// Create a DB2ClientRerouteServerList object
DB2ClientRerouteServerList address = new DB2ClientRerouteServerList();

// Set the port number and server name for the primary server
address.setPrimaryPortNumber(50000);
address.setPrimaryServerName("srv1.sj.ibm.com");

// Set the  port number and server name for the alternate server
int[] port = {50002};
String[] server = {"srv3.sj.ibm.com"};
address.setAlternatePortNumber(port);
address.setAlternateServerName(server);

registry.rebind("serverList", address);
// Assign the JNDI name of the DB2ClientRerouteServerList object to the 
// clientRerouteServerListJNDIName property
datasource.setClientRerouteServerListJNDIName("serverList");