A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://mariadb.com/docs/connectors/mariadb-connector-python/pooling below:

Connection Pooling | MariaDB Documentation

Connection Pooling | MariaDB Documentation
  1. Connector/Python
Connection Pooling

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.

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

  3. Perform database operation(s)

  4. Close the connection instance and return it to the connection pool.

Creating a connection pool

When creating a connection pool, the following parameters have to be provided:

  1. Connection pool specific parameters

Example:

import mariadb

# connection parameters
conn_params= {
  "user" : "example_user",
  "password" : "GHbe_Su3B8",
  "database" : "test"
}

# create new pool
with mariadb.ConnectionPool(pool_name="myfirstpool", pool_size=5, **conn_params) as pool:
    print("Pool size of '%s': %s" % (pool.pool_name, pool.pool_size))

    # get a connection from pool
    with pool.get_connection() as conn:

        # print the default database for connection
        print("Current database: %s" % conn.database)

Output:

Pool size of 'myfirstpool': 5
Current database: test

This page is covered by the Creative Commons Attribution 3.0 license .


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4