Connection Pooling

Since version 2.0: Connection pooling is now a separate optional package. Install with:

pip install mariadb[pool]

A connection pool is a cache of connections to a database server where connections can be reused for future requests. Since establishing a connection is resource-expensive and time-consuming, especially when used inside a middle tier environment which maintains multiple connections and requires connections to be immediately available on the fly.

Especially for server-side web applications, a connection pool is the standard way to maintain a pool of database connections which are reused across requests.

Version 2.0 introduces:

  • Synchronous pools with create_pool()

  • Asynchronous pools with create_async_pool()

  • Improved API with min_size and max_size parameters

  • Context manager support with acquire()

Configuring and using a connection pool

The typical way for creating and using a connection pool is:

  1. Create (and configure) a connection pool

  2. Obtain a connection from connection pool using acquire()

  3. Perform database operation(s)

  4. Return the connection to the pool (automatically with context managers)

Synchronous Connection Pool

Since version 2.0

Create a synchronous connection pool using create_pool():

Pool Configuration Parameters:

  • min_size (int) - Minimum number of connections in the pool. Default: same than max_size

  • max_size (int) - Maximum number of connections in the pool. Default: 10

  • max_idle_time (float) - Maximum time (seconds) a connection can be idle before being closed. Default: 600.0 (10 minutes)

  • max_lifetime (float) - Maximum lifetime (seconds) of a connection before being replaced. Default: 3600.0 (1 hour)

  • validation_interval (float) - Interval (seconds) between health checks. Default: 30.0

  • acquire_timeout (float) - Timeout (seconds) when acquiring a connection from the pool. Default: 30.0

  • enable_health_check (bool) - Enable periodic health checks on pooled connections. Default: True

  • reset_connection (bool) - Reset connection state when returning to pool (clears session variables, temporary tables, and prepared statements). Default: False

  • ping_threshold (float) - Ping connection if idle for more than this many seconds (0 = disabled). Default: 0.25

Connection Release Behavior:

When a connection is returned to the pool (either explicitly or via context manager), the pool automatically handles cleanup:

  1. If reset_connection=True: Calls conn.reset() to clear all session state (session variables, temporary tables, prepared statements) without reconnecting. This ensures a clean state for the next user but adds overhead.

  2. If reset_connection=False (default): Checks if the connection has an active transaction. If a transaction is in progress, it automatically calls conn.rollback() to prevent transaction leakage between pool users.

Best Practices:

  • Use reset_connection=True if you need guaranteed clean state (e.g., different users sharing a pool with session-specific settings)

  • Use reset_connection=False (default) for better performance when session state doesn't matter

  • Always commit or rollback transactions explicitly before releasing connections for clarity

Connection Parameters:

  • All connection parameters from mariadb.connect() are supported (host, user, password, database, ssl_ca, etc.)

Example - Synchronous Pool:

Example - URI Connection:

Asynchronous Connection Pool

Since version 2.0

Create an asynchronous connection pool for async/await applications:

FastAPI Example

Migration from Version 1.1

Version 1.1:

Version 2.0:

spinner

Last updated

Was this helpful?