python数据库连接池

布满荆棘的人生 2022-07-17 03:43 426阅读 0赞

python-oracle数据库连接池

使用连接池,可以避免每次请求都创建一个连接,直接从连接池获取连接,大大加快连接速度和效率。
python的数据库连接池主要是DBUtils,详细的介绍可以参考:
http://www.tuicool.com/articles/U3ymUb7

这里介绍cx_oracle模块自带的连接池,同时也给出DBUtils的连接池简单使用方法。

  1. # file: pyoracle.py
  2. # cx_oracle自带的线程池模块
  3. import cx_oracle
  4. # 定义全局变量
  5. class global_val:
  6. pools=None
  7. class oraclepool(object):
  8. def __init__(self, user="abc", pwd="123456", ip="192.168.1.16", db="orcl"):
  9. self.user = user
  10. self.password = pwd
  11. self.ip = ip
  12. self.db = db
  13. self.dsn = ip + "/" + db # 注意,oracle接受的参数其实是 dsn
  14. self.conn = OraclePool.get_conn(self)
  15. self.cursor = self.conn.cursor()
  16. def get_conn(self):
  17. """创建连接池,返回数据库连接"""
  18. if GOrcl.pools is None:
  19. GOrcl.pools = cx_Oracle.SessionPool(user=self.user, password=self.password, dsn=self.dsn, min=1, max=30,
  20. increment=2)
  21. return GOrcl.pools.acquire()
  22. def release(self):
  23. """连接不用了,要返回连接池"""
  24. GOrcl.pools.release(self.conn)
  25. def execute(self, sql):
  26. """执行sql语句"""
  27. self.cursor.execute(sql)
  28. self.conn.commit()
  29. def get_conn_num(self):
  30. """获取当前的激活的连接数"""
  31. return GOrcl.pools.busy
  32. # ------------------------------------------------------------
  33. # 使用方法:
  34. # file: test.py
  35. import pandas as pd
  36. import pyoracle
  37. # 实例化
  38. orcl = pyoracle.oraclepool()
  39. # 使用pandas读取数据
  40. data = pd.read_sql("select sysdate from daul", orcl.conn)
  41. # 类的方法
  42. data = orcl.execute("select 1 from dual")
  43. # 将连接放回到连接池
  44. orcl.release()

DBUtils模块,其实更加通用,因为支持所有的标准的数据库,比如mysql,sqlserver等。
这里的例子参考下面的博文:
http://blog.csdn.net/zbc1090549839/article/details/51336458

  1. # file: pyoracle.py
  2. import cx_oracle
  3. from DBUtils.PooledDB import PooledDB
  4. class oracle(object):
  5. """数据连接对象,产生数据库连接池. 此类中的连接采用连接池实现获取连接对象:conn = oracle.getConn() 释放连接对象;conn.close()或del conn """
  6. # 连接池对象
  7. __pool = None
  8. def __init__(self):
  9. # 数据库构造函数,从连接池中取出连接,并生成操作游标
  10. self._conn = oracle.get_onn()
  11. self._cursor = self._conn.cursor()
  12. @staticmethod
  13. def get_onn():
  14. """ 静态方法,从连接池中取出连接 return oracle.connection """
  15. if oracle.__pool is None:
  16. user = config.global_var.user
  17. pwd = config.global_var.pwd
  18. ip = config.global_var.ip
  19. db = config.global_var.db
  20. dsn = ip + "/" + db
  21. __pool = PooledDB(creator=cx_Oracle, mincached=1, maxcached=20, user=user, password=pwd,
  22. dsn=dsn)
  23. return __pool.connection()
  24. def execute(self, sql):
  25. """执行指定sql语句"""
  26. self._cursor.execute(sql) # 执行语句
  27. self._conn.commit() # 然后提交
  28. def close(self):
  29. """释放连接池资源"""
  30. self._cursor.close()
  31. self._conn.close()
  32. #-------------------------------
  33. # 测试
  34. # 1.导入类
  35. import pyoracle
  36. # 2.创建连接池
  37. orcl=pyoracle.oracle()
  38. # 3.使用pandas读取数据
  39. sql = "select sysdate from dual"
  40. df = pd.read_sql(sql, con=orcl._conn) # 访问私有变量的方式
  41. print(df)
  42. # 4.执行语句
  43. orcl.execute(sql)
  44. # 5.最后记得关闭连接,也就是将连接放回连接池
  45. orcl.close()

发表评论

表情:
评论列表 (有 0 条评论,426人围观)

还没有评论,来说两句吧...

相关阅读

    相关 数据库连接

    连接池原理 连接池技术的核心思想是:连接复用,通过建立一个数据库连接池以及一套连接使用、分配、治理策略,使得该连接池中的连接可以得到高效、安全的复用,避免了数据库连接频繁建立