一、dbutils.pooldb
首先是参数:
cx_Oracle:第三方数据库连接库,如果是Mysql数据库的话,替换为pymysql
maxconnections:设置的最大连接数
mincached:连接池初始化时创建的连接数
blocking:True:申请连接时,当连接池内无连接时,任务等待;False:抛出异常
dsn:使用cx_Oracle的makedsn方法制作dsn, dsn组成:host, port, 服务名(server_name)或者sid(数据库唯一标识) (这里的server_name或者sid, 其实就是oracle连接时 host:port/server_name(或者sid), 端口/后边的东西)
from dbutils.pooled_db import PooledDB import cx_Oracle dsn = cx_Oracle.makedsn(host, port, 'helowin'(这里是server_name)) pool = PooledDB(cx_Oracle, maxconnections=10, mincached=2, blocking=True, user=登录数据库用户名, password=登录数据库密码, dsn=dsn) conn = pool.connection() cursor = conn.cursor() print(cursor.execute(sql).fetchall()) cursor.close() conn.close()
二、cx_Oracle.Sessionpool
pool = cx_Oracle.SessionPool(user=登录数据库用户名, password=登录数据库密码, dsn='host:port/helowin', min=1, max=10, increment = True) conn = pool.acquire() cursor = conn.cursor() print(cursor.execute(sql).fetchall())