|
| 1 | +# Constructing a DBConnectionPool |
| 2 | + |
| 3 | +As introduced previously, Sessions enable you to communicate with the DolphinDB server. However, scripts can only be executed sequentially within a single session using the `run` method. Multi-threading is not allowed within the same session. |
| 4 | + |
| 5 | +If you want to execute scripts concurrently, it is recommended to use the DBConnectionPool class, which enables concurrent task execution by maintaining multiple threads. |
| 6 | + |
| 7 | +The following script creates a DBConnectionPool object with default parameter values: |
| 8 | + |
| 9 | +``` |
| 10 | +DBConnectionPool(host, port, threadNum=10, userid=None, password=None, |
| 11 | + loadBalance=False, highAvailability=False, compress=False, |
| 12 | + reConnect=False, python=False, protocol=PROTOCOL_DEFAULT) |
| 13 | +``` |
| 14 | + |
| 15 | +You can obtain the ID of all the sessions created by the DBConnectionPool with its method `getSessionId()`. Connections will be released when DBConnectionPool is deconstructed. |
| 16 | + |
| 17 | +In the following sections, we will explain the parameters in detail. |
| 18 | + |
| 19 | +## *host*, *port*, *threadNum*, *userid*, *password* |
| 20 | + |
| 21 | +- **host:** *required*. The server address to connect to. |
| 22 | +- **port:** *required.* The port number of the server. |
| 23 | +- **threadNum:** *int, optional, default 10.* The number of connections to create. |
| 24 | +- **userid:** *optional.* The username for server login. |
| 25 | +- **password:** *optional.* The password for server login. |
| 26 | + |
| 27 | +Connect DBConnectionPool to a DolphinDB server by specifying the domain/IP address and the port number. You can also log in to the server by specifying the user credentials. |
| 28 | + |
| 29 | +**Example** |
| 30 | + |
| 31 | +``` |
| 32 | +import dolphindb as ddb |
| 33 | +
|
| 34 | +# Create 10 connections to the local DolphinDB server |
| 35 | +pool = ddb.DBConnectionPool("localhost", 8848) |
| 36 | +
|
| 37 | +# Create 8 connections to the local DolphinDB server and log in as the user "admin" |
| 38 | +pool = ddb.DBConnectionPool("localhost", 8848, 8, "admin", "123456") |
| 39 | +``` |
| 40 | + |
| 41 | +**Note** |
| 42 | + |
| 43 | +- If the connectivity parameters are incorrect, the DBConnectionPool object will not be created and the connection will fail. |
| 44 | + |
| 45 | +## *loadBalance* |
| 46 | + |
| 47 | +*bool, default False.* Whether to enable load balancing when connecting to the DolphinDB server. To enable load balancing, specify this parameter as True. |
| 48 | + |
| 49 | +**Example** |
| 50 | + |
| 51 | +``` |
| 52 | +import dolphindb as ddb |
| 53 | +
|
| 54 | +# create DBConnectionPool with load balancing enabled |
| 55 | +pool = ddb.DBConnectionPool("localhost", 8848, 8, loadBalance=True) |
| 56 | +``` |
| 57 | + |
| 58 | +**Note that in load balancing mode:** |
| 59 | + |
| 60 | +- if API high availability (HA) mode is enabled, all data nodes and compute nodes in the cluster are available for connection. In this case, *loadBalance* does not take effect. |
| 61 | +- if API HA mode is disabled, DBConnectionPool will evenly distribute connections to all available nodes. For example, there are 3 nodes in the cluster and each has \[5, 12, 13] connections, respectively. A DBConnectionPool needs to establish 6 connections. As a result, connections for each node is increased by 2, and the final connections on each node would be \[7, 14, 15], respectively. |
| 62 | + |
| 63 | +## *highAvailability* |
| 64 | + |
| 65 | +*bool, default False.* Whether to enable high availability on all cluster nodes. |
| 66 | + |
| 67 | +**Note:** In API HA mode, if load balancing is disabled, DBConnectionPool will connect to the cluster node with the minimum load. As all the connections are created at the same time, they will all be added to the same node with the lowest load at the time, causing load imbalance. |
| 68 | + |
| 69 | +**Example** |
| 70 | + |
| 71 | +``` |
| 72 | +import dolphindb as ddb |
| 73 | +
|
| 74 | +# create DBConnectionPool with HA mode enabled. all data/compute nodes are available for connection. |
| 75 | +pool = ddb.DBConnectionPool("localhost", 8848, 8, "admin", "123456", highAvailability=True) |
| 76 | +``` |
| 77 | + |
| 78 | +## *compress* |
| 79 | + |
| 80 | +*bool, default False.* Whether to enable compressed communication on the connections. |
| 81 | + |
| 82 | +Compressed communication can be a useful option when writing or querying large amounts of data. When *compress* is set to True, the data will be compressed before being transferred, which can reduce bandwidth usage. Note that it increases the computational complexity on the server and API client, which may impact performance. |
| 83 | + |
| 84 | +**Note** |
| 85 | + |
| 86 | +- The *compress* parameter is supported on DolphinDB server since version 1.30.6. |
| 87 | +- Compress communication is supported only when *protocol* = PROTOCOL_DDB. (In API versions 1.30.19.4 and below, *compress* is always supported because these versions use PROTOCOL_DDB by default. ) |
| 88 | + |
| 89 | +**Example** |
| 90 | + |
| 91 | +``` |
| 92 | +import dolphindb as ddb |
| 93 | +import dolphindb.settings as keys |
| 94 | +
|
| 95 | +# when API version >= 1.30.21.1, specify PROTOCOL_DDB to enable compression |
| 96 | +pool = ddb.DBConnectionPool("localhost", 8848, 8, compress=True, protocol=keys.PROTOCOL_DDB) |
| 97 | +
|
| 98 | +# when API version <= 1.30.19.4, PROTOCOL_DDB is used by default, i.e., enablePickle False |
| 99 | +pool = ddb.DBConnectionPool("localhost", 8848, 8, compress=True) |
| 100 | +``` |
| 101 | + |
| 102 | +## *reconnect* |
| 103 | + |
| 104 | +*bool, default False.* Whether to reconnect if the API detects a connection exception when HA mode is disabled. |
| 105 | + |
| 106 | +When HA mode is enabled, the system automatically reconnects when connection exception is detected, and it is not required to specify *reconnect*. Otherwise, set *reconnect* to True for auto reconnection. |
| 107 | + |
| 108 | +``` |
| 109 | +import dolphindb as ddb |
| 110 | +
|
| 111 | +# create DBConnectionPool with auto reconnection |
| 112 | +pool = ddb.DBConnectionPool("localhost", 8848, 8, reconnect=True) |
| 113 | +``` |
| 114 | + |
| 115 | +## *protocol* |
| 116 | + |
| 117 | +*protocol name, default PROTOCOL_DEFAULT (equivalent to PROTOCOL_PICKLE).* The protocol for object serialization in communication between the API client and the DolphinDB server. |
| 118 | + |
| 119 | +Currently, the protocols PROTOCOL_DDB, PROTOCOL_PICKLE, and PROTOCOL_ARROW are supported. Protocols determine the format of data returned after running DolphinDB scripts through Python API. For more information about the protocols, see [Data Type Conversion](../../AdvancedOperations/DataTypeCasting/TypeCasting.md). |
| 120 | + |
| 121 | +``` |
| 122 | +import dolphindb.settings as keys |
| 123 | +
|
| 124 | +# use PROTOCOL_DDB |
| 125 | +pool = ddb.DBConnectionPool("localhost", 8848, 10, protocol=keys.PROTOCOL_DDB) |
| 126 | +
|
| 127 | +# use PROTOCOL_PICKLE |
| 128 | +pool = ddb.DBConnectionPool("localhost", 8848, 10, protocol=keys.PROTOCOL_PICKLE) |
| 129 | +
|
| 130 | +# use PROTOCOL_ARROW |
| 131 | +pool = ddb.DBConnectionPool("localhost", 8848, 10, protocol=keys.PROTOCOL_ARROW) |
| 132 | +``` |
| 133 | + |
| 134 | +**Note:** Starting from 1.30.21.1, the *protocol* parameter is provided for specifying the object serialization protocol. Prior to 1.30.19.4, PROTOCOL_DDB is used by default, i.e., *enablePickle* = False. |
| 135 | + |
| 136 | +## *python* |
| 137 | + |
| 138 | +*bool, default False.* Whether to enable the Python Parser feature when running scripts with `DBConnectionPool.run`. |
| 139 | + |
| 140 | +**Example** |
| 141 | + |
| 142 | +``` |
| 143 | +import dolphindb as ddb |
| 144 | +
|
| 145 | +# enable python parser |
| 146 | +pool = ddb.DBConnectionPool("localhost", 8848, 10, python=True) |
| 147 | +``` |
| 148 | + |
| 149 | +**Note:** This parameter will be supported in the upcoming version 2.10 of the DolphinDB server. |
0 commit comments