python数据库连接池
一丶持久数据库 (persistent_db)
1. 每当线程第一次打开数据库连接时,将打开一个到数据库的新连接,该连接将从现在开始用于该特定线程
2. 当线程关闭数据库连接时,它仍然保持打开状态,以便下次同一个线程请求连接时,可以使用这个已经打开的连接
3. 当线程死亡时,连接将自动关闭
简而言之:
persistent_db尝试回收数据库连接以提高线程应用程序的整体数据库访问性能,但它确保线程之间永远不会共享连接
二丶池化数据库 (pooled_db)
三丶持久数据库 (persistent_db)
- creator : 返回新的 DB-API 2 连接对象的任意函数或符合 DB-API 2 的数据库模块
- maxusage : 单个连接的最大重用次数(默认为0 或None 表示无限重用)
每当达到限制时,连接将被重置
- setsession : 可用于准备会话的 SQL 命令的可选列表,例如["set datestyle to German" , ...]
- failures : 如果默认值(OperationalError,InterfaceError,InternalError)不适用于使用的数据库模块,则应应用连接故障转移机制的可选异常类或异常类元组
- ping : 一个可选标志,用于控制何时使用ping()方法检查连接,如果这种方法可用(0 =无= 从不,1 = 默认 = 每当请求时, 2 = 创建游标时,4 = 当执行查询, 7 = 总是,以及这些值的所有其他位组合)
- closeable : 如果设置为 true,则允许关闭连接,但默认情况下,这将被忽略
- threadlocal : 一个可选类,用于表示将使用的线程本地数据,而不是我们的 Python 实现(threading.local 更快,但不能在所有情况下都使用)
import pgdb
from dbutils.persistent_db import PersistentDB
persist = PersistentDB(pgdb, 1000 , database='mydb' )
db = persist.connection()
四丶池化数据库 (pooled_db)
- creator : 返回新的 DB-API 2 连接对象的任意函数或符合 DB-API 2 的数据库模块
- mincached : 池中的初始空闲连接数(默认为0 表示启动时不建立连接)
- maxcached : 池中的最大空闲连接数(默认值0 或None 表示无限池大小)
- maxshared : 允许的最大共享连接数(默认值0 或None 表示所有连接都是专用的)
当达到此最大数量时,如果连接被请求为可共享,则连接将被共享。
- maxconnections : 一般允许的最大连接数(默认值0 或None 表示任意数量的连接)
- blocking : 确定超过最大值时的行为
- maxusage : 单个连接的最大重用次数(默认为0 或None 表示无限重用), 当达到此连接的最大使用次数时,连接会自动重置(关闭并重新打开)
- setsession : 可用于准备会话的 SQL 命令的可选列表,例如["set datestyle to German" , ...]
- reset : 返回池时应如何重置连接(False 或None 回滚以begin() 开始的事务,默认值True 出于安全考虑总是发出回滚)
- failures : 如果默认值(OperationalError,InterfaceError,InternalError)不适用于使用的数据库模块,则应应用连接故障转移机制的可选异常类或异常类元组
- ping : 一个可选标志,用于控制何时使用ping()方法检查连接(如果此类方法可用)(0 =无= 从不,1 = 默认 = 每当从池中获取时, 2 = 创建游标时,4 = 何时执行查询, 7 = 总是,以及这些值的所有其他位组合)
import pgdb
from dbutils.pooled_db import PooledDB
pool = PooledDB(pgdb, 5 , database='mydb' )
db = pool.connection()
db = pool.connection(shareable=False )
↓
db = pool.dedicated_connection()
db = pool.connection()
cur = db.cursor()
cur.execute(...)
res = cur.fetchone()
cur.close()
db.close()
with pool.connection() as db:
with db.cursor() as cur:
cur.execute(...)
res = cur.fetchone()
五丶 代码+官网
https://webwareforpython.github.io/DBUtils/main.html
import os
import logging
import pymysql
from dbutils.pooled_db import PooledDB
base_path = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
logging.basicConfig(
level=30 ,
filename=os.path.join(base_path, 'log' , 'db_process.log' ),
filemode='a' ,
)
class DbTool (object ):
"""
# 单例模式 + 数据连接池
"""
mincached = 10
maxcached = 20
maxshared = 10
maxconnections = 200
blocking = True
maxusage = 100
setsession = None
reset = True
_pool = None
__instance = None
def __init__ (self, db_config ):
host = db_config['host' ]
port = db_config['port' ]
user = db_config['user' ]
password = db_config['password' ]
db = db_config['db' ]
if not self._pool:
self._pool = PooledDB(
creator=pymysql,
maxconnections=self.maxconnections,
mincached=self.mincached,
maxcached=self.maxcached,
blocking=self.blocking,
maxusage=self.maxusage,
setsession=self.setsession,
host=host,
port=port,
user=user,
password=password,
database=db,
)
logging.info('SUCCESS: create postgresql success.\n' )
def __new__ (cls, *args, **kwargs ):
"""
:param args:
:param kwargs:
"""
if not cls.__instance:
cls.__instance = super (DbTool, cls).__new__(cls)
return cls.__instance
def pg_select_operator (self, sql ):
'''
# 查询 SQL
:param sql: sql语句
:return: sql结果
'''
conn = self._pool.connection()
cursor = conn.cursor()
result = False
try :
cursor.execute(sql)
result = cursor.fetchall()
except Exception as e:
logging.error('ERROR:' , e)
finally :
cursor.close()
conn.close()
return result
def pg_update_operator (self, sql ):
result = False
conn = self._pool.connection()
cursor = conn.cursor()
try :
cursor.execute(sql)
result = True
except Exception as e:
logging.error('ERROR:' , e)
finally :
cursor.close()
conn.commit()
conn.close()
return result
def pg_insert_operator (self, sql ):
result = False
conn = self._pool.connection()
cursor = conn.cursor()
try :
cursor.execute(sql)
result = True
except Exception as e:
logging.error('ERROR:' , e)
finally :
cursor.close()
conn.commit()
conn.close()
return result
def close_pool (self ):
'''
关闭连接池
:return:
'''
if self._pool != None :
self._pool.close()
if __name__ == '__main__' :
db_config = {
"host" : "127.0.0.1" ,
"port" : 3306 ,
"user" : "root" ,
"password" : "" ,
"db" : "testdb"
}
obj = DbTool(db_config)
sql = """
INSERT INTO `testdb`.`p_user`(`id`, `user_id`, `user_name`, `user_sex`, `user_phone`, `user_email`, `user_pwd`, `isadmin`, `create_time`, `end_time`, `user_origal`) VALUES (12, '10000001', 'admin_x', '1', '13811111112', 'admin_x@163.com', 'pbkdf2:sha256:150000$EU5aTt0N$555f3c7a0d28c092f8b5c3402f0218fffd51b6fa83bab54fed1670f969898c55', '1', '2021-08-01 11:34:07', NULL, NULL);
"""
print ('id:' , id (obj))
try :
obj.pg_insert_operator(sql)
except Exception as e:
print (e)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了