DBConnection
DBConnection (supported since Python API 3.0.4.0) represents a single
underlying connection between the client and the DolphinDB server, used to execute
scripts, call functions, and upload variables.
DBConnection is recommended for single-connection scenarios, while
SimpleDBConnectionPool should be used for multithreaded
environments. The legacy Session interface is retained for backward
compatibility (see "Migrating from Session to DBConnection" for details).
Note: Instantiating a DBConnection object does not automatically
establish a connection to the server. You must call connect() before
performing any operations.
import dolphindb as ddb
conn = ddb.DBConnection(protocol="ddb")
try:
connected = conn.connect(
host="localhost",
port=8848,
userid="admin",
password="your_password",
)
if not connected:
raise RuntimeError("Failed to connect to DolphinDB")
print(conn.exec("1 + 1"))
# output: 2
finally:
conn.close()
Select the appropriate connection interface based on your use case:
| Use Case | Recommended Interface |
|---|---|
| Execute scripts, call functions, or upload variables on a single connection | DBConnection |
Operate DolphinDB databases and tables directly through Python
objects, such as calling s.table(),
s.loadTable(), or s.database(), or
use the built-in stream subscription method
s.subscribe() |
Session |
| Reuse connections in multiple threads and manually acquire and return them | SimpleDBConnectionPool |
Note: To focus on the core operations, the following examples assume that a
connection has been established through conn. In production, use a
try...finally block and explicitly call
conn.close() to ensure that network connections and system
resources are released properly.
