Python使PooledDB连接池连接SQL Server2014

使用连接池有诸多好处:

1.可以减少关闭连接的次数,提示连接的速度

2.进程内线程共享

连接参数定义:

1. mincached,最少的空闲连接数,如果空闲连接数小于这个数,pool会创建一个新的连接
2. maxcached,最大的空闲连接数,如果空闲连接数大于这个数,pool会关闭空闲连接
3. maxconnections,最大的连接数,
4. blocking,当连接数达到最大的连接数时,在请求连接的时候,如果这个值是True,请求连接的程序会一直等待,直到当前连接数小于最大连接数,如果这个值是False,会报错,
5. maxshared 当连接数达到这个数,新请求的连接会分享已经分配出去的连接

from DBUtils.PooledDB import PooledDB
import pymssql


sql ="SELECT  distinct  city FROM xxxx WHERE region=%s"
host='1xxx.xxx.xxx'
port=1433
user='xxx'
password='xxxxxx'
database='xxxxxx'
pool = PooledDB(creator=pymssql,mincached=2, maxcached=5,maxshared=3, maxconnections=6, blocking=True, host=host, port=port, user=user, password=password, database=database, charset="utf8")
conn = pool.connection()
cur = conn.cursor()
cur.execute(sql,"North")
#conn.commit()
row=cur.fetchall()
for i in row:
    print(i)
cur.close()
conn.close()

    

猜你喜欢

转载自blog.csdn.net/lbship/article/details/87896004