To ensure continuous, uninterrupted access to data, the drivers provide the following levels of failover protection, listed from basic to more comprehensive:
|
■
|
Connection failover provides failover protection for new connections only. The driver fails over new connections to an alternate, or backup, database server if the primary database server is unavailable, for example, because of a hardware failure or traffic overload. If a connection to the database is lost, or dropped, the driver does not fail over the connection. This failover method is the default.
|
|
■
|
Extended connection failover provides failover protection for new connections and lost database connections. If a connection to the database is lost, the driver fails over the connection to an alternate server, preserving the state of the connection at the time it was lost, but not any work in progress.
|
|
■
|
Select Connection failover provides failover protection for new connections and lost database connections. In addition, it provides protection for Select statements that have work in progress. If a connection to the database is lost, the driver fails over the connection to an alternate server, preserving the state of the connection at the time it was lost and preserving the state of any work being performed by Select statements.
|
The method you choose depends on how failure-tolerant your application is. For example, if a communication failure occurs while processing, can your application handle the recovery of transactions and restart them?
When using either extended connection failover mode or select connection failover mode, your application needs the ability to recover and restart transactions. The advantage of select mode is that it preserves the state of any work that was being performed by the Select statement at the time of connection loss. If your application had been iterating through results at the time of the failure, when the connection is reestablished, the driver can reposition on the same row where it stopped so that the application does not have to undo all of its previous result processing. For example, if your application was paging through a list of items on a Web page when a failover occurred, the next page operation would be seamless instead of starting from the beginning. Performance, however, is a factor in selecting a failover mode. Select mode incurs additional overhead when tracking which rows the application has already processed.
You can specify which failover method you want to use by setting the FailoverMode connection property. Read the following sections for details on each failover method:
Connection failover allows an application to connect to an alternate, or backup, database server if the primary database server is unavailable, for example, because of a hardware failure or traffic overload. Connection failover provides failover protection for new connections only and does not provide protection for lost connections to the database, nor does it preserve states for transactions or queries.
You can customize the drivers for connection failover by configuring a list of alternate database servers that are tried if the primary server is not accepting connections. Connection attempts continue until a connection is successfully established or until all the alternate database servers have been tried the specified number of times.
For example, suppose you have the environment with multiple database servers as shown in
Figure 3-1. Database Server A is designated as the primary database server, Database Server B is the first alternate server, and Database Server C is the second alternate server.
First, the application attempts to connect to the primary database server, Database Server A (
1). If connection failover is enabled and Database Server A fails to accept the connection, the application attempts to connect to Database Server B (
2). If that connection attempt also fails, the application attempts to connect to Database Server C (
3).
In this scenario, it is probable that at least one connection attempt would succeed, but if no connection attempt succeeds, the driver can retry each alternate database server (primary and alternate) for a specified number of attempts. You can specify the number of attempts that are made through the
connection retry feature. You can also specify the number of seconds of delay, if any, between attempts through the
connection delay feature. See
“Using Connection Retry” for more information about connection retry.
A driver fails over to the next alternate database server only if a successful connection cannot be established with the current alternate server. If the driver successfully establishes communication with a database server and the connection request is rejected by the database server because, for example, the login information is invalid, then the driver generates an exception.
When a connection to the database is lost, the driver fails over the connection to an alternate server, restoring the same state of the connection at the time it was lost. For example, when reestablishing a lost connection on the alternate database server, the driver performs the following actions:
The driver does not preserve work in progress. For example, if the database server experienced a hardware failure while processing a query, partial rows processed by the database and returned to the client would be lost.
If an exception occurs while the driver is reestablishing a lost connection, the driver can fail the entire failover process or proceed with the process as far as it is able. For example, suppose an exception occurred while reestablishing the connection because the driver was unable to log the user into the database. In this case, you may want the driver to notify your application of the exception and proceed with the failover process. You can choose how you want the driver to behave if exceptions occur during failover by setting the FailoverGranularity connection property.
During the failover process, your application may experience a short pause while the driver establishes a new connection or reestablishes a lost connection on an alternate server. If your application is time-sensitive (a real-time customer order application, for example) and cannot absorb this wait, you can set the FailoverPreconnect property to
true. Setting the FailoverPreconnect property to
true instructs the driver to establish connections to the primary server and an alternate server at the same time. Your application uses the first connection that is successfully established. As a bonus, if this connection to the database is lost at a later time, the driver saves time in reestablishing the connection on the server it fails over to because it can use the spare connection in its failover process.
In addition, the driver can recover work in progress because it keeps track of the last Select statement the application executed on each Statement handle, including how many rows were fetched to the client. For example, if the database had only processed 500 of 1,000 rows requested by a Select statement when the connection was lost, the driver would reestablish the connection to an alternate server, re-execute the Select statement, and position the cursor on the next row so that the driver can continue fetching the balance of rows as if nothing had happened.
Performance, however, is a factor when considering whether to use Select mode. Select mode incurs additional overhead when tracking what rows the application has already processed.
When in manual transaction mode, no statements are rerun if any of the operations in the transaction were Insert, Update, or Delete. This is true even if the statement in process at the time of failover was a Select statement.
By default, the driver verifies that the rows that are restored match the rows that were originally fetched and, if they do not match, generates an exception warning your application that the Select statement must be reissued. By setting the FailoverGranularity connection property, you can customize the driver to fail the entire failover process if the rows do not match.
Many databases provide advanced database replication technologies such as DB2 High Availability Disaster Recovery (HADR), Oracle Real Application Clusters (RAC), and Microsoft Cluster Server (MSCS). The failover functionality provided by the drivers does not require any of these technologies, but can work with them to provide comprehensive failover protection. Use the following guidelines for primary and alternate servers to ensure that failover works correctly in your environment:
Client load balancing helps distribute new connections in your environment so that no one server is overwhelmed with connection requests. When client load balancing is enabled, the order in which primary and alternate database servers are tried is random. For example, suppose that client load balancing is enabled as shown in the
Figure 3-2.
First, Database Server B is tried (1). Then, Database Server C may be tried (
2), followed by a connection attempt to Database Server A (
3). In contrast, if client load balancing were not enabled in this scenario, each database server would be tried in sequential order, primary server first, then each alternate server based on its entry order in the alternate servers list.
Connection retry defines the number of times the driver attempts to connect to the primary server and, if configured, alternate database servers after the initial unsuccessful connection attempt. It can be used with connection failover, extended connection failover, and select failover. Connection retry can be an important strategy for system recovery. For example, suppose you have a power failure in which both the client and the server fails. When the power is restored and all computers are restarted, the client may be ready to attempt a connection before the server has completed its startup routines. If connection retry is enabled, the client application can continue to retry the connection until a connection is successfully accepted by the server.
Using the ConnectionRetryCount and ConnectionRetryDelay properties, you can specify the number of times the driver attempts to connect and the time in seconds between connection attempts. For details on configuring connection retry, see the individual driver chapter in this book.