DBUtils is a suite of Python modules providing robust, persistent, and pooled connections to a database, designed for multi-threaded environments. It supports DB-API 2 compliant database interfaces and the classic PyGreSQL interface. The current version, 3.1.2, is actively maintained and supports Python versions 3.7 to 3.14.
pip install DBUtilsVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to set up and use `PooledDB` with an in-memory SQLite database. Replace `sqlite3.connect` with your specific DB-API 2 compliant database connector (e.g., `psycopg2.connect`) and provide appropriate connection arguments for your database.
Consult the official DBUtils changelog and documentation for migration guidance when upgrading from pre-2.0 versions.
For production environments, always use `dbutils.pooled_db.PooledDB` or `dbutils.persistent_db.PersistentDB` for robust connection management.
Ensure the appropriate database driver is installed via pip (e.g., `pip install psycopg2-binary`) and imported into your application.
Ensure the `DBUtils` package is installed via `pip install DBUtils`. Then, import specific components with correct casing, for example: `from DBUtils.PooledDB import PooledDB`.
Upgrade `DBUtils` to version 2.0.2 or newer, which added `__enter__` and `__exit__` methods to connection objects. Alternatively, for older versions, use `contextlib.closing` or explicitly call `connection.close()` in a `finally` block.
Increase the `maxconnections` parameter in the `PooledDB` constructor, ensure that database connections are properly closed and returned to the pool after use, or set `blocking=True` to make connection requests wait until a connection becomes available.
Verify that the database server is running and accessible, and that all connection parameters (host, port, user, password, database) passed to your `creator` function (e.g., `pymysql.connect`, `psycopg2.connect`) are correct. Ensure the `creator` function returns a valid DB-API 2 compliant connection object.