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_sizeandmax_sizeparametersContext manager support with
acquire()
Configuring and using a connection pool
The typical way for creating and using a connection pool is:
Create (and configure) a connection pool
Obtain a connection from connection pool using
acquire()Perform database operation(s)
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_sizemax_size(int) - Maximum number of connections in the pool. Default: 10max_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.0acquire_timeout(float) - Timeout (seconds) when acquiring a connection from the pool. Default: 30.0enable_health_check(bool) - Enable periodic health checks on pooled connections. Default: Truereset_connection(bool) - Reset connection state when returning to pool (clears session variables, temporary tables, and prepared statements). Default: Falseping_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:
If
reset_connection=True: Callsconn.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.If
reset_connection=False(default): Checks if the connection has an active transaction. If a transaction is in progress, it automatically callsconn.rollback()to prevent transaction leakage between pool users.
Best Practices:
Use
reset_connection=Trueif 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 matterAlways 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:
Last updated
Was this helpful?

