What is database connection pool?

Typically, opening a database connection is an expensive operation.
And it is unreasonable if you open a database connection then close it even when you know exactly that you or someone else have to open it again soon.
The concept of Pooling Of Database is instead of close a connection you keep it active, put it into a pool then you can take it from the pool next time

But when a connection should be closed, the pool gonna be full soon if everyone keep throwing connections into. Usually you should set up maximal number of connections for a pool. If a pool reach its limit, a connection gonna be close after done its tasks instead of being stored in the pool

How to use connection pool in python

from threading import Thread
import pymysql
from DBUtils.PooledDB import PooledDB

mySQLConnectionPool = PooledDB(creator = pymysql,

                               host= dbServerIP,

                               user= dbUserName,

                               password=dbUserPassword,

                               database=databaseToUse,

                               autocommit=True,

                               charset=charSet,

                               cursorclass=cusrorType,

                               blocking=False,

                               maxconnections=number)
mySQLConnection = mySQLConnectionPool.connection()

A nature, universe, science, music, love lover

Leave a Reply

Your email address will not be published. Required fields are marked *

Bitnami