{"id":2465,"library":"dbutils","title":"DBUtils","description":"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.","status":"active","version":"3.1.2","language":"en","source_language":"en","source_url":"https://github.com/WebwareForPython/DBUtils","tags":["database","connection-pool","multi-threading","db-api","pooling","thread-safety"],"install":[{"cmd":"pip install DBUtils","lang":"bash","label":"Install DBUtils"}],"dependencies":[{"reason":"DBUtils requires a separate database driver to connect to a specific database. It does not include one itself.","package":"Any DB-API 2 compliant database driver (e.g., psycopg2, mysql-connector-python, cx_Oracle) or PyGreSQL","optional":false}],"imports":[{"symbol":"PooledDB","correct":"from dbutils.pooled_db import PooledDB"},{"symbol":"PersistentDB","correct":"from dbutils.persistent_db import PersistentDB"},{"note":"Not recommended for production use; intended as a basic reference implementation.","symbol":"SimplePooledDB","correct":"from dbutils.simple_pooled_db import SimplePooledDB"}],"quickstart":{"code":"import sqlite3\nfrom dbutils.pooled_db import PooledDB\n\n# This example uses sqlite3 which is part of Python's standard library.\n# For other databases, replace sqlite3.connect with your actual DB-API 2 connect function,\n# e.g., import psycopg2; db_module = psycopg2\n\n# Create a pool of connections\n# mincached: Minimum number of connections to keep in the pool\n# maxcached: Maximum number of connections to keep in the pool\n# maxconnections: Maximum number of connections to create in total\n# blocking: Whether to block if maxconnections is reached (True) or raise an error (False)\n# creator: The DB-API 2 module's connect function\npool = PooledDB(\n    creator=sqlite3.connect,\n    database=\":memory:\", # Use an in-memory SQLite database for the example\n    mincached=2,\n    maxcached=5,\n    maxconnections=10,\n    blocking=True\n)\n\ndef get_data_from_db(pool_instance):\n    conn = None\n    cursor = None\n    try:\n        # Get a connection from the pool\n        conn = pool_instance.connection()\n        cursor = conn.cursor()\n        cursor.execute(\"CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT)\")\n        cursor.execute(\"INSERT INTO users (name) VALUES ('Alice')\")\n        conn.commit()\n        \n        cursor.execute(\"SELECT * FROM users\")\n        rows = cursor.fetchall()\n        print(f\"Fetched users: {rows}\")\n        return rows\n    except Exception as e:\n        print(f\"An error occurred: {e}\")\n        if conn: conn.rollback()\n    finally:\n        # Return the connection to the pool\n        if conn: conn.close()\n\nif __name__ == \"__main__\":\n    print(\"Running quickstart example for DBUtils.PooledDB\")\n    get_data_from_db(pool)\n    get_data_from_db(pool) # Get data again, should reuse pooled connections\n    print(\"Example finished.\")","lang":"python","description":"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."},"warnings":[{"fix":"Consult the official DBUtils changelog and documentation for migration guidance when upgrading from pre-2.0 versions.","message":"Version 2.0 introduced significant breaking changes. Users upgrading from versions 1.x should carefully review the changelog to understand necessary modifications to their code.","severity":"breaking","affected_versions":"2.0.0 and above"},{"fix":"For production environments, always use `dbutils.pooled_db.PooledDB` or `dbutils.persistent_db.PersistentDB` for robust connection management.","message":"The `dbutils.simple_pooled_db.SimplePooledDB` class is provided as a basic reference implementation and is explicitly NOT recommended for production use due to lacking sophisticated features like failover.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Ensure the appropriate database driver is installed via pip (e.g., `pip install psycopg2-binary`) and imported into your application.","message":"DBUtils itself does not include a database driver. You must separately install a DB-API 2 compliant driver for your specific database (e.g., `psycopg2` for PostgreSQL, `mysql-connector-python` for MySQL, `cx_Oracle` for Oracle) and pass its `connect` function to `PooledDB` or `PersistentDB`.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-10T00:00:00.000Z","next_check":"2026-07-09T00:00:00.000Z"}