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.
- The driver tries to connect to host1:port1.
- The connection to host1:port1 fails.
- The driver waits two seconds.
- The driver tries to connect to host1:port1.
- The connection to host1:port1 fails.
- The driver waits two seconds.
- The driver tries to connect to host1:port1.
- The connection to host1:port1 fails.
- The driver waits two seconds.
- The driver tries to connect to host2:port2.
- The connection to host2:port2 fails.
- The driver waits two seconds.
- The driver tries to connect to host2:port2.
- The connection to host2:port2 fails.
- The driver waits two seconds.
- The driver tries to connect to host2:port2.
- The connection to host2:port2 fails.
- The driver waits two seconds.
- The driver tries to connect to host3:port3.
- The connection to host3:port3 fails.
- The driver waits two seconds.
- The driver tries to connect to host3:port3.
- The connection to host3:port3 fails.
- The driver waits two seconds.
- The driver tries to connect to host3:port3.
- The connection to host3:port3 fails.
- The driver waits two seconds.
- 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.
- The driver successfully connects to host1:port1 on behalf of an
application.
- The database administrator brings down host1:port1.
- The application tries to do work on the connection.
- The driver successfully fails over to host2:port2.
- After a total of 200 seconds have elapsed, the work is committed.
- 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.
- After a total of 350 seconds have elapsed, host1:port1 is brought
back online.
- The application continues to do work on host2:port2, because the
latest failback interval has not elapsed.
- 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.
- After a total of 650 seconds have elapsed, the work is committed.
- 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.