9 Microsoft SQL Server Driver : Configuring Failover

Configuring Failover
1
■
■
See “Specifying Primary and Alternate Servers”.
NOTES:
■
■
If using failover with Microsoft Cluster Server (MSCS), which determines the alternate server for failover instead of the driver, any alternate server specified must be the same as the primary server. For example:
jdbc:datadirect:sqlserver://server1:1433;DatabaseName=TEST;
User=test;Password=secret;AlternateServers=(server1:1433;
DatabaseName=TEST)
2
Choose a failover method by setting the FailoverMode connection property. The default method is connection failover (FailoverMode=connect). See “Using Failover” for an overview of each failover method.
3
If FailoverMode=extended or FailoverMode=select, set the FailoverGranularity property to specify how you want the driver to behave if exceptions occur while trying to reestablish a lost connection. The default behavior of the driver is to continue with the failover process and post any exceptions on the statement on which they occur (FailoverGranularity=nonAtomic).
4
5
Optionally, set the FailoverPreconnect property if you want the driver to establish a connection with the primary and an alternate server at the same time. The default behavior is to connect to an alternate server only when failover is caused by an unsuccessful connection attempt or a lost connection (FailoverPreconnect=false).
Specifying Primary and Alternate Servers
Connection information for primary and alternate servers can be specified using either one of the following methods:
■
■
For example, the following connection URL for the SQL Server driver specifies connection information for the primary and alternate servers using a connection URL:
jdbc:datadirect:sqlserver://server1:1433;DatabaseName=TEST;User=test;
Password=secret;AlternateServers=(server2:1433;DatabaseName=TEST2,server3:1433;
DatabaseName=TEST3)
In this example:
...server1:1433;DatabaseName=TEST...
is the part of the connection URL that specifies connection information for the primary server. Alternate servers are specified using the AlternateServers property. For example:
...;AlternateServers=(server2:1433;DatabaseName=TEST2,server3:1433;
DatabaseName=TEST3)
Similarly, the same connection information for the primary and alternate servers specified using a JDBC data source would look like this:
SQLServerDataSource mds = new SQLServerDataSource();
mds.setDescription("My SQLServerDataSource");
mds.setServerName("server1");
mds.setPortNumber(1433);
mds.setDatabaseName("TEST");
mds.setUser("test");
mds.setPassword("secret");
mds.setAlternateServers(server2:1433;DatabaseName=TEST2, server3:1433;
   DatabaseName=TEST3)
In this example, connection information for the primary server is specified using the ServerName, PortNumber, and DatabaseName properties. Connection information for alternate servers is specified using the AlternateServers property.
The SQL Server driver also allows you to specify connections to named instances, multiple instances of a Microsoft SQL Server database running concurrently on the same server. If specifying named instances for the primary and alternate servers, the connection URL would look like this:
jdbc:datadirect:sqlserver://server1\\instance1;User=test;Password=secret;
AlternateServers=(server2\\instance2:1433;DatabaseName=TEST2,
server3\\instance3:1433;DatabaseName=TEST3)
Similarly, the same connection information to named instances for the primary and alternate servers specified using a JDBC data source would look like this:
SQLServerDataSource mds = new SQLServerDataSource();
mds.setDescription("My SQLServerDataSource");
mds.setServerName("server1\\instance1");
mds.setPortNumber(1433);
mds.setDatabaseName("TEST");
mds.setUser("test");
mds.setPassword("secret");
mds.setAlternateServers(server2\\instance2:1433;DatabaseName=
   TEST2,server3\\instance3:1433;DatabaseName=TEST3)
To connect to a named instance using a data source, you specify the named instance on the primary server using the ServerName property.
See “Connecting to Named Instances” for more information about connecting to named instances on Microsoft SQL Server.
The value of the AlternateServers property is a string that has the format:
(servername1[:port1][;property=value][,servername2[:port2]
[;property=value]]...)
or, if connecting to named instances:
(servername1\\instance1[;property=value][,servername2\\instance2
[;property=value]]
where:
servername1 is the IP address or server name of the first alternate database server, servername2 is the IP address or server name of the second alternate database server, and so on. The IP address or server name is required for each alternate server entry.
instance1 is the named instance on the first alternate database server, servername2 is the named instance on the second alternate database server, and so on. If connecting to named instances, the named instance is required for each alternate server entry.
port1 is the port number on which the first alternate database server is listening, port2 is the port number on which the second alternate database server is listening, and so on. The port number is optional for each alternate server entry. If unspecified, the port number specified for the primary server is used. If a port number is unspecified for the primary server, a default port number of 1433 is used.
property=value is the DatabaseName connection property. This property is optional for each alternate server entry. For example:
jdbc:datadirect:sqlserver://server1:1433;DatabaseName=TEST;User=test;
Password=secret;AlternateServers=(server2:1433;DatabaseName=TEST2,server3:1433;
DatabaseName=TEST3)
or, if connecting to named instances:
jdbc:datadirect:sqlserver://server1\\instance1:1433;DatabaseName=TEST;
User=test;Password=secret;AlternateServers=(server2\\instance2:1433;
DatabaseName=TEST2,server3\\instance3:1433;DatabaseName=TEST3)
If you do not specify the DatabaseName connection property in an alternate server entry, the connection to that alternate server uses the property specified in the URL for the primary server. For example, if you specify DatabaseName=TEST for the primary server, but do not specify a database name in the alternate server entry as shown in the following URL, the driver tries to connect to the TEST database on the alternate server:
jdbc:datadirect:sqlserver://server1:1433;DatabaseName=TEST;User=test;
Password=secret;AlternateServers=(server2:1433,server3:1433)
Specifying Connection Retry
Connection retry allows the SQL Server driver to retry connections to the primary database server, and if specified, alternate servers until a successful connection is established. You use the ConnectionRetryCount and ConnectionRetryDelay properties to enable and control how connection retry works. For example:
jdbc:datadirect:sqlserver://server1:1433;DatabaseName=TEST;User=test;
Password=secret;AlternateServers=(server2:1433;DatabaseName=TEST2,server3:1433;
DatabaseName=TEST3);ConnectionRetryCount=2;ConnectionRetryDelay=5
In this example, if a successful connection is not established on the SQL Server driver’s first pass through the list of database servers (primary and alternate), the driver retries the list of servers in the same sequence twice (ConnectionRetryCount=2). Because the connection retry delay has been set to five seconds (ConnectionRetryDelay=5), the driver waits five seconds between retry passes.
Failover Properties
Table 9-5 summarizes the connection properties that control how failover works with the SQL Server driver. See “Connection Properties” for details about configuring each property.
One or multiple alternate database servers. An IP address or server name identifying each server is required. Port number and the connection property DatabaseName are optional. If the port number is unspecified, the port number specified for the primary server is used. If a port number is unspecified for the primary server, the default port number of 1433 is used.
Number of times the driver retries the primary database server, and if specified, alternate servers until a successful connection is established. The default is 5.
Wait interval, in seconds, between connection retry attempts when the ConnectionRetryCount property is set to a positive integer. The default is 1.
Determines whether the driver fails the entire failover process or continues with the process if exceptions occur while trying to reestablish a lost connection. The default is nonAtomic (the driver continues with the failover process and posts any exceptions on the statement on which they occur).
The failover method you want the driver to use. The default is connect (connection failover is used).
Specifies whether the driver tries to connect to the primary and an alternate server at the same time. The default is false (the driver tries to connect to an alternate server only when failover is caused by an unsuccessful connection attempt or a lost connection).
Sets whether the driver will use client load balancing in its attempts to connect to the database servers (primary and alternate). If client load balancing is enabled, the driver uses a random pattern instead of a sequential pattern in its attempts to connect. The default is false (client load balancing is disabled).
Port listening for connections on the primary database server. This property is supported only for data source connections. The default port number is 1433.
IP address or server name for the primary database server. This property is supported only for data source connections.
See “Using Failover” and “Using Client Load Balancing” for overviews of failover and client load balancing.