Connecting to Server
This section introduces the method connect
for connecting to DolphinDB
server.
Syntax
public boolean connect(String hostName, int port, String userId, String password, String initialScript, boolean enableHighAvailability, String[] highAvailabilitySites, boolean reconnect, boolean enableLoadBalance, int tryReconnectNums)
Parameters
- hostName: The host name.
- port: The port number.
- timeout (optional, default 3000): An int specifying both the maximum
connection and read timeouts (in ms) for underlying API Socket connections.Note: From version 3.00.1.3 onwards, new timeout parameters connectTimeout and readTimeout are added. If set along with timeout, the values of connectTimeout and readTimeout take precedence.
- connectTimeout (optional, default 3000): An int indicating the maximum connection timeout (in ms) for underlying API Socket connections.
- readTimeout (optional, default 0): An int indicating the maximum read timeout (in ms) for underlying API Socket connections.
- userId (optional, default ""): The user to log in.
- password (optional, default ""): The password to log in.
If the connection is established without specifying a user’s userId and password, the scripts will be executed with guest privileges. To connect to the server as an administrator, you can fill the administrator username and password. Alternatively, you can refer to the
login
method that will be introduced later. - initialScript (optional, default ""): The initialization script. To define and use user-defined functions in a Java client, you can pass the function definitions to initialScript. These functions do not required to be defined repeatedly every time a script is executed. The Java API provides an automatic reconnection mechanism, which will generate a new session after reconnection. If initialScript is specified, the Java API will automatically execute the script and re-register the functions required by subsequent scripts.
- enableHighAvailability (optional, default false): A Boolean value indicating whether to enable high availability.
- highAvailabilitySites (optional, default null): A list of host:port of all available nodes.
- reconnect (optional, default false): A Boolean value indicating whether to resubscribe after network disconnection.
- enableLoadBalance (optional, default false): A Boolean value indicating
whether to enable load balancing in HA mode. Load balancing is only supported in
HA mode and it is disabled by default. As of version 1.30.22.2, load balancing
is automatically enabled for HA mode. This parameter is introduced since version
2.00.11.0.
If load balancing is disabled in HA mode, the API establishes connection to a random node. You can specify a group of nodes for connection for highAvailabilitySites, and the API will establish the connection to a random node from the group. If load balancing is enabled in HA mode, the API establishes connection to a low-load node. A low-load node is selected based on: memory usage<80%, connections<90%, and node load<80%. If a disconnection occurs, the API automatically reconnects following the above rules.
- tryReconnectNums: Number of reconnection attempts for each connection. If not specified, reconnection attempts are not limited. If specified, limited attempts are applied in HA and non-HA modes. In HA mode, cycles through each node once per attempt, up to tryReconnectNums cycles.
Returns
A Boolean value indicating whether the connection has been established.
Example
The following example creates a DBConnection
object and connects to
DolphinDB server.
First, create a connection object and specify the high availability node array. Then
perform the connection operation, passing the specified hostName
,
port
, userId
, and password
.
In this example, the initialization script parameter initialScript
is clearAllCache()
, which clears cached data, including data loaded
into memory from partitioned tables and intermediate results of map-reduce tasks in
distributed computing. High availability and automatic reconnection are enabled.
@Test public void testConnect() throws IOException { DBConnection dbConnection = new DBConnection(); String[] highAvailabilitySites = new String[]{"192.168.1.167:18921", "192.168.1.167:18922", "192.168.1.167:18923"}; boolean success = dbConnection.connect("192.168.1.167", 18921, "admin", "123456", "clearAllCache();", true, highAvailabilitySites, true); System.out.println("connect result: " + success); }
Throws
- If the
userId
orpassword
is incorrect:java.io.IOException: 192.168.1.167:18921 Server response: 'RemoteRun[ctl18920] The user name or password is incorrect.' function: 'login'
- If the target server IP or port is
incorrect:
java.net.SocketException: Connection reset
If an error containing
SocketException
is reported duringconnect
, check the IP and port first. - If the
initialScript
has errors, such as using an undefined variablex
:java.io.IOException: 192.168.1.167:18922 Server response: 'Syntax Error: [line #1] Cannot recognize the token x' script: 'x'
If the error starts with
hostName_+":"+port_+" Server response: 'Syntax Error'
, the error occurs on the server.