用sqlalchemy構建Django連線池
As you know, Django uses new database connection for each request. This works well initially. However as the load on the server increases, creating/destroying connections to database starts taking significant amount of time. You will find many questions about using some kind of connection pooling for Django on sites like StackOverflow For example, Django persistent database connection.
At BootStrap Today we use sqlalchemy‘s connection pooling mechanism with Django for pooling the database connections. We use variation of approach by Igor Katson described in http://dumpz.org/67550/. Igor’s approach requires patching Django which we wanted to avoid. Hence we created a small function that we import in one of __init__.py (or models.py) (i.e. some file which gets imported early in the application startup).
pool_initialized=False
def init_pool():
if not globals().get('pool_initialized', False):
global pool_initialized
pool_initialized = True
try:
backendname = settings.DATABASES['default']['ENGINE']
backend = load_backend(backendname)
#replace the database object with a proxy.
backend.Database = pool.manage(backend.Database)
backend.DatabaseError = backend.Database.DatabaseError
backend.IntegrityError = backend.Database.IntegrityError
logging.info("Connection Pool initialized")
except:
logging.exception("Connection Pool initialization error")
#Now call init_pool function to initialize the connection pool. No change required in the
# Django code.
init_pool()
At BootStrap Today we use sqlalchemy‘s connection pooling mechanism with Django for pooling the database connections. We use variation of approach by Igor Katson described in http://dumpz.org/67550/. Igor’s approach requires patching Django which we wanted to avoid. Hence we created a small function that we import in one of __init__.py (or models.py) (i.e. some file which gets imported early in the application startup).
CODE:
import sqlalchemy.pool as poolpool_initialized=False
def init_pool():
if not globals().get('pool_initialized', False):
global pool_initialized
pool_initialized = True
try:
backendname = settings.DATABASES['default']['ENGINE']
backend = load_backend(backendname)
#replace the database object with a proxy.
backend.Database = pool.manage(backend.Database)
backend.DatabaseError = backend.Database.DatabaseError
backend.IntegrityError = backend.Database.IntegrityError
logging.info("Connection Pool initialized")
except:
logging.exception("Connection Pool initialization error")
#Now call init_pool function to initialize the connection pool. No change required in the
# Django code.
init_pool()
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/301743/viewspace-735999/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- django連線池Django
- django中的資料庫連線池實現Django資料庫
- 【JDBC】使用OracleDataSource建立連線池用於連線OracleJDBCOracle
- 連線池
- HTTP連線池HTTP
- Http持久連線與HttpClient連線池HTTPclient
- 連線池和連線數詳解
- ElasticSearch連線池建立Elasticsearch
- 自定義連線池
- 用idea配置c3p0連線池Idea
- django怎麼連線mysqlDjangoMySql
- django 內建server 外網不能訪問, 報連線超時DjangoServer
- 資料庫連線池-Druid資料庫連線池原始碼解析資料庫UI原始碼
- go 語言連線池Go
- Golang SQL連線池梳理GolangSQL
- Tomcat 的 JDBC 連線池TomcatJDBC
- SQLAlchemy - 資料庫的連線、建立會話與模型SQL資料庫會話模型
- 《四 資料庫連線池原始碼》手寫資料庫連線池資料庫原始碼
- OkHttp3原始碼解析(三)——連線池複用HTTP原始碼
- 用 Jenkins 構建 CI/CD 流水線Jenkins
- django | 連線mysql資料庫DjangoMySql資料庫
- golang連線MySQL時候的連線池設定GolangMySql
- 【JDBC】java連線池模擬測試 連線oracleJDBCJavaOracle
- 使用Dockerfile構建django專案DockerDjango
- Flask資料庫連線池Flask資料庫
- HikariCP連線池的學習
- 安裝配置PGBouncer連線池
- cx_Oracle.SessionPool 連線池OracleSession
- MOSN 原始碼解析 - 連線池原始碼
- python資料庫連線池Python資料庫
- Java篇-DBUtils與連線池Java
- Spring系列之HikariCP連線池Spring
- swoole連線池原理解釋
- 資料庫連線池原理資料庫
- Python實現MySQL連線池PythonMySql
- Django與微服務架構:構建可擴充套件的Web應用Django微服務架構套件Web
- 代理Ip池構建及使用
- ServiceStack.Redis的原始碼分析(連線與連線池)Redis原始碼
- 某客戶系統tomcat連線池連線異常Tomcat