Enable automatic database reconnection
- Last Updated: May 25, 2021
- 4 minute read
- OpenEdge
- Version 12.2
If a database is offline or otherwise unavailable, client-server connections can automatically connect to an alternate database, such as a replication target or backup database.
Use database parameters to define a connection set consisting of the primary database and up to two alternate databases, plus an optional retry count and pause interval.
You can specify your connection set using session startup parameters, in the CONNECT statement, or in a parameter file ( .pf ).
Specify a connection set using database connection parameters
| Parameter | Description |
|---|---|
| Alternate Database 1 (-dbalt1). | Connection information for the alternate database. |
| Alternate Database 2 (-dbalt2) | Connection information for the second alternate database if there multiple alternates. |
| Retry Connect Set (-retryConnect) | Number of retry attempts allowed for the connection set as a whole. |
| Retry Connect Set Pause (-retryConnectPause) | Interval to wait, in seconds, before retrying the connection set. |
Database connection parameter considerations
- Like other database startup parameters,
-dbalt1,-dbalt2,-retryConnect, and-retryConnectPauseapply only to the last database parameter (-db) encountered in the parameter list thus far. - You must specify the primary database first, before listing an alternate, otherwise the AVM issues an error.
- If both
-dbalt1and-dbalt2are specified and the connection to the primary database fails,-dbalt1is tried first and-dbalt2is tried second. - If you only specify
-dbalt2, it becomes the first alternate. - If the AVM is unable to connect to any database the
CONNECTstatement goes through the usual error handling. - Any database parameters from the primary database, except for
-Hand-S, are also applied to the alternate databases, such as-ld,-Uand-P. - The logical name of the alternate database is always taken from the primary database, even if not explicitly set with
-ld. - If the same parameter is encountered more than once within the argument list for
-dbalt1or-dbalt2, the last one is used. This is consistent with the parsing rules for parameters in general. - If multiple
-dbalt1or-dbalt2parameters are encountered within the parameter list for a particular primary database, the last one is used. -ct(Connection Retry Attempts) is an independent, but related, startup parameter that controls the number of connection retries for an individual database connection. The connection attempts are done in succession on the same database connection and have a default value of 50 attempts; the AVM pauses one second between connection attempts. This differs from-retryConnect, which applies to the connection set as a whole. If using both-ctand-retryConnect, you may want to set-ctto 0, or to a value lower than the default.
Example configurations
Example: Specify a primary database with one alternate:
|
- Try to connect to mainDb.
- If that fails, it would try to connect to altDb.
- If that fails, it would pause 5 seconds (default for
-retryConnectPause) and repeat from step 1, three more times (because-retryConnectis 3).
Example: Specify a primary database with two alternates:
|
Example: Specify multiple primary databases with alternates:
|
Usage of parameters in the CONNECT statement
You may also use these database connection parameters in a CONNECT statement. For -dbalt1 and -dbalt2 each alternate database name, along with its host and port parameters, must be enclosed in quotes. In the following example, the arguments to -dbalt1 and -dbalt2 are surrounded by single quotes and the connection string is surrounded by double quotes. You can reverse this and use double quotes to surround the inner arguments and use single quotes for the outer connection string.
In the following example code, one alternate database is specified:
|
The following example code assigns the connection string to a CHARACTER variable. It uses single quotes to enclose the inner -dbalt1 parameters and double quotes for the outer string:
|
The following example code specifies two alternate databases:
|
Enable logging for troubleshooting
The DB.Connects log entry type (-logentrytypes DB.Connects) turns on logging for database connections. (For information on configuring and enabling logging, see Logging infrastructure overview.) Normally, connect and disconnect messages are logged at the basic level (2 or higher). With this feature, additional messages are logged if the initial attempt to connect to the primary database fails. Consider the following connection string:
|
|
|
-retryConnectPause.
|
-autoReconnect is enabled, a message is logged:
|