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/ADK stores.
Install and When to Choose¶
- Choose
Db2SyncConfigorDb2AsyncConfigfor transactional OLTP workloads over IBM's nativeclidriver(ibm_db/ibm_db_dbi). - Note on Linux
aarch64: IBM publishes prebuiltclidriverwheels for Linuxx86_64, macOSarm64, and Windows. On Linux ARM64 hosts, connect to Db2 viaadbc(adbc-driver-db2pure-Go DRDA wheel) or guardibm-dbwith environment markers. - For high-volume columnar Arrow extracts on
x86_64, you can also pairarrow_odbc(dialect="db2"over 64-bitlibdb2o.so).
Minimal Connection and Pool Setup¶
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,
}
)
)
Key Driver Features and Pool Tuning¶
SQLSpec provides Db2SyncConnectionPool and Db2AsyncConnectionPool with health-check pings and recycle intervals rather than relying on ibm_db.pconnect (which can retain dropped SQL0900N sockets):
| Setting | Location | Description |
|---|---|---|
dsn / database, hostname, port, protocol |
connection_config |
Full DRDA DSN (DATABASE=...;HOSTNAME=...;PORT=...;) or validated discrete connection keys (note hostname and user, not host or username). |
current_schema |
connection_config |
Default Db2 schema applied to unqualified table references (CURRENT SCHEMA). |
security / ssl_server_certificate |
connection_config |
Enables TLS transport (security="SSL") and server CA certificate verification. |
max_size / acquire_timeout |
connection_config |
Pool concurrency cap and checkout timeout in seconds. |
health_check_interval / pool_recycle_seconds |
connection_config |
Idle liveness probe interval (VALUES (1)) and maximum connection age before rotation. |
enable_lowercase_column_names |
driver_features |
Converts Db2's default uppercase unquoted column identifiers (USER_ID) to lowercase (user_id) (True by default). |
json_serializer / json_deserializer |
driver_features |
Customizes JSON text/CLOB serialization callables. |
See Db2 API Reference for complete Db2ConnectionParams, Db2PoolParams, and Db2DriverFeatures TypedDict tables.
Type Mapping and Dialect Notes¶
- 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. - Schema-Guided Arrow Export:
select_to_arrowconverts fetched rows using cursorcolumn_typesmetadata so even empty or all-NULLcolumns preserve exact PyArrow types. - SQLCODE and SQLSTATE Normalization: Db2 error codes (
-803unique,-530/-532foreign key,-407not null,-911deadlock/lock timeout,-952query timeout,-551permission denied,-104/-204syntax/undefined object) map into SQLSpec's unified exception hierarchy. - Events and ADK Stores:
Db2EventQueueStoreusesFETCH FIRST n ROWS ONLYwithFOR UPDATE WITH RS SKIP LOCKED DATAfor lock-free concurrent queue workers, andDb2ADKMemoryStoreusesPOSSTR(LOWER(content_text), LOWER(?)) > 0for case-insensitive text search.