Django数据库连接池(mysql)

都知道django每次请求都会连接数据库和释放数据库连接。Django为每个请求使用新的数据库连接。一开始这个方法行得通。然而随着服务器上的负载的增加,创建/销毁连接数据库开始花大量的时间。要避免这个,你可以使用数据库连接池。

本文使用 DBUtils的连接池, 使Django持久化数据库连接。

DBUtils实现django连接池

github代码

DBUtils 是一套用于管理数据库连接池的Python包,为高频度高并发的数据库访问提供更好的性能,可以自动管理连接对象的创建和释放。并允许对非线程安全的数据库接口进行线程安全包装。
django结合DBUtils使用,数据库连接管理完全交给DBUtils处理,平滑切换db backends。

django使用连接的过程:

  • 请求开始: 在第一次访问数据库的时候新建连接(从DBUtils管理的连接池中获取连接)
  • 请求结束: 关闭连接(实际由DBUtils将连接返还连接池)

DBUtils

DBUtils提供两种外部接口:

  • PersistentDB :提供线程专用的数据库连接(为每个线程创立连接,仅供自己的线程再次使用,当线程终止时,连接自动关闭),并自动管理连接。
    在这里插入图片描述

  • PooledDB :提供线程间可共享的数据库连接(创建一批连接,供所有线程共享使用,当线程终止时,连接自动关闭),并自动管理连接。
    在这里插入图片描述

Note:通过DBUtils获取的连接关闭时(调用conn.close() )并未正在关闭连接,只是将连接重新放回池子。

如果您的应用程序主程用的线程池,保持使用数据库的线程数量不变,那么使用PersistentDB更好。但是,如果您的应用程序频繁地启动和结束线程,那么使用PooledDB会更好

实测证明 PersistentDB 的速度是最高的,但是在某些特殊情况下,数据库的连接过程可能异常缓慢,而此时的PooledDB则可以提供相对来说平均连接时间比较短的管理方式。

django settings

DATABASES = {
    "default": {
        "ENGINE": "db_pool.db.backends.mysql",
        "NAME": "xxx",
        "USER": "xxx",
        "PASSWORD": "xxx",
        "HOST": "mysql",
        "PORT": "3306",
        "ATOMIC_REQUESTS": True,
        "CHARSET": "utf8",
        "COLLATION": "utf8_bin",
        "POOL": {
            "mincached": 5,
            "maxcached ": 500,
        }
    }
}
其中连接池(POOL)配置参见 DBUtils的 PooledDB参数:
mincached: initial number of idle connections in the pool
    (0 means no connections are made at startup)
maxcached: maximum number of idle connections in the pool
    (0 or None means unlimited pool size)
maxshared: maximum number of shared connections
    (0 or None means all connections are dedicated)
    When this maximum number is reached, connections are
    shared if they have been requested as shareable.
maxconnections: maximum number of connections generally allowed
    (0 or None means an arbitrary number of connections)
blocking: determines behavior when exceeding the maximum
    (if this is set to true, block and wait until the number of
    connections decreases, otherwise an error will be reported)
maxusage: maximum number of reuses of a single connection
    (0 or None means unlimited reuse)
    When this maximum usage number of the connection is reached,
    the connection is automatically reset (closed and reopened).
setsession: optional list of SQL commands that may serve to prepare
    the session, e.g. ["set datestyle to ...", "set time zone ..."]
reset: how connections should be reset when returned to the pool
    (False or None to rollback transcations started with begin(),
    True to always issue a rollback for safety's sake)
failures: an optional exception class or a tuple of exception classes
    for which the connection failover mechanism shall be applied,
    if the default (OperationalError, InternalError) is not adequate
ping: determines when the connection should be checked with ping()
    (0 = None = never, 1 = default = whenever fetched from the pool,
    2 = when a cursor is created, 4 = when a query is executed,
    7 = always, and all other bit combinations of these values)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值