ExclusiveDBConnectionPool

The ExclusiveDBConnectionPool works with tasks. After constructing the pool, users need to call the execute method of the pool to run BasicDBTask. Users can then use methods of BasicDBTask to execute scripts, check execution status, and retrieve results, such as using the getResult method to obtain the task's execution result.

Constructor

ExclusiveDBConnectionPool(String host, int port, String uid, String pwd, int count, boolean loadBalance, boolean enableHighAvailability, String[] highAvailabilitySites, String initialScript,boolean compress, boolean useSSL, boolean usePython)

Parameters

  • host: Host name.
  • port: Port number.
  • uid (optional, default ""): Username.
  • pwd (optional, default ""): Password.
  • count: The number of connections in the pool. If the number of submitted tasks exceeds the number of connections, tasks will wait until a connection becomes available.
  • loadBalance: Whether to enable load balancing when initializing the connections.
    • If enabled, the API will distribute the connections evenly among all nodes in the cluster.
    • If not enabled, all connections in the pool are established using the specified host and port.
  • enableHighAvailability (optional, default false): Whether to enable high availability for connections in the pool.
  • highAvailabilitySites (optional, default null): A list of ip:port of all available nodes for high availability. This parameter is used only if enableHighAvailability is true.
    • If null:
      • For versions before 3.00.0.1, only the node specified by host and port is supported.
      • For version 3.00.0.1 and later, all nodes in the cluster are considered as high-availability nodes.
    • If not null, the specified nodes are used as high-availability nodes. If the current node disconnects, the connection will switch to the nodes in the order specified.
  • initialScript (optional, default ""): The script to be run during initialization.
  • compress (optional, default false): Whether to compress data during download. This compress mode is useful for large data queries. Transferring compressed data can save network bandwidth but increases computation for DolphinDB and the API.
  • useSSL (optional, default false): Whether to use SSL connection. To enable SSL connection, the configuration parameter enableHTTPS must be set to true on server.
  • usePython (optional, default false): Whether to enable Python parser.

Examples

This example creates a thread pool of size 5:

@Test
public void testDBConnectionPool() throws IOException {
    String[] highAvailabilitySites = new String[]{"192.168.1.167:18921", "192.168.1.167:18922", "192.168.1.167:18923"};
    DBConnectionPool pool = new ExclusiveDBConnectionPool("192.168.0.68", 8848, "admin", "123456", 5, true, true, highAvailabilitySites, "clearAllCache()", true, true, false);
}

Methods

The following methods are provided in the ExclusiveDBConnectionPool class:

Method

Description

ExclusiveDBConnectionPool(String host, int port, String uid, String pwd, int count, boolean loadBalance, boolean enableHighAvailability, String[] highAvailabilitySites, String initialScript, boolean compress, boolean useSSL, boolean usePython) Constructor, parameters explained above
execute(IDBTask task) Execute a task
execute(List tasks) Execute multiple tasks
getConnectionCount() Get the number of connections
shutdown() Shut down the connection pool
Note: If the current ExclusiveDBConnectionPool is no longer in use, Java API will automatically close the connection after a while. To release the connection resources, call shutdown() upon the completion of thread tasks.

BasicDBTask wraps the functions and arguments to be executed.

Method

Details

BasicDBTask(String script, List args) Constructor with a script and arguments
BasicDBTask(String script) Constructor with a script
isSuccessful() Check if the task was successfully executed
getResult() Get the result of the script execution
getErrorMsg() Get the error message if any occurred during execution

Managing Tasks

Creating a Task

Java API provides the BasicDBTask object to create a task. It encapsulates the script, arguments, execution status, and results.

Constructor

public BasicDBTask(String script)

Parameters

  • script: The script to be executed.

Examples

@Test
public void testBasicDBTask() {
    BasicDBTask task = new BasicDBTask("x=1;");
}

Submitting a Task

After creating the task, you can submit it to the connection pool using the execute method.

Parameters

  • task: Task object.
  • tasks: List of tasks for batch submission.
  • timeout (optional, default -1): Timeout in milliseconds. The default value -1 means to wait indefinitely.

Examples

This code executes a simple script task with a timeout of 10000 ms:

@Test
public void testExclusiveDBConnectionPool() throws IOException {
    ExclusiveDBConnectionPool pool = new ExclusiveDBConnectionPool("192.168.0.68",8848,"admin","123456",5,false,false);
    DBTask task = new BasicDBTask("x=1; x;");
    pool.execute(task, 10000);
    pool.shutdown();
}

Submitting Multiple Tasks

To submit multiple tasks at once, declare a List of BasicDBTask and add tasks to it. For example:
@Test
public void testExclusiveDBConnectionPool() throws IOException {
    DBConnectionPool pool = new ExclusiveDBConnectionPool("192.168.0.68",8848,"admin","123456",5,false,false);
    List<DBTask> dbTaskList = new ArrayList<>();
    dbTaskList.add(new BasicDBTask("select * from loadTable(\"dfs://test\",`tb1)"));
    dbTaskList.add(new BasicDBTask("x = 1; x;"));
    dbTaskList.add(new BasicDBTask("sum(1 2 3 NULL 4);"));
    pool.execute(dbTaskList);
    pool.shutdown();
}

Obtaining Execution Results

Use the following methods of the BasicDBTask class to retrieve task results and information:

  • boolean isSuccessful(): Returns a boolean indicating whether the task was executed successfully.
  • Entity getResult(): Returns an Entity object with the task execution result.
  • String getErrorMsg(): Returns a String with error information if any occurred.

Examples

@Test
public void testExclusiveDBConnectionPool() throws IOException {
    DBConnectionPool pool = new ExclusiveDBConnectionPool("192.168.0.68", 8848, "admin", "123456", 5, false, false);
    DBTask task = new BasicDBTask("sum(1 2 3 NULL 4);");
    pool.execute(task);

    // Print whether the task was successfully executed
    System.out.println(task.isSuccessful());

    // Get the task result
    System.out.println(task.getResult());

    // Get the task error message
    System.out.println(task.getErrorMsg());
}

Execution result:

Connect to 192.168.0.68:8848.
Connect to 192.168.0.68:8848.
Connect to 192.168.0.68:8848.
Connect to 192.168.0.68:8848.
Connect to 192.168.0.68:8848.
true
10
null

Closing Connection Pool

To shut down a connection pool, call the shutdown method:

dbConnectionPool.shutdown()

Calling shutdown() will wait for all tasks to complete before releasing the connections.