DB2 Version 10.1 for Linux, UNIX, and Windows

Example of enabling client affinities in Java clients for DB2® for Linux, UNIX, and Windows connections

Before you can use client affinities for automatic client reroute in Java™ applications, you need to set properties to indicate that you want to use client affinities, and to identify the primary alternate servers.

The following example shows how to enable client affinities for failover without failback.

Suppose that you set the following properties for a connection to a database:

Property Value
enableClientAffinitiesList DB2BaseDataSource.YES (1)
clientRerouteAlternateServername host1,host2,host3
clientRerouteAlternatePortNumber port1,port2,port3
maxRetriesForClientReroute 3
retryIntervalForClientReroute 2
Suppose that a communication failure occurs during a connection to the server that is identified by host1:port1. The following steps demonstrate automatic client reroute with client affinities.
  1. The driver tries to connect to host1:port1.
  2. The connection to host1:port1 fails.
  3. The driver waits two seconds.
  4. The driver tries to connect to host1:port1.
  5. The connection to host1:port1 fails.
  6. The driver waits two seconds.
  7. The driver tries to connect to host1:port1.
  8. The connection to host1:port1 fails.
  9. The driver waits two seconds.
  10. The driver tries to connect to host2:port2.
  11. The connection to host2:port2 fails.
  12. The driver waits two seconds.
  13. The driver tries to connect to host2:port2.
  14. The connection to host2:port2 fails.
  15. The driver waits two seconds.
  16. The driver tries to connect to host2:port2.
  17. The connection to host2:port2 fails.
  18. The driver waits two seconds.
  19. The driver tries to connect to host3:port3.
  20. The connection to host3:port3 fails.
  21. The driver waits two seconds.
  22. The driver tries to connect to host3:port3.
  23. The connection to host3:port3 fails.
  24. The driver waits two seconds.
  25. The driver tries to connect to host3:port3.
  26. The connection to host3:port3 fails.
  27. The driver waits two seconds.
  28. The driver throws an SQLException with error code -4499.

The following example shows how to enable client affinities for failover with failback.

Suppose that you set the following properties for a connection to a database:

Property Value
enableClientAffinitiesList DB2BaseDataSource.YES (1)
clientRerouteAlternateServername host1,host2,host3
clientRerouteAlternatePortNumber port1,port2,port3
maxRetriesForClientReroute 3
retryIntervalForClientReroute 2
affinityFailbackInterval 300
Suppose that the database administrator takes the server that is identified by host1:port1 down for maintenance after a connection is made to host1:port1. The following steps demonstrate failover to an alternate server and failback to the primary server after maintenance is complete.
  1. The driver successfully connects to host1:port1 on behalf of an application.
  2. The database administrator brings down host1:port1.
  3. The application tries to do work on the connection.
  4. The driver successfully fails over to host2:port2.
  5. After a total of 200 seconds have elapsed, the work is committed.
  6. After a total of 300 seconds have elapsed, the failback interval has elapsed. The driver checks whether the primary server is up. It is not up, so no failback occurs.
  7. After a total of 350 seconds have elapsed, host1:port1 is brought back online.
  8. The application continues to do work on host2:port2, because the latest failback interval has not elapsed.
  9. After a total of 600 seconds have elapsed, the failback interval has elapsed again. The driver checks whether the primary server is up. It is now up.
  10. After a total of 650 seconds have elapsed, the work is committed.
  11. After a total of 651 seconds have elapsed, the application tries to start a new transaction on host2:port2. Failback to host1:port1 occurs, so the new transaction starts on host1:port1.