用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/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 【JDBC】使用OracleDataSource建立連線池用於連線OracleJDBCOracle
- 連線池
- Go連線池Go
- HTTP連線池HTTP
- 連線池優化之啟用PoolPreparedStatements優化
- Http持久連線與HttpClient連線池HTTPclient
- 連線池和連線數詳解
- ElasticSearch連線池建立Elasticsearch
- 自定義連線池
- golang tcp連線池GolangTCP
- Oracle 連線池配置Oracle
- oracle occi 連線池Oracle
- Resin 配置連線池
- [尋]連線池例子
- proxool連線池 配置
- 連線池 druidUI
- java連線池解決連線中斷Java
- proxool連線池如何使用SSL方式連線?
- django怎麼連線mysqlDjangoMySql
- OkHttp3原始碼分析[複用連線池]HTTP原始碼
- Golang SQL連線池梳理GolangSQL
- go 語言連線池Go
- DBCP連線池原理分析
- Hibernate連線池配置
- Tomcat連線池使用Tomcat
- 資料庫連線池資料庫
- 《四 資料庫連線池原始碼》手寫資料庫連線池資料庫原始碼
- 資料庫連線池-Druid資料庫連線池原始碼解析資料庫UI原始碼
- 用idea配置c3p0連線池Idea
- OkHttp3原始碼解析(三)——連線池複用HTTP原始碼
- django | 連線mysql資料庫DjangoMySql資料庫
- 【JDBC】java連線池模擬測試 連線oracleJDBCJavaOracle
- golang連線MySQL時候的連線池設定GolangMySql
- 各位老大,用tomcat的連線池在application獲得連線的問題TomcatAPP
- 在WeLogic下建連線池的錯誤,請幫忙看看!
- 執行緒池、連線池、物件池從0到1執行緒物件
- cx_Oracle.SessionPool 連線池OracleSession
- Java篇-DBUtils與連線池Java