ConnectionPool()
This page is part of MariaDB's Enterprise Documentation.
The parent of this page is: Connector/Python API
Topics on this page:
Overview
Create a connection pool and returns a connection pool object.
See also: MariaDB Connector/Python 1.1 and in 1.0
DETAILS
A connection pool holds a number of open connections and handles thread safety when providing connections to threads.
The size of a connection pool is configurable at creation time, but cannot be changed afterwards. The maximum size of a connection pool is limited to 64 connections.
Threads using the pool fetch a connection via get_connection() and return it to the pool by calling close() on the connection (or using the default close on the object's destruction).
The connection parameters include all the keyword parameters that can be used with connect() as well as a few pool-specific parameters:
pool_name
The name of the pool.
pool_size
The number of connections that the pool can hold. The default when omitted is 5.
pool_reset_connection
When set to True the connection is reset when close() is called (instead of just being returned to the pool without changes).
EXAMPLES
The following will create a connection pool object, then fetch a connection, use it, and return it to the pool:
import mariadb
pool = mariadb.ConnectionPool(
pool_name = 'pool1',
pool_size = 3,
pool_reset_connection = False,
host = '127.0.0.1',
user = 'root',
password = 'secret',
database = 'test',
)
# These would normally be allocated in separate threads:
conn1 = pool.get_connection()
conn2 = pool.get_connection()
conn3 = pool.get_connection()
# Attempting to fetch a 4th connection would throw an exception
# given the pool_size == 3 option above.
cursor = conn1.cursor()
cursor.execute('INSERT INTO sample VALUES (?, ?)', (1, 2))
cursor = conn2.cursor()
cursor.execute('INSERT INTO sample VALUES (?, ?)', (3, 4))
cursor = conn3.cursor()
cursor.execute('INSERT INTO sample VALUES (?, ?)', (5, 6))
conn1.close()
conn2.close()
conn3.close()
CHANGE HISTORY
Release Series | History |
---|---|
1.1 |
|
1.0 |
|