altairbow / django-db-connection-pool

Database connection pool component library for Django
https://pypi.python.org/pypi/django-db-connection-pool/
MIT License
195 stars 24 forks source link
database database-pool database-pooling django jdbc mysql oceanbase odbc pool pooling postgres postgresql python

django-db-connection-pool

:star: If this project is helpful to you, please light up the star, Thank you:smile:

MySQL & Oracle & PostgreSQL & JDBC (Oracle, OceanBase) connection pool components for Django, Be based on SQLAlchemy. Works fine in multiprocessing and multithreading django project.

Quickstart

Installation

Install with pip with all engines:

$ pip install django-db-connection-pool[all]

or select specific engines:

$ pip install django-db-connection-pool[mysql,oracle,postgresql,jdbc]

or one of mysql,oracle,postgresql,jdbc

$ pip install django-db-connection-pool[oracle]

Update settings.DATABASES

MySQL

change django.db.backends.mysql to dj_db_conn_pool.backends.mysql:

DATABASES = {
    'default': {
        'ENGINE': 'dj_db_conn_pool.backends.mysql'
    }
}

Oracle

change django.db.backends.oracle to dj_db_conn_pool.backends.oracle:

DATABASES = {
    'default': {
        'ENGINE': 'dj_db_conn_pool.backends.oracle'
    }
}

PostgreSQL

change django.db.backends.postgresql to dj_db_conn_pool.backends.postgresql:

DATABASES = {
    'default': {
        'ENGINE': 'dj_db_conn_pool.backends.postgresql'
    }
}

Pool options(optional)

you can provide additional options to pass to SQLAlchemy's pool creation, key's name is POOL_OPTIONS:

DATABASES = {
    'default': {
        'POOL_OPTIONS': {
            'POOL_SIZE': 10,
            'MAX_OVERFLOW': 10,
            'RECYCLE': 24 * 60 * 60
        }
    }
}

django-db-connection-pool has more configuration options here: PoolContainer.pool_default_params

Here's the explanation of these options(from SQLAlchemy's Doc):

Or, you can use dj_db_conn_pool.setup to change default arguments(for each pool's creation), before using database pool:

import dj_db_conn_pool

dj_db_conn_pool.setup(pool_size=100, max_overflow=50)

multiprocessing environment

In a multiprocessing environment, such as uWSGI, each process will have its own dj_db_conn_pool.core:pool_container object, It means that each process has an independent connection pool, for example: The POOL_OPTIONS configuration of database db1 is{ 'POOL_SIZE': 10, 'MAX_OVERFLOW': 20 }, If uWSGI starts 8 worker processes, then the total connection pool size of db1 is 8 * 10, The maximum number of connections will not exceed 8 * 10 + 8 * 20

JDBC

Thanks to JPype, django-db-connection-pool can connect to database by jdbc

Usage

Set Java runtime environment

export JAVA_HOME=$PATH_TO_JRE;
export CLASSPATH=$PATH_RO_JDBC_DRIVER_JAR

Update settings.DATABASES

Oracle

change django.db.backends.oracle to dj_db_conn_pool.backends.jdbc.oracle:

DATABASES = {
    'default': {
        'ENGINE': 'dj_db_conn_pool.backends.jdbc.oracle'
    }
}
OceanBase

use dj_db_conn_pool.backends.jdbc.oceanbase:

DATABASES = {
    'default': {
        'ENGINE': 'dj_db_conn_pool.backends.jdbc.oceanbase'
    }
}

Performing raw SQL queries

Just like django's built-in backends, all JDBC backends support named parameters in raw SQL queries, you can execute raw sql queries like this:

from django.db import connections

with connections["default"].cursor() as cursor:
    cursor.execute('select name, phone from users where name = %(name)s', params={"name": "Altair"})
    result = cursor.fetchall()

Acknowledgments