Skip to main content

Advanced connection pooling for MariaDB Connector/Python

Project description

mariadb-pool

mariadb-pool is the connection pooling add-on for the mariadb connector. It provides synchronous and asynchronous connection pools with automatic connection management, health checking, and monitoring.

Installation

Install MariaDB Connector/Python with pool support:

pip install mariadb[pool]

Or install the pool package separately:

pip install mariadb-pool

Features

  • Easy Connection Management: Automatic connection pooling via mariadb.ConnectionPool
  • URI Support: Create pools with connection URIs for clean configuration
  • Dynamic Pool Sizing: Automatically adjusts between min and max connections
  • Connection Health Checking: Periodic validation of idle connections
  • Thread-Safe: Fully thread-safe for multi-threaded applications
  • Pool Statistics: Monitor pool usage and performance
  • Context Manager Support: Clean connection handling with with statements

Quick Start

Synchronous Connection Pool

Using create_pool() (Recommended)

The cleanest way to create a connection pool with separated pool and connection parameters:

import mariadb

# Create pool with clean parameter separation
pool = mariadb.create_pool(
    host="localhost",
    port=3306,
    user="root",
    password="password",
    database="mydb",
    min_size=5,
    max_size=10,
    ping_threshold=0.25,  # Only ping connections idle > 250ms
    max_idle_time=600.0,
    reset_connection=True
)

# Get connection from pool
with pool.acquire() as conn:
    with conn.cursor() as cursor:
        cursor.execute("SELECT * FROM users")
        results = cursor.fetchall()

# Close pool when done
pool.close()

Using ConnectionPool Constructor

Alternatively, use the traditional constructor:

import mariadb

# Create pool with connection parameters
pool = mariadb.ConnectionPool(
    pool_name="mypool",
    host="localhost",
    port=3306,
    user="root",
    password="password",
    database="mydb",
    max_size=10,
    pool_reset_connection=True
)

# Use context manager to ensure connection is returned to pool
with pool.get_connection() as conn:
    with conn.cursor() as cursor:
        cursor.execute("SELECT * FROM users")
        results = cursor.fetchall()

pool.close()

Asynchronous Connection Pool

Using create_async_pool() (Recommended)

The cleanest way to create an async pool - automatically pre-filled and ready to use:

import mariadb
import asyncio

async def main():
    # Create async pool with clean parameter separation
    # Pool is automatically opened and pre-filled with connections
    pool = await mariadb.create_async_pool(
        host="localhost",
        port=3306,
        user="root",
        password="password",
        database="mydb",
        min_size=5,
        max_size=20,
        ping_threshold=0.25,  # Only ping connections idle > 250ms
        max_idle_time=600.0,
        reset_connection=True
    )
    
    # Acquire connection from pool
    async with await pool.acquire() as conn:
        async with conn.cursor() as cursor:
            await cursor.execute("SELECT * FROM users")
            results = await cursor.fetchall()
    
    # Close pool if unneeded anymore
    await pool.close()

asyncio.run(main())

Pool Configuration Options

The pool accepts the following configuration parameters:

Pool-Specific Parameters

Parameter Type Default Description
min_size int 10 Minimum number of connections to maintain
max_size int 10 Maximum number of connections allowed (max: 64)
max_idle_time float 600.0 Maximum idle time (seconds) before closing connection
max_lifetime float 3600.0 Maximum lifetime (seconds) of a connection
validation_interval float 30.0 Interval (seconds) between connection validations
acquire_timeout float 30.0 Timeout (seconds) when acquiring a connection
enable_health_check bool True Enable periodic health checks on idle connections
reset_connection bool False Reset connection state when returned to pool
ping_threshold float 0.25 Only ping connections idle > threshold seconds (0 = always ping)

Performance Tip: The ping_threshold option significantly improves performance by avoiding unnecessary database pings. Connections that have been idle for less than the threshold (default: 250ms) are assumed healthy and returned immediately. Only connections idle longer than the threshold are verified with a ping. Set to 0 to ensure checking connection states.

Note: If you set only min_size or only max_size (without setting the other), both will be set to the same value to create a fixed-size pool.

mariadb.ConnectionPool Parameters

Additional parameters supported by mariadb.ConnectionPool:

Parameter Type Default Description
pool_name str Required Unique name for the pool
pool_reset_connection bool True Alias for reset_connection (mapped by wrapper)
pool_validation_interval float - Alias for validation_interval (mapped by wrapper)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mariadb_pool-2.0.0rc2.tar.gz (11.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mariadb_pool-2.0.0rc2-py3-none-any.whl (34.7 kB view details)

Uploaded Python 3

File details

Details for the file mariadb_pool-2.0.0rc2.tar.gz.

File metadata

  • Download URL: mariadb_pool-2.0.0rc2.tar.gz
  • Upload date:
  • Size: 11.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for mariadb_pool-2.0.0rc2.tar.gz
Algorithm Hash digest
SHA256 1a70946c87647e1eb607585ceba82790c6697c6c8b2edff398c2e2b699a6984a
MD5 1bd6d8648b7bae94393c4beb1e82ea83
BLAKE2b-256 51a47b1956b0f65240f229ed93f865b9ae0fca101939739e53e850250d037f6f

See more details on using hashes here.

Provenance

The following attestation bundles were made for mariadb_pool-2.0.0rc2.tar.gz:

Publisher: release.yml on mariadb-corporation/mariadb-connector-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file mariadb_pool-2.0.0rc2-py3-none-any.whl.

File metadata

File hashes

Hashes for mariadb_pool-2.0.0rc2-py3-none-any.whl
Algorithm Hash digest
SHA256 530d10579939f20472d79cc762772a84fdbb061cdf4c66e9ccd9a774b7f05f56
MD5 44def97762f8f24b0b6fab4196bae86b
BLAKE2b-256 2090e5fe0d92f3b6c67d1d307809924ce9e0c7daa3969f2dc8957b9ead0ae7a7

See more details on using hashes here.

Provenance

The following attestation bundles were made for mariadb_pool-2.0.0rc2-py3-none-any.whl:

Publisher: release.yml on mariadb-corporation/mariadb-connector-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page