DB2 Version 10.1 for Linux, UNIX, and Windows

Operation of automatic client reroute for connections to DB2 for Linux, UNIX, and Windows from Java clients

When IBM® Data Server Driver for JDBC and SQLJ client reroute support is enabled, a Java™ application that is connected to a DB2® for Linux, UNIX, and Windows database can continue to run when the primary server has a failure.

Automatic client reroute for a Java application that is connected to a DB2 for Linux, UNIX, and Windows database operates in the following way when support for client affinities is disabled:

  1. During each connection to the data source, the IBM Data Server Driver for JDBC and SQLJ obtains primary and alternate server information.
    • For the first connection to a DB2 for Linux, UNIX, and Windows database:
      1. If the clientRerouteAlternateServerName and clientRerouteAlternatePortNumber properties are set, the IBM Data Server Driver for JDBC and SQLJ loads those values into memory as the alternate server values, along with the primary server values serverName and portNumber.
      2. If the clientRerouteAlternateServerName and clientRerouteAlternatePortNumber properties are not set, and a JNDI store is configured by setting the property clientRerouteServerListJNDIName on the DB2BaseDataSource, the IBM Data Server Driver for JDBC and SQLJ loads the primary and alternate server information from the JNDI store into memory.
      3. If no DataSource properties are set for the alternate servers, and JNDI is not configured, the IBM Data Server Driver for JDBC and SQLJ checks DNS tables for primary and alternate server information. If DNS information exists, the IBM Data Server Driver for JDBC and SQLJ loads those values into memory.
        In a DB2 pureScale® environment, regardless of the outcome of the DNS lookup:
        1. If configuration property db2.jcc.outputDirectory is set, the IBM Data Server Driver for JDBC and SQLJ searches the directory that is specified by db2.jcc.outputDirectory for a file named jccServerListCache.bin.
        2. If db2.jcc.outputDirectory is not set, and the java.io.tmpdir system property is set, the IBM Data Server Driver for JDBC and SQLJ searches the directory that is specified by java.io.tmpdir for a file named jccServerListCache.bin.
        3. If jccServerListCache.bin can be accessed, the IBM Data Server Driver for JDBC and SQLJ loads the cache into memory, and obtains the alternate server information from jccServerListCache.bin for the serverName value that is defined for the DataSource object.
      4. If no primary or alternate server information is available, a connection cannot be established, and the IBM Data Server Driver for JDBC and SQLJ throws an exception.
    • For subsequent connections, the IBM Data Server Driver for JDBC and SQLJ obtains primary and alternate server values from driver memory.
  2. The IBM Data Server Driver for JDBC and SQLJ attempts to connect to the data source using the primary server name and port number.

    In a non-DB2 pureScale environment, the primary server is a stand-alone server. In a DB2 pureScale environment, the primary server is a member of a DB2 pureScale instance.

    If the connection is through the DriverManager interface, the IBM Data Server Driver for JDBC and SQLJ creates an internal DataSource object for automatic client reroute processing.

  3. If the connection to the primary server fails:
    1. If this is the first connection, the IBM Data Server Driver for JDBC and SQLJ attempts to reconnect to a server using information that is provided by driver properties such as clientRerouteAlternateServerName and clientRerouteAlternatePortNumber.
    2. If this is not the first connection, the IBM Data Server Driver for JDBC and SQLJ attempts to make a connection using the information from the latest server list that is returned from the server.

    Connection to an alternate server is called failover.

    The IBM Data Server Driver for JDBC and SQLJ uses the maxRetriesForClientReroute and retryIntervalForClientReroute properties to determine how many times to retry the connection and how long to wait between retries. An attempt to connect to the primary server and alternate servers counts as one retry.

  4. If the connection is not established, maxRetriesForClientReroute and retryIntervalForClientReroute are not set, and the original serverName and portNumber values that are defined on the DataSource are different from the serverName and portNumber values that were used for the current connection, the connection is retried with the serverName and portNumber values that are defined on the DataSource.
  5. If failover is successful during the initial connection, the driver generates an SQLWarning. If a successful failover occurs after the initial connection:
    • If seamless failover is enabled, and the following conditions are satisfied, the driver retries the transaction on the new server, without notifying the application.
      • The enableSeamlessFailover property is set to DB2BaseDataSource.YES (1).
      • The connection is not in a transaction. That is, the failure occurs when the first SQL statement in the transaction is executed.
      • There are no global temporary tables in use on the server.
      • There are no open, held cursors.
    • If seamless failover is not in effect, the driver throws an SQLException to the application with error code -4498, to indicate to the application that the connection was automatically reestablished and the transaction was implicitly rolled back. The application can then retry its transaction without doing an explicit rollback first.

      A reason code that is returned with error code -4498 indicates whether any database server special registers that were modified during the original connection are reestablished in the failover connection.

    You can determine whether alternate server information was used in establishing the initial connection by calling the DB2Connection.alternateWasUsedOnConnect method.

  6. After failover, driver memory is updated with new primary and alternate server information that is returned from the new primary server.

Examples

Example: Automatic client reroute to a DB2 for Linux, UNIX, and Windows server when maxRetriesForClientReroute and retryIntervalForClientReroute are not set: Suppose that the following properties are set for a connection to a database:

Property Value
enableClientAffinitiesList DB2BaseDataSource.NO (2)
serverName host1
portNumber port1
clientRerouteAlternateServerName host2
clientRerouteAlternatePortNumber port2
The following steps demonstrate an automatic client reroute scenario for a connection to a DB2 for Linux, UNIX, and Windows server:
  1. The IBM Data Server Driver for JDBC and SQLJ loads host1:port1 into its memory as the primary server address, and host2:port2 into its memory as the alternate server address.
  2. On the initial connection, the driver tries to connect to host1:port1.
  3. The connection to host1:port1 fails, so the driver tries another connection to host1:port1.
  4. The reconnection to host1:port1 fails, so the driver tries to connect to host2:port2.
  5. The connection to host2:port2 succeeds.
  6. The driver retrieves alternate server information that was received from server host2:port2, and updates its memory with that information.

    Assume that the driver receives a server list that contains host2:port2, host2a:port2a. host2:port2 is stored as the new primary server, and host2a:port2a is stored as the new alternate server. If another communication failure is detected on this same connection, or on another connection that is created from the same DataSource, the driver tries to connect to host2:port2 as the new primary server. If that connection fails, the driver tries to connect to the new alternate server host2a:port2a.

  7. A communication failure occurs during the connection to host2:port2.
  8. The driver tries to connect to host2a:port2a.
  9. The connection to host2a:port2a is successful.
  10. The driver retrieves alternate server information that was received from server host2a:port2a, and updates its memory with that information.

Example: Automatic client reroute to a DB2 for Linux, UNIX, and Windows server in a DB2 pureScale environment, when maxRetriesForClientReroute and retryIntervalForClientReroute are not set, and configuration property db2.jcc.outputDirectory is set: Suppose that the following properties are set for a connection that is established from DataSource A:

Property Value
enableClientAffinitiesList DB2BaseDataSource.NO (2)
serverName host1
portNumber port1
db2.jcc.outputDirectory (configuration property) /home/tmp
The following steps demonstrate an automatic client reroute scenario for a connection to a DB2 for Linux, UNIX, and Windows server:
  1. Using the information in DataSource A, the IBM Data Server Driver for JDBC and SQLJ loads host1:port1 into its memory as the primary server address. The driver searches for cache file jccServerListCache.bin in /home/tmp, but the cache file does not exist.
  2. The connection to host1:port1 succeeds. Suppose that the server returns a server list that contains host1:port1 and host2:port2.
  3. The driver creates a cache in memory, with an entry that specifies host2:port2 as the alternate server list for host1:port1. The driver then creates the cache file /home/tmp/jccServerListCache.bin, and writes the cache from memory to this file.
  4. The connection of Application A to host1:port1 fails, so the driver tries to connect to host2:port2.
  5. The connection of Application A to host2:port2 succeeds. Suppose that the server returns a server list that contains host2:port2 and host2a:port2a. host2:port2 is the new primary server, and host2a:port2a is the new alternate server.
  6. The driver looks for alternate server information for host2:port2 in the in-memory cache, but does not find any. It creates a new entry in the in-memory cache for host2:port2, with host2a:port2a as the alternate server list. The driver updates cache file /home/tmp/jccServerListCache.bin with the new entry that was added to the in-memory cache.
  7. Application A completes, and the JVM exits.
  8. Application B, which also uses DataSource A, starts.
  9. The driver loads the server list from cache file /home/tmp/jccServerListCache.bin into memory, and finds the entry for host1:port1, which specifies host2:port2 as the alternate server list. The driver sets host2:port2 as the alternate server list for host1:port1.
  10. A communication failure occurs when Application B tries to connect to host1:port1.
  11. Application B attempts to connect to alternate server host2:port2.
  12. The connection to host2:port2 succeeds. Application B continues.

Example: Automatic client reroute to a DB2 for Linux, UNIX, and Windows server when maxRetriesForClientReroute and retryIntervalForClientReroute are set for multiple retries: Suppose that the following properties are set for a connection to a database:

Property Value
enableClientAffinitiesList DB2BaseDataSource.NO (2)
serverName host1
portNumber port1
clientRerouteAlternateServerName host2
clientRerouteAlternatePortNumber port2
maxRetriesForClientReroute 3
retryIntervalForClientReroute 2
The following steps demonstrate an automatic client reroute scenario for a connection to a DB2 for Linux, UNIX, and Windows server:
  1. The IBM Data Server Driver for JDBC and SQLJ loads host1:port1 into its memory as the primary server address, and host2:port2 into its memory as the alternate server address.
  2. On the initial connection, the driver tries to connect to host1:port1.
  3. The connection to host1:port1 fails, so the driver tries another connection to host1:port1.
  4. The connection to host1:port1 fails again, so the driver tries to connect to host2:port2.
  5. The connection to host2:port2 fails.
  6. The driver waits two seconds.
  7. The driver tries to connect to host1:port1 and fails.
  8. The driver tries to connect to host2:port2 and fails.
  9. The driver waits two seconds.
  10. The driver tries to connect to host1:port1 and fails.
  11. The driver tries to connect to host2:port2 and fails.
  12. The driver waits two seconds.
  13. The driver throws an SQLException with error code -4499.