SimpleDBConnectionPool

Java API provides the SimpleDBConnectionPool for managing and reusing connections. It provides methods for initializing and closing the pool, as well as acquiring and releasing connections.

SimpleDBConnectionPoolConfig

To use this connection pool, set parameters for SimpleDBConnectionPoolConfig, and pass it as the configuration to the constructor to initialize the connection pool. Once created, users can obtain a connection using the getConnection method. users can obtain a connection with getConnection, and release a connection with DBConnection.close(). When a connection is returned to the pool, it becomes idle and can be utilized later.

The parameters for SimpleDBConnectionPoolConfig can only be set using the setXxx methods. For example:
SimpleDBConnectionPoolConfig config = new SimpleDBConnectionPoolConfig();
        config.setHostName("1sss");
        config.setPort(PORT);
        config.setUserId("admin");
        config.setPassword("123456");
        config.setminimumPoolSize(6);
        config.setEnableHighAvailability(false);

Parameters

  • hostName (default "localhost"): IP address.
  • port (default 8848): Port number.
  • userId (default ""): Username.
  • password (default ""): Password. Only with both userId and password correctly specified can the connection log in the user. If only one of userId or password is specified, the connection will not log in the user. If the userId or password is incorrect, the connection fails.
  • initialPoolSize (default 5): Initial number of connections. Note: This parameter is deprecated since API version 3.00.1.1. Please use minimumPoolSize and maximumPoolSize to set the intial size instead.
  • minimumPoolSize (default 5): Minimum number of connections in the pool, which can be used to initalize the connection pool. Its value takes priority for initial connections, regardless of initialPoolSize. Note that if the number of connections exceeds minimumPoolSize, the excess idle connections in the pool are automatically recycled.
  • maximumPoolSize (default 5): Maximum number of connections in the pool. The value of maximumPoolSize must be no smaller than minimumPoolSize.
  • idleTimeout (default 60000): Maximum idle time (in ms) for connections before closure. Its minimum value is 10000. Idle connection recycling only applies to connections beyond minimumPoolSize.
  • 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.
  • initialScript (default ""): Script to be run during initialization.
  • compress (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 (default false): Whether to use SSL connection. To enable SSL connection, the configuration parameter enableHTTPS must be set to true on server.
  • usePython (default false): Whether to enable Python parser.
  • loadBalance (default false): Whether to enable load balancing. If true:
    • Without specifying highAvailabilitySites, the Java API will use a round-robin strategy across all nodes in the server cluster.
    • With highAvailabilitySites specified, the Java API will use a round-robin strategy across the nodes in the highAvailabilitySites array.
  • enableHighAvailability (default false): Whether to enable high availability.
  • highAvailabilitySites (default null): A list of ip:port of all available nodes for high availability.

Methods

The following methods are provided in the SimpleDBConnectionPool class:

Method

Description

Note
SimpleDBConnectionPool (SimpleDBConnectionPoolConfig) Constructor
close() Close a conection Change the connection state from active to idle.
isClosed() Check if the connection pool is closed
getActiveConnectionsCount() Get the number of active connections
getIdleConnectionsCount() Get the number of idle connections
getTotalConnectionsCount() Get the size of the pool
DBConnection.close() Release the current connection back to the pool
getConnection() Get an idle connection
  • Return an idle connection if available.
  • Create a new connection if no idle connections and pool size < maximumPoolSize.
  • Throw an exception if maximumPoolSize is reached.
manualCleanupIdleConnections() Close idle connections If the current number of connections exceeds minimumPoolSize, the excess idle connections are recycled.
Note: The DBConnection.close() method is specifically used to release a connection obtained by getConnection, which is different from close() of DBConnection that closes the current session.

Examples

// Configure the connection pool
SimpleDBConnectionPoolConfig config = new SimpleDBConnectionPoolConfig();
        config.setHostName("1sss");
        config.setPort(PORT);
        config.setUserId("admin");
        config.setPassword("123456");
        config.setInitialPoolSize(5);
        config.setEnableHighAvailability(false);
 
// Initialization
SimpleDBConnectionPool pool = new SimpleDBConnectionPool(config);

// Get a connection
DBConnection conn = pool.getConnection();
conn.run("..."); // Execute a script

// Release the connection
conn.close();

// Get the number of active connections
int activeConns = pool.getActiveConnectionsCount();

// Get the number of idle connections
int idleConns = pool.getIdleConnectionsCount();

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.