mysql数据库连接池python

1. DBUtils

官方文档还是11年的,但还是非常简单好用。主要使用其中的PersistentDB 和 PooledDB两个模块。

1. PersistentDB

每当线程第一次打开数据库连接时,PersistentDB将建立一个与该数据库的新连接,该连接将始终用于此特定线程,不能被其他线程使用。当该线程调用close()后,该连接回到线程池,静静的等待这个线程再次调用它(其他线程无法使用这个连接)。该连接最终随该线程死亡而真正关闭。

2. PooledDB

@singleton
class TlcfDBconPool(object):
    def __init__(self):
        db_conf_dict = InternalCFG().get_datayesdb_conf
        self.pool = PersistentDB.PersistentDB(creator=MySQLdb, maxusage=5,host=db_conf_dict['host'], user=db_conf_dict['user'], passwd=db_conf_dict['passwd'],db=db_conf_dict['db'], port=db_conf_dict['port'])

    def get_db_connection(self):
        return self.pool.connection()

2. SqlAlchemy中的连接池

作为高级的ORM工具,SqlAlchemy本身就有连接池的

首先engine.connect()返回的是Connection object.

Return a new Connection object.

The Connection object is a facade that uses a DBAPI connection internally in order to communicate with the database. This connection is procured from the connection-holding Pool referenced by this Engine. When the close() method of the Connection object is called, the underlying DBAPI connection is then returned to the connection pool, where it may be used again in a subsequent call to connect().

engine.raw_connection()返回的是 Raw DBAPI Connections,但是这个依然不是我想要的东西,因为我在代码中依然发现这个connection是“proxied”的,每次返回的object的id都不一样

The name “fairy” is inspired by the fact that the _ConnectionFairy object’s lifespan is transitory, as it lasts only for the length of a specific DBAPI connection being checked out from the pool, and additionally that as a transparent proxy, it is mostly invisible.

engine = DBConnectionFactory().get_db_connection('db')
conn = engine.connect()
print conn.connection.thread_id()
print id(conn)
sql = "select * from mytable where trade_date={2}"
dat = read_db(conn, sql)
dat.columns =['ID', 'factor']
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值