The LoadBalance mode of OceanBase Connector/J is used to create a connection for each connection object. Each time a connection object is created, a host is selected from the host list by using the SLB policy to create a connection. Profile-based LoadBalance can monitor the corresponding configuration file directory and realize it after updating the configuration file. You can update the SLB policy, host list, and other information without modifying the URL each time you execute getConnection.
Load balancing strategies
You can implement three load balancing strategies of OceanBase Connector/J by configuring the OBLB_STRATEGY and LOAD_BALANCE_STRATEGY parameters in the extended configuration information and the loadBalanceStrategy parameter in URL Option. The following table describes the valid values of the parameters.
| Strategy | Feature | Remarks |
|---|---|---|
| RANDOM | Random load balancing | Specifies to use the random algorithm, which is the default load balancing algorithm. |
| SERVERAFFINITY | Weighted load balancing | Specifies to use the weighted random algorithm. |
| ROTATION | Rotation load balancing | Specifies to use the rotation algorithm. |
Load balancing group selection strategy
OceanBase Connector/J supports only one strategy for load balancing between multiple groups of hosts, which corresponds to the OBLB_GROUP_STRATEGY parameter in the extended configuration information. The following table describes the valid value of the parameter.
| Strategy | Feature | Remarks |
|---|---|---|
| ROTATION | Specifies to use the rotation algorithm. | This is the default algorithm. |
Blacklist strategies
OceanBase Connector/J allows you to specify blacklist strategies by setting the following two sub-parameters of the OBLB_BLACKLIST parameter. The blacklist entry removal strategy can be configured by setting the REMOVE_STRATEGY sub-parameter. The blacklist entry adding strategies can be configured by setting theAPPEND_STRATEGY sub-parameter.
OceanBase Connector/J supports two blacklist entry adding strategies, which correspond to the APPEND_STRATEGY sub-parameter. The following table describes the valid values of the sub-parameter.
| Strategy | Feature | Remarks |
|---|---|---|
| NORMAL | Normal strategy | The default strategy, which specifies to add a host to the blacklist when a network exception occurs during the connection of the host. |
| RETRYDURATION | Retry within a specified period | Specifies to add a host to the blacklist after the number of connection retries within a specified period reaches the specified value. This strategy can be configured after you set the RETRYTIMES and DURATION parameters. The unit of DURATION is milliseconds. Example: (RETRYTIMES=3) (DURATION=100) specifies to add the host to the blacklist after the third connection retry fails within 100 ms. |
OceanBase Connector/J supports only one blacklist removal strategy, which corresponds to the REMOVE_STRATEGY sub-parameter. The following table describes the valid value of the sub-parameter.
| Strategy | Feature | Remarks |
|---|---|---|
| TIMEOUT | The timeout strategy | The default strategy, which specifies to remove a host from the blacklist after the specified timeout period, in milliseconds. |
Examples of blacklist strategies
String url = "jdbc:oceanbase:loadbalance://@(NET_SERVICE_NAME=(DESCRIPTION=
(OBLB=ON)
(OBLB_RETRY_ALL_DOWNS=120)
(OBLB_GROUP_STRATEGY=ROTATION)
(OBLB_BLACKLIST=
(
REMOVE_STRATEGY=(
(NAME=TIMEOUT)
(TIMEOUT=100)
)
)
(
APPEND_STRATEGY =(
(NAME=RETRYDURATION)
(RETRYTIMES=3)
(DURATION=100)
)
)
)
(ADDRESS_LIST=
(OBLB_STRATEGY=SERVERAFFINITY)
(ADDRESS=(PROTOCOL=tcp)(HOST=host1)(PORT=port1)(WEIGHT=1))
(ADDRESS=(PROTOCOL=tcp)(HOST=host2)(PORT=port2)(WEIGHT=10))
)
(ADDRESS_LIST=
(OBLB_STRATEGY=RANDOM)
(ADDRESS=(PROTOCOL=tcp)(HOST=host3)(port=port3))
(ADDRESS=(PROTOCOL=tcp)(HOST=host4)(port=port4))
)
(ADDRESS_LIST=
(OBLB_STRATEGY=ROTATION)
(ADDRESS=(PROTOCOL=tcp)(HOST=host3)(port=port3))
(ADDRESS=(PROTOCOL=tcp)(HOST=host4)(port=port4))
)
(CONNECT_DATA=(SERVICE_NAME=OBDATABASE))
))/OBDATABASE?rewriteBatchedStatements=true&allowSendParamTypes=true&prepStmtCacheSize=1000";
Connection = DriverManager.getConnection(url);
Parameters
| Parameter | Description | Default value | Remarks |
|---|---|---|---|
| DESCRIPTION | A description for the connection. | DESCRIPTION=(). The description of the connection is contained in the parentheses "()". |
None |
| OBLB | Specifies whether to enable load balancing. You can specify this parameter in DESCRIPTION or ADDRESS_LIST. |
OFF |
OceanBase Connector/J ignores this parameter because LoadBalance is enabled in the extended configuration information. Also, this parameter does not take effect if it is specified in ADDRESS_LIST. You must configure this parameter later. |
| LOAD_BALANCE | Specifies whether to enable load balancing. You can specify this parameter in DESCRIPTION or ADDRESS_LIST. |
OFF |
This parameter is equivalent to OBLB. |
| OBLB_RETRY_ALL_DOWNS | Specifies the number of retries to connect all hosts. This parameter is specified in DESCRIPTION. |
120 | This parameter is equivalent to the retryAllDowns in URL Option. |
| OBLB_STRATEGY | Specifies the LoadBalance strategy. You can specify this parameter in DESCRIPTION or ADDRESS_LIST. |
RANDOM |
If you specify this parameter in DESCRIPTION, each host specified in ADDRESS_LIST will use the strategy. If you specify this parameter in ADDRESS_LIST, only the specified hosts will use the strategy and the strategy settings in DESCRIPTION are ignored. |
| LOAD_BALANCE_STRATEGY | Specifies the LoadBalance strategy. You can specify this parameter in DESCRIPTION or ADDRESS_LIST. |
RANDOM |
This parameter is equivalent to OBLB_STRATEGY. |
| OBLB_GROUP_STRATEGY | Specifies the strategy for the access among hosts specified in ADDRESS_LIST. |
ROTATION |
Only the ROTATION strategy is supported. |
| OBLB_BLACKLIST | A parameter in DESCRIPTION. The parentheses "()" after OBLB_BLACKLIST contain REMOVE_STRATEGY and APPEND_STRATEGY which are parameters about the blacklist. |
OBLB_BLACKLIST=(). The description of the blacklist is contained in the parentheses "()". |
If you do not specify OBLB_BLACKLIST, the strategies specified for REMOVE_STRATEGY and APPEND_STRATEGY are used. |
| REMOVE_STRATEGY | A parameter of OBLB_BLACKLIST. |
REMOVE_STRATEGY specifies the strategy for removing a host from the blacklist. These strategies are contained in the parentheses "()". |
If you do not specify REMOVE_STRATEGY, the default value of TIMEOUT is used, which specifies to remove the host from the blacklist after a specified period. |
| APPEND_STRATEGY | A parameter of OBLB_BLACKLIST. APPEND_STRATEGY specifies strategies for adding a host from the blacklist. |
These strategies are contained in the parentheses "()". | If you do not specify APPEND_STRATEGY, the default NORMAL strategy is used, which specifies to add the host to the blacklist when its connection fails. |
| NAME | A parameter in both APPEND_STRATEGY and REMOVE_STRATEGY. You can use this parameter to specify the names of APPEND_STRATEGY and REMOVE_STRATEGY. |
The value of NAME must be either one of the specified strings. Otherwise, an error is returned. |
The only name of REMOVE_STRATEGY is TIMEOUT. Supported names of APPEND_STRATEGY are RETRYDURATION and NORMAL. |
| TIMEOUT | The value for NAME of REMOVE_STRATEGY. |
TIMEOUT= specifies the timeout period in milliseconds. Default value: 50. |
This parameter is equivalent to loadBalanceBlacklistTimeout in URL Option. |
| RETRYTIMES | One of the parameters of APPEND_STRATEGY when the value of NAME is RETRYDURATION. |
This parameter specifies the maximum number of retries within the period specified by DURATION. After the number of connection retries of a host exceeds the specified value, the host is added to the blacklist. |
You must use this parameter with DURATION. |
| DURATION | One of the parameters of APPEND_STRATEGY when the value of NAME is RETRYDURATION. |
When the number of connection retries of a host exceeds the value of RETRYTIMES within the period specified by DURATION, the host is added to the blacklist. |
You must use this parameter with RETRYTIMES. |
| ADDRESS_LIST | A parameter in DESCRIPTION. You can use this parameter to specify OBLB_STRATEGY for multiple groups of ADDRESS fields or a separate OBLB_STRATEGY. The load balancing strategy for multiple groups of hosts is specified by OBLB_GROUP_STRATEGY. |
ADDRESS_LIST=(). The description of an ADDRESS group is contained in the parentheses "()". |
None |
| ADDRESS | A parameter in ADDRESS_LIST that specifies the host information, such as the hostname, port, and weight. |
ADDRESS=(). The description of an ADDRESS is contained in the parentheses "()". |
None |
| HOST | A parameter in ADDRESS that specifies the name of the current host. |
None | None |
| PORT | A parameter in ADDRESS that specifies the port of the current host. |
None | None |
| WEIGHT | A parameter in ADDRESS that specifies the weight of the current host. |
None | The weight is recognized only when OBLB_STRATEGY is set to SERVERAFFINITY. |
| PROTOCOL | A parameter in ADDRESS that specifies the network protocol used by the current host. |
None | At present, this parameter is provided only to be compatible with the configuration format of the Local Naming Parameters file of Oracle. |
| CONNECT_DATA | Specifies the name of the database service to connect. | None | This parameter is provided only to be compatible with the configuration format of the Local Naming Parameters file of Oracle. |
| SERVICE_NAME | Specifies the schema information. | None | This parameter is provided only to be compatible with the configuration format of the Local Naming Parameters file of Oracle. |