|
| 1 | +# django-db-connection-pool |
| 2 | + |
| 3 | +驱动 Django MySQL、Oracle、PostgreSQL连接池的轮子, 基于 SQLAlchemy 队列池 |
| 4 | +* [English version](https://github.com/altairbow/django-db-connection-pool/blob/master/README.md) |
| 5 | + |
| 6 | +#### 快速开始 |
| 7 | +1. 使用 `pip` 安装所有所支持的数据库组件: |
| 8 | + ```bash |
| 9 | + $ pip install django-db-connection-pool[all] |
| 10 | + ``` |
| 11 | + 或者是选择性安装数据库组件: |
| 12 | + ```bash |
| 13 | + $ pip install django-db-connection-pool[mysql,oracle,postgresql] |
| 14 | + ``` |
| 15 | + |
| 16 | +2. 更新 DATABASES 的 配置 |
| 17 | + * ##### MySQL |
| 18 | + 将 ENGINE `django.db.backends.mysql` 更改为 `dj_db_conn_pool.backends.mysql`: |
| 19 | + ``` |
| 20 | + DATABASES = { |
| 21 | + 'default': { |
| 22 | + ... |
| 23 | + 'ENGINE': 'dj_db_conn_pool.backends.mysql' |
| 24 | + ... |
| 25 | + } |
| 26 | + } |
| 27 | + ``` |
| 28 | + |
| 29 | + * ##### Oracle |
| 30 | + 将 ENGINE `django.db.backends.oracle` 更改为 `dj_db_conn_pool.backends.oracle`: |
| 31 | + ``` |
| 32 | + DATABASES = { |
| 33 | + 'default': { |
| 34 | + ... |
| 35 | + 'ENGINE': 'dj_db_conn_pool.backends.oracle' |
| 36 | + ... |
| 37 | + } |
| 38 | + } |
| 39 | + ``` |
| 40 | + * ##### PostgreSQL |
| 41 | + 将 ENGINE `django.db.backends.postgresql` 更改为 `dj_db_conn_pool.backends.postgresql`: |
| 42 | + ``` |
| 43 | + DATABASES = { |
| 44 | + 'default': { |
| 45 | + ... |
| 46 | + 'ENGINE': 'dj_db_conn_pool.backends.postgresql' |
| 47 | + ... |
| 48 | + } |
| 49 | + } |
| 50 | + ``` |
| 51 | + * ##### 连接池配置(可选) |
| 52 | + 目前连接池限制用户传入的连接池配置为:POOL_SIZE(连接池容量)、MAX_OVERFLOW(连接池容量上下浮动最大值) |
| 53 | + 这两个参数包含在 `POOL_OPTIONS` 内,例如下面的配置,default 的连接池常规容量为10个连接,最大浮动10个, |
| 54 | + 即为:在 default 连接池创建后,随着程序对连接池的请求,连接池内连接将逐步增加到10个,如果在连接池内连接 |
| 55 | + 全部用光后,程序又请求了第11个连接,此时的连接池容量将短暂超过 POOL_SIZE,但最大不超过 POOL_SIZE + MAX_OVERFLOW, |
| 56 | + 如果程序请求 default 数据库的连接数量超过 POOL_SIZE + MAX_OVERFLOW,那连接池将一直等待直到程序释放连接。 |
| 57 | + ``` |
| 58 | + DATABASES = { |
| 59 | + 'default': { |
| 60 | + ... |
| 61 | + 'POOL_OPTIONS' : { |
| 62 | + 'POOL_SIZE': 10, |
| 63 | + 'MAX_OVERFLOW': 10 |
| 64 | + } |
| 65 | + ... |
| 66 | + } |
| 67 | + } |
| 68 | + ``` |
| 69 | + |
| 70 | + 附这两个参数的解释:(摘录于 SQLAlchemy 的文档): |
| 71 | + |
| 72 | + * **pool_size**: The size of the pool to be maintained, |
| 73 | + defaults to 5. This is the largest number of connections that |
| 74 | + will be kept persistently in the pool. Note that the pool |
| 75 | + begins with no connections; once this number of connections |
| 76 | + is requested, that number of connections will remain. |
| 77 | + `pool_size` can be set to 0 to indicate no size limit; to |
| 78 | + disable pooling, use a :class:`~sqlalchemy.pool.NullPool` |
| 79 | + instead. |
| 80 | + |
| 81 | + * **max_overflow**: The maximum overflow size of the |
| 82 | + pool. When the number of checked-out connections reaches the |
| 83 | + size set in pool_size, additional connections will be |
| 84 | + returned up to this limit. When those additional connections |
| 85 | + are returned to the pool, they are disconnected and |
| 86 | + discarded. It follows then that the total number of |
| 87 | + simultaneous connections the pool will allow is pool_size + |
| 88 | + `max_overflow`, and the total number of "sleeping" |
| 89 | + connections the pool will allow is pool_size. `max_overflow` |
| 90 | + can be set to -1 to indicate no overflow limit; no limit |
| 91 | + will be placed on the total number of concurrent |
| 92 | + connections. Defaults to 10. |
0 commit comments