IBM Db2¶
sqlspec.adapters.db2 supports IBM Db2 LUW 11.5+ via ibm_db (>= 3.3.0), offering both synchronous (Db2SyncConfig) and asyncio (Db2AsyncConfig) managed connection pools, custom SQLGlot db2 dialect transpilation, automatic lowercase column normalization, and Litestar, Events, and Google ADK session/memory store extensions.
Overview & Capabilities¶
| Adapter Config | Driver Extra | Execution | Pool Architecture | Best For |
|---|---|---|---|---|
Db2AsyncConfig |
sqlspec[db2] |
Async (asyncio) |
Db2AsyncConnectionPool (max_size, acquire_timeout, pool_recycle_seconds, health_check_interval) |
Async HTTP services and ADK agents communicating with IBM Db2 over DRDA. |
Db2SyncConfig |
sqlspec[db2] |
Sync | Db2SyncConnectionPool (thread-local manager with pool_recycle_seconds, health_check_interval) |
Synchronous applications, batch jobs, and CLI migrations targeting IBM Db2 LUW. |
[!NOTE] IBM publishes prebuilt
clidriverwheels for Linuxx86_64, macOSarm64, and Windows. On Linuxaarch64hosts, connect to Db2 viaadbc(adbc-driver-db2pure-Go DRDA wheel) or pairarrow_odbc(dialect="db2"over 64-bitlibdb2o.so).
Installation & Quickstart¶
from sqlspec import SQLSpec
from sqlspec.adapters.db2 import Db2AsyncConfig
spec = SQLSpec()
config = spec.add_config(
Db2AsyncConfig(
connection_config={
"database": "appdb",
"hostname": "localhost",
"port": 50000,
"user": "db2inst1",
"password": "secret",
"current_schema": "APP",
"max_size": 10,
"acquire_timeout": 30.0,
}
)
)
Connection & Pool Configuration¶
Connection and Pool Parameters¶
Pass Db2ConnectionParams, Db2PoolParams, and Db2AsyncPoolParams keys inside connection_config={...} on Db2SyncConfig or Db2AsyncConfig:
| Key | Type | Default | Description |
|---|---|---|---|
database |
str |
- | Database name or cataloged alias mapped to the DATABASE CLI keyword. |
hostname |
str |
- | Db2 server hostname mapped to HOSTNAME (omit for cataloged local aliases). |
port |
int |
50000 |
Db2 TCP listener port mapped to PORT (defaults to 50000 when hostname is set). |
protocol |
str |
'TCPIP' |
Communications protocol mapped to PROTOCOL (defaults to 'TCPIP' when hostname is set). |
user |
str |
- | Authentication username mapped to the UID CLI keyword. |
password |
str |
- | Authentication password mapped to the PWD CLI keyword. |
current_schema |
str |
- | Default SQL schema mapped to the CURRENTSCHEMA CLI keyword. |
security |
str |
- | Transport security mode mapped to SECURITY (for example 'SSL'). |
ssl_server_certificate |
str |
- | Server certificate file path mapped to the SSLSERVERCERTIFICATE CLI keyword. |
authentication |
str |
- | Db2 authentication mechanism mapped to the AUTHENTICATION CLI keyword. |
connect_timeout |
int |
- | Connection establishment timeout in seconds mapped to CONNECTTIMEOUT. |
autocommit |
bool |
- | Initial connection autocommit state applied via ibm_db_dbi outside the DSN. |
dsn |
str |
- | Base KEY=VALUE;... string or db2:// URL overridden by explicit keyword parameters. |
extra |
dict[str, str or int or bool] |
- | Additional CLI keywords appended verbatim to the compiled DSN string. |
pool_recycle_seconds |
int |
86400 |
Maximum connection age in seconds before replacement (defaults to 86400). |
health_check_interval |
float |
30.0 |
Idle seconds elapsed before a pooled connection is pinged on reuse (defaults to 30.0). |
max_size |
int |
10 |
Maximum number of connections checked out or opening concurrently (defaults to 10). |
acquire_timeout |
float |
30.0 |
Maximum seconds to wait for a free connection before raising ConnectionTimeoutError (defaults to 30.0). |
Driver Features¶
Pass Db2DriverFeatures keys inside driver_features={...} on Db2SyncConfig or Db2AsyncConfig:
| Key | Type | Default | Description |
|---|---|---|---|
json_serializer |
Callable[[Any], str] |
sqlspec.utils.serializers.to_json |
Serializer converting dict/list parameters to JSON text (defaults to sqlspec.utils.serializers.to_json). |
json_deserializer |
Callable[[str], Any] |
sqlspec.utils.serializers.from_json |
Deserializer parsing JSON text payloads (defaults to sqlspec.utils.serializers.from_json). |
on_connection_create |
Callable[[Any], Any] |
- | Callback invoked with the raw ibm_db_dbi sync or async connection after open. |
enable_events |
bool |
- | Enable SQLSpec table-backed event channel support. |
events_backend |
Literal['poll_queue'] |
- | Event queue backend name ('poll_queue'). |
enable_lowercase_column_names |
bool |
True |
Normalize unquoted uppercase Db2 column names to lowercase (defaults to True). |
Type Mappings & Dialect Notes¶
| Database Type | Python Type | Notes / Feature Flag |
|---|---|---|
CLOB / VARCHAR (JSON) |
dict / list |
Serialized and deserialized via json_serializer and json_deserializer. |
DECIMAL / DECFLOAT |
decimal.Decimal |
Exact arbitrary-precision decimal arithmetic. |
TIMESTAMP / DATE |
datetime.datetime / datetime.date |
Native Db2 timestamp and date conversion. |
BLOB / VARBINARY |
bytes / bytearray |
Binary payload storage. |
BOOLEAN / SMALLINT |
bool / int |
Native Db2 BOOLEAN and 0/1 integer flags. |
VARCHAR(36) / CHAR(16) FOR BIT DATA |
uuid.UUID / str |
UUID string and binary parameter coercion. |
- Data-Change-Table Returning Syntax (
FINAL TABLE): Db2 compilesINSERT ... RETURNINGprojections usingSELECT ... FROM FINAL TABLE (INSERT INTO ...)syntax via SQLSpec's customdb2SQLGlot dialect, and supports standardMERGE INTOupserts. - Strict Connection Key Validation:
Db2SyncConfigandDb2AsyncConfigvalidateconnection_configkeys eagerly, raisingImproperConfigurationErrorif unknown aliases such ashost,username, ormin_sizeare passed instead ofhostname,user, andmax_size. - Savepoint Cursor Retention: Db2 closes open cursors on savepoint rollback unless
ON ROLLBACK RETAIN CURSORSis specified; SQLSpec emitsSAVEPOINT <name> ON ROLLBACK RETAIN CURSORSandRELEASE SAVEPOINT <name>automatically. - Event Queue and ADK Stores:
Db2SyncEventQueueStoreandDb2AsyncEventQueueStoreclaim rows withFETCH FIRST n ROWS ONLYandWITH RS USE AND KEEP UPDATE LOCKS SKIP LOCKED DATAso concurrent queue workers skip rows that are already claimed.Db2SyncADKMemoryStoreandDb2AsyncADKMemoryStorematch text withLOCATE(LOWER(?), LOWER(content_text)) > 0for case-insensitive search.