DBUtils of python common library (connection pool connection database)

DBUtils of python common library (connection pool connection database)

What is DBUtils

Official website: https://webwareforpython.github.io/DBUtils/
Official pip: https://pypi.org/search/?q=DBUtils

Dbutils is a suite of tools that provides reliable, persistent and aggregated connections to databases that can be used in a variety of multithreaded environments.

The suite supports both DB-API 2 compliant database interfaces and classic PygresQL interfaces.

The current version 3.0.3 of Dbutils supports Python versions 3.6 to 3.11.

scenes to be used

If you're using one of the popular object-relational mappers SQLObject or SQLAlchemy, you don't need DBUtils as they come with their own connection pools. SQLObject 2 (SQL-API) actually borrows some code from DBUtils, separating pooling into a separate layer.

installation and use

pip install DBUtils

This article installs the tested version DBUtils-3.0.3

example

import pymysql
from dbutils.pooled_db import PooledDB

# 定义连接参数
pool = PooledDB(
    creator=pymysql,
    maxconnections=6,
    mincached=2,
    maxcached=5,
    blocking=True,
    host='localhost',
    user='root',
    passwd='123456',
    db='mydb',
    port=3306,
    charset='utf8mb4'
)

# 从连接池获取连接
conn = pool.connection()
cursor = conn.cursor()

Taking pymysql as an example, first define the configuration parameters of the connection pool, then call the pool.connection() method to obtain a connection from the connection pool, and then use this connection to create a cursor object cursor.

# 执行 SQL 语句
sql = "SELECT * FROM students"
cursor.execute(sql)
result = cursor.fetchall()

# 处理查询结果
for row in result:
    print(row)

# 关闭游标和连接
cursor.close()
conn.close()

After use, we need to explicitly close the cursor and connection in order to release resources back to the connection pool.

advanced usage

Sometimes you may want to prepare a connection before it is used by DBUtils, and that's not possible with just the correct parameters. For example, pyodbc may need to configure the connection by calling the connection's setencoding() method. You can do this by passing a modified connect() function to PersistentDB or PooledDB as the creator (first argument), like so:

from pyodbc import connect 
from dbutils.pooled_db import PooledDB 

def creator(): 
    con = connect(...) 
    con.setdecoding(...)
    返回 con 

creator.dbapi = pyodbc 

db_pool = PooledDB(creator, mincached=5)

Guess you like

Origin blog.csdn.net/inthat/article/details/131314367